Looping from pipeline to add columns

by Feb 13, 2015

Hi guys,

Just learning powershell this last week and am trying to write some of my own scripts, one of the scripts I'm putting together is to find any remnant XP machines on the network and determine who is logging in to them.

I've tried the below to try and retrieve the known XP computers from AD and then list them along with the last logged on user but am having issues and am hoping you can fix up what's going wrong. I suspect it is to do with the bracketing in the foreach loop?

The end product I'd like to see is headed:
name, lastlogondate, operatingsystem, OperatingSystemVersion, OperatingSystemServicePack, username

 

1: This works well at pulling out the AD Computers that are XP

Get-ADComputer -filter{operatingsystem -like "Windows XP*"} -property * | select name, lastlogondate, operatingsystem, OperatingSystemVersion, OperatingSystemServicePack | Sort-Object lastlogondate -Descending | Select-Object -First 10 | Format-Table -AutoSize

2: I found this one online so am not familiar with it, but it works if I manually enter a computer name that I know is turned on:

gwmi -computername xxxxxx -class win32_computersystem -property username | select username

 

What I'd now like to do is combine the 2 so that I end up with the headings: name, lastlogondate, operatingsystem, OperatingSystemVersion, OperatingSystemServicePack, username

3: I tried simplifying it to start with by just displaying the last logged on user, and not yet worry about displaying this as another column, by piping the 2 commands and using a foreach object. This does not work.

Get-ADComputer -filter{operatingsystem -like "Windows XP*"} -property * | select name | sort name | foreach {gwmi -computername $_ -class win32_computersystem -property username}

4. I also tried creating a foreach enumerator with the below, but this also does not work

$computerlist = Get-ADComputer -filter{operatingsystem -like "Windows XP*"} -property * | select name | sort name
foreach($comp in $computerlist){
    gwmi -computername ($comp.name) -class win32_computersystem -property username | select username
}

Note, I also tried
$($comp.name)
$comp
in place of ($comp.name) in the loop.

I have also tried this with "-filter{operatingsystem -like "Windows 8*"}" (w/o quotes) in case it was an XP related thing, but it still doesn't work.

Your assistance would be greatly appreciated as this is driving me a bit crazy! Thanks!!

 

Errors:

For # 3 above I get the error:

gwmi : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At line:1 char:109
+ … ame | foreach {gwmi -computername $_ -class win32_computersystem -property usern …
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

 

For # 4 above I get the error:

gwmi : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At line:9 char:5
+     gwmi -computername ($comp.name) -class win32_computersystem -property userna …
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

 

PS, sorry, there's no text color option in the forum post editor, otherwise I would have made it easier to read