使用 Powershell 的多个 SQL Select 查询
Multiple SQL Select Queries with Powershell
进行了大量搜索,但找不到解决我的问题的方法。
首先,我有一个脚本,我从中通过本地 Veeam 数据库进行一些查询。
它有几个 select 查询,但每次我 运行 它,它只给我第一个的结果,而不是其他的。我只是想让代码更简洁一些,所以也许有人可以给我提示
我刚得到 $job 和 $space_backup 的结果。我不想有四个不同的查询,但如果不可能,我必须接受它。如果有任何帮助,我将不胜感激。
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=$dbServer;Database=$db;uid=$User;password=$Pass;"
$SQLConnection.Open()
$SQLCommand = $SQLConnection.CreateCommand()
$SQLCommand.CommandText =
"SELECT TOP 1 stored_size AS size, job_name AS job FROM [VeeamBackup].[dbo].[ReportSessionView] ORDER BY [creation_time] DESC;"
"SELECT [free_space] AS [free] FROM [VeeamBackup].[dbo].[BackupRepositories] WHERE (name != 'Default Backup Repository');"
"SELECT SUM(backup_size) AS backupsize FROM [VeeamBackup].[dbo].[WmiServer.RestorePointsView];"
"SELECT TOP 1 reason AS Reason, stop_details AS Detail FROM [VeeamBackup].[dbo].[Backup.Model.JobSessions] ORDER BY creation_time DESC;"
$SQLReader = $SQLCommand.ExecuteReader()
foreach($SQLCommand in $SQLReader.Read()){
$space_backup = $SQLReader["size"]
$job = $SQLReader["job"]
$space_free = $SQLReader["free"]
$space_all = $SQLReader["backupsize"]
$reason = $SQLReader["Reason"]
$detail = $SQLReader["Detail"]
$SQLReader.Close()
}
这应该适合你。只需添加 SqlDataAdapter 和 DataSet 来代替您的 $SQLReader = $SQLCommand.ExecuteReader() 命令,然后选择一个长字符串。我在用 ; .
$readAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$readSet = New-Object System.Data.DataSet
$readAdapter.SelectCommand = $SQLCommand
$readAdapter.Fill($readSet) |out-null
$SQLConnection.Close()
Foreach ($row in $readSet.Tables[0].rows) {
Write-Output "Do what you want with the first query results here"
Write-Output "$($row.size) $($row.job)"
}
Foreach ($row in $readSet.Tables[1].rows) {
Write-Output "Do what you want with the second query results here"
Write-Output "$($row.free) "
}
测试时结束了我的每一个
继续为每个查询添加一个 Foreach
进行了大量搜索,但找不到解决我的问题的方法。 首先,我有一个脚本,我从中通过本地 Veeam 数据库进行一些查询。 它有几个 select 查询,但每次我 运行 它,它只给我第一个的结果,而不是其他的。我只是想让代码更简洁一些,所以也许有人可以给我提示 我刚得到 $job 和 $space_backup 的结果。我不想有四个不同的查询,但如果不可能,我必须接受它。如果有任何帮助,我将不胜感激。
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=$dbServer;Database=$db;uid=$User;password=$Pass;"
$SQLConnection.Open()
$SQLCommand = $SQLConnection.CreateCommand()
$SQLCommand.CommandText =
"SELECT TOP 1 stored_size AS size, job_name AS job FROM [VeeamBackup].[dbo].[ReportSessionView] ORDER BY [creation_time] DESC;"
"SELECT [free_space] AS [free] FROM [VeeamBackup].[dbo].[BackupRepositories] WHERE (name != 'Default Backup Repository');"
"SELECT SUM(backup_size) AS backupsize FROM [VeeamBackup].[dbo].[WmiServer.RestorePointsView];"
"SELECT TOP 1 reason AS Reason, stop_details AS Detail FROM [VeeamBackup].[dbo].[Backup.Model.JobSessions] ORDER BY creation_time DESC;"
$SQLReader = $SQLCommand.ExecuteReader()
foreach($SQLCommand in $SQLReader.Read()){
$space_backup = $SQLReader["size"]
$job = $SQLReader["job"]
$space_free = $SQLReader["free"]
$space_all = $SQLReader["backupsize"]
$reason = $SQLReader["Reason"]
$detail = $SQLReader["Detail"]
$SQLReader.Close()
}
这应该适合你。只需添加 SqlDataAdapter 和 DataSet 来代替您的 $SQLReader = $SQLCommand.ExecuteReader() 命令,然后选择一个长字符串。我在用 ; .
$readAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$readSet = New-Object System.Data.DataSet
$readAdapter.SelectCommand = $SQLCommand
$readAdapter.Fill($readSet) |out-null
$SQLConnection.Close()
Foreach ($row in $readSet.Tables[0].rows) {
Write-Output "Do what you want with the first query results here"
Write-Output "$($row.size) $($row.job)"
}
Foreach ($row in $readSet.Tables[1].rows) {
Write-Output "Do what you want with the second query results here"
Write-Output "$($row.free) "
}
测试时结束了我的每一个
继续为每个查询添加一个 Foreach