PowerShell - iterating over computer names in Active Directory -
i'm new powershell , i'm attempting write script query ad machine names, check ones responding , write output file. far have this:
$computers = get-adcomputer -filter {(name -like "pc*")} | select-object -property name foreach ($computer in $computers) { if((test-connection -cn $computer -buffersize 16 -count 1 -ea 0 -quiet)) { "machine $computer connected." | out-file "out.txt" -append } else { "machine $computer not connected." | out-file "out.txt" -append } #end if } #end foreach
what in output text file looks following:
... machine @{name=pc-0649} not connected. machine @{name=pc-1541} not connected. machine @{name=pc-1574} not connected. ...
i think problem lies select-object -property name
part of first line. running debugger, looks powershell formatting each iteration of $computer
include header line.
[dbg]: ps y:\>> $computer name ---- pc-0649
what's best way me strip out except pc-#### part in situation?
i think problem still have list of (truncated) computer objects in $computers
. verify doing $computers[0].gettype()
. if don't see string, it's not string. :) try instead:
$computers = get-adcomputer -filter {(name -like "pc*")} | select-object -expandproperty name
Comments
Post a Comment