Author Posts

September 9, 2015 at 3:01 am

Hello Everyone,

The scenario is I have a table that contains a list of standard issues as shown below,

$List
——————————————————————————–
New: — Health Service Heartbeat Failure
This computer is offline
Jaguar Service Will be Restarted
Secondary – is down
———————————————————————————

I need to compare a string value through the list that has some more added values in it.
eg.
$variable =[string]"Secondary – VM-ABC123 is down"

if($list -match $variable)
{
echo "Standard Issue"
}
else
{
echo "New Issue"
}

– As you could see the string in the variable has the server name in it but the standard issue will not have the server names . How will I match it ?

Any suggestions will be a great help..
thanks in advance

September 9, 2015 at 4:17 am

Check out the -Contains operator:

#Create an array of standard issue subjects
$stdIssue = "New: — Health Service Heartbeat Failure",
            "This computer is offline"
            "Jaguar Service Will be Restarted"
            "Secondary – is down"


#Another array that contains some test subjects
$tests = "Secondary – VM-ABC123 is down", "This computer is offline"


foreach ($test in $tests) {
    #You can use the -contains operator to see if the
    #subject is in the $stdIssue array
    if ($stdIssue -contains $test) {
        "{0} is a STANDARD issue" -f $test
    }
    else {
        "{0} is a NEW issue" -f $test
    }
}

September 9, 2015 at 5:23 am

@Rob, Thanks for this quick response. will "-contains" will do the needful because you see the string "Secondary – is down" is a standard issue and for a string "Secondary – VM-ABC123 is down" although it has a VM name added to it this should be shown as standard issue and not as new issue.I guess some regex would solve the problem.

September 9, 2015 at 5:32 am

I got one solution i.e. if the computer names will be in each line of the $list array, we can just add a * and then do a -like