0
我正在实施多个vCenter的VM审计(其中大约5个),报告只是每个VM的csv以及一些属性。虽然这个脚本会在一夜之间运行,但我发现花了大约5-6个小时才完成并希望提高其效率。我了解了工作流程,并认为在同一时间而不是逐个审核每个中心会更快。这比我预计在4个小时后完成的速度要慢。我注意到数据中有很多重复内容,我无法弄清楚为什么会这样;也许我关于工作流如何工作的想法是有缺陷的。我也在寻找提高代码效率的技巧。提前致谢。工作流VM审计中的重复项
的工作流程:
workflow test {
param([string[]]$vcenters, [string]$session, [string]$username, [string]$password)
foreach -parallel($vcenter in $vcenters){
$main = InlineScript{
Add-PSSnapin VMware.VimAutomation.Core
Connect-VIServer -Server $Using:vcenter -User $Using:username -Password $Using:password
$vms = Get-View -ViewType VirtualMachine -Property Name, Summary.Config.GuestFullName, Runtime.Powerstate, Config.Hardware.MemoryMB, Config.Hardware.NumCPU
ForEach($machine in $vms){
$vm = Get-VM -Server $Using:vcenter -Name $machine.Name -ErrorAction SilentlyContinue
$object = New-Object -Type PSObject -Property ([ordered]@{
Name = $machine.Name
GuestOS = $machine.Summary.Config.GuestFullName
PowerState = $machine.Runtime.PowerState
MemoryGB = ($machine.Config.Hardware.MemoryMB/1024)
CPU = $machine.Config.Hardware.NumCPU
VLAN=(Get-NetworkAdapter -VM $vm |Sort-Object NetworkName |Select -Unique -Expand NetworkName) -join '; '
})
$object| Export-Csv -Append “C:\TestReports\$($vcenter)_TestReport.csv” -NoTypeInformation
}
Disconnect-VIServer - $Using:vcenter -Confirm:$false
}
}
}
添加一些日志记录,看看发生了什么。有很多方法可以做到这一点,包括:Add-Content(记录到文件),Write-Verbose(记录到Verbose PowerShell流)等。 –