Purclot
Purclot

Reputation: 559

Add-Member to an object strange behaviour

I try to add a member "canonicalName" to an object. When I use a Statement:

$obj | Add-Member NoteProperty "CanonicalName" 
-value (Get-ADComputer $row.ServerName -Properties CanonicalName).CanonicalName

the column "canonicalName" doesn't appear at all. Using the Statement with a variable:

    $a = (Get-ADComputer $row.ServerName -Properties CanonicalName).CanonicalName 
$obj | Add-Member NoteProperty "CanonicalName" -value $a 

works perfectly.

Hint: some ServerNames I'm Looping through are no more in the Domain. the code:

$data = $dataSet.Tables[0] 
$ErrorActionPreference = "silentlycontinue"
$info = @()
foreach($row in $data)
{ 
    $obj = New-Object psobject
    $obj | Add-Member NoteProperty "ServerName" -Value $row.ServerName
    #$a = (Get-ADComputer $row.ServerName -Properties CanonicalName).CanonicalName 
    #$obj | Add-Member NoteProperty "CanonicalName" -value $a 
    $obj | Add-Member NoteProperty "CanonicalName" -value (Get-ADComputer $row.ServerName -Properties CanonicalName).CanonicalName


    $info += $obj    
}

$info | ft -AutoSize

I expect to get:

ServerName CanonicalName                                                             
---------- -------------                                                             
N3751022   xx.yy.de/Memberserver/SQL-Cluster/xx/n3887022                 
N3732022   xx.zz.de/Memberserver/yy/uu/zz/N3732022 

without the variable $a all what I get is (no column canonicalName):

ServerName
----------
N3751022  
N3732022 

I've tested the code in a different Domain (all the Server in the dataset are present in the Domain!) I get the proper result (two columns serverName and canonicalName), even if there is no value for a canonicalName for a given serverName.

Upvotes: 1

Views: 2212

Answers (2)

Deadrobot
Deadrobot

Reputation: 43

Here's how I would do it. Using the filter on Get-ADComputer will allow it to return null if there is no matching record instead of an error:

$data = $dataSet.Tables[0] 
$ErrorActionPreference = "silentlycontinue"
$info = @()

foreach ($Row in $Data) {
    $ServerName =  $Row.ServerName
    $obj = New-Object psobject
    $obj | Add-Member NoteProperty "ServerName" -Value $ServerName
    $CanonicalName = (Get-ADComputer -Filter {Name -like $ServerName} -Properties CanonicalName).CanonicalName
    if ($CanonicalName) {
        $obj | Add-Member NoteProperty "CanonicalName" -value $CanonicalName
    }
    else {
        $obj | Add-Member NoteProperty "CanonicalName" -value $null
    }
    $info += $obj 
}

$info | ft -AutoSize

Upvotes: 0

notjustme
notjustme

Reputation: 2494

Using try/catch might be a workaround for you...

Please note however, that the catch block used in this example is far from precise. Any exception from running Get-ADComputer will be treated the same, we simply just assume that no output from Get-ADComputer means that the computer isn't member of the domain.

$data = $dataSet.Tables[0] 
$ErrorActionPreference = "silentlycontinue"
$info = @()
foreach($row in $data)
{ 
    $obj = New-Object psobject
    $obj | Add-Member NoteProperty "ServerName" -Value $row.ServerName
    try {
            $obj | Add-Member NoteProperty "CanonicalName" -Value (Get-ADComputer $row.ServerName -Properties CanonicalName).CanonicalName
    } catch {
            $obj | Add-Member NoteProperty "CanonicalName" -Value $null
    }

    $info += $obj    
}

$info | ft -AutoSize

Upvotes: 1

Related Questions