1

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 
}

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I think your suggestion of a Try/Catch block is an excellent suggestion for a solution to their problem. I wish you would have given an example of how they could have used that.
Note that for try/catch to work you need to make the error a terminating error (-ErrorAction Stop or $ErrorActionPreference = 'Stop'). Using -ErrorAction SilentlyContinue only makes sense if you want to hide the (non-terminating) error message and continue regardless.
@samuel-prout How do you get the colors in your code example?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.