简体   繁体   English

批量快照PowerCLI脚本会降低性能

[英]Batch snapshot PowerCLI script slow performance

I've made a powershell script which will take in a delimited batch of VM IP addresses and take snapshots of each VM. 我已经制作了一个powershell脚本,它将使用一定数量的VM IP地址并为每个VM拍摄快照。

My issue is I am calling Get-VM every time for every VM, which obviously is very slow. 我的问题是我每次为每个VM调用Get-VM VM,这显然很慢。 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: 只需将Get-VM调用放在foreach之外,将其分配给变量,然后使用它即可:

$retrievedVMs = Get-VM
foreach($vm in $vms)
{
    $retrievedVMs | where {$_.Guest.IPAddress -match $vm}| New-Snapshot -Name "Patching" -Description (Get-Date)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM