I'm eating my brain out on a silly issue... I have a plugin in vCheck called 27 VM Tools Issues.ps1 which I modified like this:
# Start of Settings
# VM Tools Issues, do not report on any VMs who are defined here
$VMTDoNotInclude = "VM1_*|VM2_*"
# End of Settings
$Result = $FullVM | Where {$_.Name -notmatch $VMTDoNotInclude} | Where {$_.Guest.State -eq "Running" -And ($_.Guest.IPAddress -eq $NULL -or $_.Guest.HostName -eq $NULL -or $_.Guest.ExtensionData.Disk -eq $NULL)} | Select Name
$Result
but it produces not output. While if I run it in ISE (as below) it does show a couple of VMs. I can't undestand why the different behaviour!!?
$VMTDoNotInclude = "VM1_*|VM2_*"
$Result = Get-VM | Where {$_.Name -notmatch $VMTDoNotInclude} | Where {$_.Guest.State -eq "Running" -And ($_.Guest.IPAddress -eq $NULL -or $_.Guest.HostName -eq $NULL -or $_.Guest.ExtensionData.Disk -eq $NULL)} | Select Name
$Result
The * character has a special meaning in a RegEx.
It is a quantifier, and says that the previous character shall be matched between 0 and unlimited times.
So your string in fact says:
So displaynames like ABCVM1 and VM1ABC will not get through.
If that is what you envisaged, fine.
On the difference between your local run in the ISE and the run in vCheck.
I would start analysing by checking if $FullVM have the same VMs in the ISE as it has in vCheck.
In vCheck the content of $FullVM is obtained by
$FullVM = Get-View -ViewType VirtualMachine | Where-Object {-not $_.Config.Template}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I suspect the issue comes from the fact that the -notmatch comparison operator expects a RegEx expression on the right side.
Try the following, it will exclude all VMs whose Displayname starts with VM1_ or VM2_
$VMTDoNotInclude = "^VM1_|^VM2_"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
thanks Luc, but it does not work. However the same variable is used in both context, within vCheck and simply run using ISE. The VMs that are listed by simply running the piece of code in ISE do need action as the tools are not in a healthy state. However when run in vCheck nothing is reported. The code is identical... I can't understand..
The * character has a special meaning in a RegEx.
It is a quantifier, and says that the previous character shall be matched between 0 and unlimited times.
So your string in fact says:
So displaynames like ABCVM1 and VM1ABC will not get through.
If that is what you envisaged, fine.
On the difference between your local run in the ISE and the run in vCheck.
I would start analysing by checking if $FullVM have the same VMs in the ISE as it has in vCheck.
In vCheck the content of $FullVM is obtained by
$FullVM = Get-View -ViewType VirtualMachine | Where-Object {-not $_.Config.Template}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks again. Although the variable with the * is used in both scenarios (and so it could not be the source of the issue) I changed it. As you suggested it's good to keep the * out of the way there.
Going to the difference between the output of Get-VM and what contained in $FullVM I can say that they are almost the same. This vCheck checks one vCenter only. However thanks to your suggestion I could reproduce the issue in ISE, so:
Get-VM | Where {$_.Name -notmatch $VMTDoNotInclude} |
Where {$_.Guest.State -eq "Running" -And ($_.Guest.IPAddress -eq $NULL -or $_.Guest.HostName -eq $NULL -or $_.Guest.ExtensionData.Disk -eq $NULL)} | Select Name
the string above produces the desired output, which is a list of VMs with VMtools issues. While the line below does not produce anything:
Get-View -ViewType VirtualMachine | Where {$_.Name -notmatch $VMTDoNotInclude} |
Where {$_.Guest.State -eq "Running" -And ($_.Guest.IPAddress -eq $NULL -or $_.Guest.HostName -eq $NULL -or $_.Guest.ExtensionData.Disk -eq $NULL)} | select Name
So the difference is the format of the output between the Get-VM and the Get-View and therefore I had to change some properties names in the dotted notation and it worked!
$FullVM | Where {$_.Name -notmatch $VMTDoNotInclude} |
Where {$_.Guest.GuestState -eq "Running" -And ($_.Guest.IPAddress -eq $NULL -or $_.Guest.HostName -eq $NULL -or $_.Guest.Disk -eq $NULL)} | select Name
Thanks again! I'll bear that in mind when amending/troubleshooting vCheck