Powershell - Need help

I'm building a script to snapshot all of our servers just before patching (we have over 200), my script is supposed to check the name and if it does not have a string in the name its not supposed to patch, but I can not get it to check the name.  I have used just about every comparison I can think of, but I'm fairly new to powershell.

Connect-VIServer -User "someusername" -Password "*********" -Server "FQDN"
$VMS = get-vm | where { ($_.PowerState -eq "PoweredOn") }
$date=Get-Date -Format dd-MMM-yyyy

$number = $VMS.Count
$num = 1
$time = Get-Date
foreach ($vm in $vms) {
    If ($vm.name -notcontains "_rogue_") {
    New-Snapshot -VM "$VM" -Name ("$date" + "_Patch_cycle") -Description "Auto Snap taken before Patch cycle $date" -Memory
    write-host "Snapshot on $VM completed, $num of $number @ $time" 
    $num = $num +1}
    } 

the snap potion works, it's just snapping every host, and I have 20 - 30 host with _rogue_ in the middle of the name, thta I would like to NOT snap, as they don't really need it... Ideally I have two Strings I would like to exclude, but I could not get the singe string to work let alone 2 strings

August 20th, 2015 1:57pm

You want -NotLike instead of -NotContains. Read more about comparison operators here:

about_Comparison_Operators

Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 2:08pm

Hi,

I'd start here:


$vm.name -notlike '*_rogue_*'
August 20th, 2015 2:15pm

thanks, thought I had tried that....maybe I missed the *

should I be able to pipe | multiple Strings?
Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 2:38pm

should I be able to pipe | multiple Strings?

What?

If you're asking if you can do multiple tests in your if statement, the answer is yes.

http://ss64.com/ps/syntax-compare.html

August 20th, 2015 2:57pm

You want -NotLike instead of -NotContains. Read more about comparison operators here:

about_Comparison_Operators

  • Proposed as answer by Mike Laughlin Thursday, August 20, 2015 6:12 PM
Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 6:04pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics