Bradley Smith
Bradley Smith

Reputation: 304

Proper way to create pscustomobject to add to array

What is the proper way to create / clear / initialize a object inside a for loop using the following method/syntax? There is a performance increases using this method.

$objComputer = [pscustomobject] @{}

I noticed if one of the systems does not exist in AD and returns an error or null the information from the previous entry/object is used in the array. For example Computer-03 is valid but BLAH (which is not) pulls from the previous entry of 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
}

RESULTS

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

Thank you for your help! :)

EDIT EXAMPLE WITH 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 
}

Upvotes: 1

Views: 9993

Answers (1)

Samuel Prout
Samuel Prout

Reputation: 665

If the call to Get-ADComputer fails, $Computer is not cleared out. You'll need to handle the error in one way or another. A simple solution:

$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"
    } 
}

You can also use a try/catch block. If you don't care all that much, you can use -ErrorAction SilentlyContinue.

Upvotes: 3

Related Questions