简体   繁体   English

PowerCLI Get-VM输出

[英]PowerCLI Get-VM output

I am working on a script, and I am having trouble with a section of code. 我正在编写脚本,但遇到了一段代码的麻烦。 When I pass in a list of computers that are all found in VCenter, the script populates the $result object correctly with a list of servers and all of the information included. 当我传递在VCenter中找到的所有计算机列表时,脚本会使用服务器列表和所有包含的信息正确填充$ result对象。 If there are any errors (unable to find server in VCenter) the only thing that is returned is the error line (in the case of multiple errors, only the last error is in $result). 如果有任何错误(无法在VCenter中找到服务器),则返回的唯一内容是错误行(在出现多个错误的情况下,只有最后一个错误位于$ result中)。 Any ideas what I can do to resolve this? 有什么想法可以解决吗?

I know that it will work if I enclose the Get-VM statement in a foreach loop, but passing one server at a time to the VCenter takes a very long time. 我知道,如果我将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
            }
        }

I think the try / catch is on the false position. 我认为try / catch处于错误位置。 Maybe if you put an if / else statement in the foreach this work? 也许您是否在foreach中将if / else语句放在这项工作中?
But I don't know how to check if $comp is en error ... 但是我不知道如何检查$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.

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