搜索了很多,但无法找到问题的解决方案。 首先,我有一个脚本,我通过本地Veeam数据库进行一些查询。 它有几个选择的查询,但每次我运行它,它只给我第一个的结果,但不是从其他人。我只是想让代码变得更加瘦,所以也许有人可以给我一个提示 我只得到$ 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()
}
答案 0 :(得分:2)
这对你有用。只需添加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