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)
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0
with attribution required.
rev 2020.2.5.35980