I've made a powershell script which will take in a delimited batch of VM IP addresses and take snapshots of each VM.
My issue is I am calling Get-VM
every time for every VM, which obviously is very slow. I'm wondering if anyone can see another way to perform the same action without having to call this every time?
Add-PSSnapin VMware.VimAutomation.Core
$VCServer = "vc"
Connect-VIServer $VCServer
[array]$vms = (Read-Host “List of IP's (separate with comma)”).split(“,”) | %{$_.trim()}
foreach($vm in $vms)
{
Get-VM | where {$_.Guest.IPAddress -match $vm}| New-Snapshot -Name "Patching" -Description (Get-Date)
}
Just put the Get-VM
invoke outside the foreach, assign it to a variable and use it instead:
$retrievedVMs = Get-VM
foreach($vm in $vms)
{
$retrievedVMs | where {$_.Guest.IPAddress -match $vm}| New-Snapshot -Name "Patching" -Description (Get-Date)
}