Set mutiple values in one parameter

Jarno343

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:

  • TestC <=

but the actual result is shows:

  • TestA <=
  • TestC <=

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

Andrey Marchuk

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.

edited at
0

Comments

0 comments
Login to comment

Related

Add mutiple values to one column

How to pass mutiple values as single parameter in ssrs

How to bind mutiple values to one label?

SSRS - Multiple Tablix Filters Based on Mutiple Parameter Values

Using one method to set values of different variables loading by parameter

Mutiple Values in single enum

How to get data by mutiple values on one column with different values on other columns from database in Entity Framework?

Compare mutiple images with one images

How to set parameter values in RDLC

GAMS - Parameter containing set values

why does return two values in pandas apply does not set the entire row like in one return parameter?

Set parameter values according to other parameter value

more then one Values in one Parameter from $_get

how to set the values of one constructor with the values of another?

One javadoc description for mutiple methods/constants

Find a string in one section of a file with mutiple sections

Add mutiple file paths into one in shell script

Mutiple consecutive outputs instead of one in a function in PHP

Append mutiple tables to one datatable in c#

Joining mutiple select statements in one SQL statement

Comparing mutiple columns and selecting only one

generate one of the mutiple possibles anagrams of a string

Add mutiple values to Django Foreign Key

Count mutiple column values from table

Can you assign mutiple values to this? and if so, how?

how to get values based on mutiple date combinations

How to hold the mutiple values of type in HashMap?

Find items with duplicate values over mutiple properties

Adding mutiple values in regex for pattern match