1

searched a lot but couldn't find a solution to my problem. First I have a script from which i make a few querys through a local Veeam database. It has a few select querys but everytime I run it, it just gives me the results of the first one but not from the others. I just tried to make the code a little more skinny so maybe somebody could give me a tip I just get the results for $job and $space_backup. I don't want to have four different querys but if it's not possible I have to go with it. I would appreciate any help.

$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()
}
2
  • 1
    Run each query separately. Commented Feb 2, 2016 at 12:00
  • 3
    any other ideas instead of this unhelpful comment? Commented Feb 2, 2016 at 13:50

1 Answer 1

4

This should work for you. Just add the SqlDataAdapter and DataSet in place of your $SQLReader = $SQLCommand.ExecuteReader() command and make the selects one long string. I ended each of mine while testing this with a ; .

$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)  "
}
Keep adding a Foreach for each query

Sign up to request clarification or add additional context in comments.

3 Comments

Doesn't work as I imagined, when I try it in PS i get a new table with the input from "free" but it won't give me anything in the variable $free, same with other foreach. It won't fill the table.
Got the mistake had to put the $row variable to $row2 and so on Foreach ($row in $readSet.Tables[0].rows) {$space_backup = $row[0]; $job = $row[1]} Foreach ($row2 in $readSet.Tables[1].rows) {$space_free = $row2[0]} Foreach ($row3 in $readSet.Tables[2].rows) {$space_all = $row3[0]} Foreach ($row4 in $readSet.Tables[3].rows) {$reason = $row4[0]; $detail = $row4[1] } works like a charm thank you!
You're correct. I just edited my answer to add the $row.size. Apologies for the mistake.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.