[英]PowerCLI Get-VM output
我正在編寫腳本,但遇到了一段代碼的麻煩。 當我傳遞在VCenter中找到的所有計算機列表時,腳本會使用服務器列表和所有包含的信息正確填充$ result對象。 如果有任何錯誤(無法在VCenter中找到服務器),則返回的唯一內容是錯誤行(在出現多個錯誤的情況下,只有最后一個錯誤位於$ result中)。 有什么想法可以解決嗎?
我知道,如果我將Get-VM語句放在foreach循環中會起作用,但是一次將一台服務器傳遞到VCenter會花費很長時間。
try {
$operation = Get-VM -Name $computers -ErrorAction Stop | Restart-VMGuest -Confirm:$false
foreach ($comp in $operation) {
$result += [pscustomobject] @{
Server = $computer
Status = $True
Error = $False
ErrorMessage = $null
Stamp = Get-Date
}
}
}
catch {
$result += [pscustomobject] @{
Server = $computer
Status = $False
Error = $True
ErrorMessage = $_
Stamp = Get-Date
}
}
我認為try / catch處於錯誤位置。 也許您是否在foreach中將if / else語句放在這項工作中?
但是我不知道如何檢查$comp
是否存在error
...
$operation = Get-VM -Name $computers -ErrorAction Stop | Restart-VMGuest -Confirm:$false
foreach ($comp in $operation) {
If ($comp -eq "??error??") {
$result += [pscustomobject] @{
Server = $computer
Status = $True
Error = $False
ErrorMessage = $null
Stamp = Get-Date
}
}
Else {
$result += [pscustomobject] @{
Server = $computer
Status = $False
Error = $True
ErrorMessage = $_
Stamp = Get-Date
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.