In our environment we are looking for a way to make sure certain VMs are on the correct storage node. I've got working code now but like all things I would like to make it that much better.
For the most part we want our odd number VMs to stay on our "A-Side" storage and our even number VMs to stay on the "B-Side" storage. A VM is odd or even by the number assigned to it from within the Notes Field.
E.G. Notes for would look like this for a EVEN VM. "Prod - Entitlements - Ticketing TC #02"
This is the line I'm looking to clean up.
$VMs = get-datacenter <DataCenter> | Get-DatastoreCluster | where {$_.Name.Contains('-A-Side')} | Get-VM | where {$_.Notes.Contains('#2 ') -or $_.Notes.Contains('#4') -or $_.Notes.Contains('#6') -or $_.Notes.Contains('#8') -or $_.Notes.Contains('#10') -or $_.Notes.Contains('#12') -or $_.Notes.Contains('#14') -or $_.Notes.Contains('#16') -or $_.Notes.Contains('#18') -or $_.Notes.Contains('#20') -or $_.Notes.Contains('#22') -or $_.Notes.Contains('#24') -or $_.Notes.Contains('#26') -or $_.Notes.Contains('#04') -or $_.Notes.Contains('#06') -or $_.Notes.Contains('#08')} | select Name, Notes
Yes it's WAAAAY too long, however it DOES get the job done. I also have to have a space after "#2 " to make sure it doesn't grab #20 VMs etc.
Now I know you can use % 2 -eq 0 to find odd or even but I'm not sure how to make powershell look at the notes field, pull out the #XX and then check to see if that's odd or even.
Ideas?
Try with a RegEx expression and the modulo function.
Something like this
$VMs = get-datacenter <DataCenter> |
Get-DatastoreCluster |
where {$_.Name.Contains('-A-Side')} |
Get-VM |
where {([regex]::Match($_.Notes,"#(\d*)").Groups[1].Value % 2) -eq 0) |
Select Name, Notes
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try with a RegEx expression and the modulo function.
Something like this
$VMs = get-datacenter <DataCenter> |
Get-DatastoreCluster |
where {$_.Name.Contains('-A-Side')} |
Get-VM |
where {([regex]::Match($_.Notes,"#(\d*)").Groups[1].Value % 2) -eq 0) |
Select Name, Notes
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You continue to scare me! That worked great, just have one SMALL problem. We have a some VMs that do NOT have a #?? in the notes, this line of code if picking them up when it should leave them alone. Ideas?
So not sure if this is the best way but it seems to work well in our setup.
$VMs = get-datacenter <DataCenter> |
Get-DatastoreCluster |
where {$_.Name.Contains('-A-Side')} |
Get-VM |
where {$_.Notes.Contains(" #") -and ([regex]::Match($_.Notes, "#(\d*)").Groups[1].Value % 2) -eq 0 } |
select Name, Notes
Thanks again for the help!
How do these lines without the # look ?
Something like "Prod - Entitlements - Ticketing TC 02" ?
Hope I didn't scare you too much, don't want you to have PowerCLI nightmares :smileygrin:
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
If I do not have the check for # we pull VMs that we "don't care" which storage system they're on.
Name Notes
---- -----
<vm name> Prod - Shared - FPT3 - Batch Server
<vm name> Prod - Shared - Production Windows Admin Node (Scripts)...
<vm name> Prod - xPass - FWi mgmt only/non-HA
<vm name> Prod - Message Center - CMR Drools
etc...
Just adding the ... $_.Notes.Contains(" #") -and.... Helps remove these VMs which is perfect.
No nightmares just ridiculously jealous of your powershell / powercli skills.