I have written the following script to compare jobs that I have saved in a folder and compare them by name on the servers.
$submaporiginal = 'D:\Jobs_from_Server\TEST_Jobs_Aangepast'
$SqlServer1Name = "servername"
$SqlServer1 = New-Object Microsoft.SqlServer.Management.Smo.Server($SqlServer1Name)
$files = Get-ChildItem $submaporiginal *.sql
$SqlServer1JobListing = $SqlServer1.JobServer.Jobs |
Select-Object -ExpandProperty Name
foreach ($file in $files)
{
$stap1 = $file.Name
$locatiedtsx = $stap1.IndexOf("_goed")
$SqlServer2JobListing = $stap1.Substring(0,$locatiedtsx)
}
Compare-Object $SqlServer1JobListing $SqlServer2JobListing
{
if ($_.SideIndicator -eq "<=")
{Write-Host $_}
}
After running it shows a list with all the jobs that are currently on the server but are not saved in the folder: TEST_Jobs_Aangepast
My problem is that currently it only ignores the last job that have run trough the foreach ($file in $files)
For excample in the map are the files TestA.sql and TestB.SQL while the server has a job named testA, TestB and TestC. The result shown should be:
but the actual result is shows:
My question is how can I set all the values that go trough foreach ($file in $files)
in one parameter SqlServer2JobListing
so that all the jobs that match between $SqlServer1JobListing $SqlServer2JobListing
are not shown
Your problem is that you always reassign the $SqlServer2JobListing
variable, instead you need to add a value to array or list. Like this:
$SqlServer1JobListing = New-Object System.Collections.ArrayList
foreach ($file in $files)
{
$stap1 = $file.Name
$locatiedtsx = $stap1.IndexOf("_goed")
$SqlServer1JobListing.Add($stap1.Substring(0,$locatiedtsx))
}
This way, after your foreach
loop you will get a collection of all the items, rather than only the last one like you had before.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments