【问题标题】:Proper way to create pscustomobject to add to array创建 pscustomobject 以添加到数组的正确方法
【发布时间】:2015-05-11 22:36:58
【问题描述】:

使用以下方法/语法在 for 循环内创建/清除/初始化对象的正确方法是什么?使用这种方法可以提高性能。

$objComputer = [pscustomobject] @{}

我注意到如果 AD 中不存在其中一个系统并返回错误或 null,则数组中使用了前一个条目/对象中的信息。例如,Computer-03 是有效的,但 BLAH(不是)从 Computer-03 的上一个条目中提取。

function Get-ADComputers ($NameList) {
$arrayComputer = @();
foreach ($line in $NameList.Split("`r`n") | ? { $_ }) {
    $PCName = $line.Trim()
    $Computer = (Get-ADComputer -Identity $PCName -Properties *)
    $objComputer = [pscustomobject] @{
        Computer = $PCName
        Active = $Computer.Enabled
        Date = $Computer.PasswordLastSet
        DaysOld = (Get-DaysOld $Computer.PasswordLastSet)
        OU =  $Computer.DistinguishedName
    } 
    write-host $objComputer
    $arrayComputer += $objComputer
    $objComputer = $null;
} return $arrayComputer
}

结果

Computer    Active  Date             DaysOld
Computer-01 TRUE    4/12/2015 8:16  -29
Computer-02 TRUE    5/4/2015 7:11   -7
Computer-03 TRUE    4/20/2015 9:01  -21
BLAH        TRUE    4/20/2015 9:01  -21
Computer-03 TRUE    4/6/2015 8:14   -35
Computer-04 TRUE    5/9/2015 17:17  -1
Computer-05 TRUE    4/17/2015 12:04 -24

感谢您的帮助! :)

使用 Try Catch 编辑示例:

function Get-ADComputers ($NameList) {
$arrayComputer = @();
foreach ($line in $NameList.Split("`r`n") | ? { $_ }) {
    $Computer = $null
    $PCName = $line.Trim()
    try {
        $Computer = (Get-ADComputer -Identity $PCName -Properties *)
        $objComputer = [pscustomobject] @{
        Computer = $PCName
        Active = $Computer.Enabled
        Date = $Computer.PasswordLastSet
        DaysOld = (Get-DaysOld $Computer.PasswordLastSet)
        OU =  $Computer.DistinguishedName
        }
    } catch {
        $objComputer = [pscustomobject] @{
        Computer = "$PCName"
        Active = "Missing"
        Date = "N/A"
        DaysOld = "N/A"
        OU =  "N/A"
        }
    }
    write-host $objComputer
    $arrayComputer += $objComputer
    $objComputer = $null;
} return $arrayComputer 
}

【问题讨论】:

    标签: arrays loops powershell object


    【解决方案1】:

    如果对Get-ADComputer 的调用失败,$Computer 不会被清除。您需要以一种或另一种方式处理错误。一个简单的解决方案:

    $Computer = $null
    $Computer = (Get-ADComputer -Identity $PCName -Properties *)
    if(!$Computer) { #handle a missing computer 
        $objComputer = [pscustomobject] @{
            Computer = "Computer not found"
            Active = "FALSE"
            Date = "N/A"
            DaysOld = "N/A"
            OU =  "N/A"
        } 
    }
    

    您也可以使用 try/catch 块。如果你不是很在意,可以使用-ErrorAction SilentlyContinue

    【讨论】:

    • 我认为您对 Try/Catch 块的建议是解决他们问题的绝佳建议。我希望你能举例说明他们如何使用它。
    • 请注意,要使 try/catch 起作用,您需要将错误设置为 terminating error-ErrorAction Stop$ErrorActionPreference = 'Stop')。仅当您想隐藏(非终止)错误消息并继续进行时,使用 -ErrorAction SilentlyContinue 才有意义。
    • @samuel-pout 您如何获得代码示例中的颜色?
    猜你喜欢
    • 1970-01-01
    • 2019-11-08
    • 1970-01-01
    • 2023-03-07
    • 2016-02-02
    • 1970-01-01
    • 1970-01-01
    • 2015-12-15
    • 1970-01-01
    相关资源
    最近更新 更多