PnP
PnP

Reputation: 3185

Change from .Name property to String

Is this possible? I need to convert from System.DirectoryServices.PropertyValueCollection to a String.

i.e.

$ou = [ADSI]"LDAP://OU=Domain Controllers,DC=domain,DC=local"
foreach ($child in $ou.psbase.Children) {
    if ($child.ObjectCategory -like '*computer*') { 
        Write-Host $child.Name 
        if (Test-Connection -quiet $child.Name) {
            Test-Connection $child.name
            Invoke-GPUpdate $child.name
        }else{Write-Host "$child.Name is offline"}
     }

}

Upvotes: 1

Views: 1116

Answers (2)

CB.
CB.

Reputation: 60976

I believe your issue is here:

}else{Write-Host "$child.Name is offline"}

Try:

}else{Write-Host "$($child.Name) is offline"}

In a string variable's properties aren't expanded, you need to enclose it in $() notation.

Upvotes: 1

Joel Smith
Joel Smith

Reputation: 867

Like this: $child.Name.ToString()

Edit: I just tested in my lab and actually this works, not sure why exactly:

$child | %{$_.name}

Upvotes: 1

Related Questions