Link to home
Start Free TrialLog in
Avatar of bbimis
bbimis

asked on

powershell - show all computers that are running xp

i need to make a list of all the computers running xp on our domain. Can someone help me with that? I would like it to list the operation system and the user that is logged in.
Here is what I have so far:
$creds = Get-Credential
Get-Content c:\computerlist.txt | ForEach-Object {
 $pctype =  gwmi -computer $compname -credential $creds Win32_OperatingSystem | Format-List @{Expression={$_.Caption};Label="OS Name"},SerialNumber,OSArchitecture  | 
  gwmi -computer $compname -credential $creds Win32_ComputerSystem | Format-Table @{Expression={$_.Username};Label="Current User"} |  Out-File "xpmachines.txt"
  }

Open in new window



thanks!
SOLUTION
Avatar of PERSJWM
PERSJWM
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of footech
@PERSJWM - Please don't just post a blind link without a description of what will be found in it (even though the URL does provide some indication).

@bbimis - Using the AD attributes like in the link that PERSJWM posted is not a bad idea, but I have also seen some instances where these don't match the actual computer (typically a result of a computer account set up for one machine not being deleted when a new machine with the same name is joined to the domain).  As such, I don't trust it 100%.  However, it's going to be a lot faster than querying each machine separately.  You may want to combine both approaches to verify the data from both ends.

I fleshed out your script to get the info you already had a reference to.  Of course, querying a machine directly by name requires your DNS info to be up to date.
$creds = Get-Credential
Get-Content c:\computerlist.txt | ForEach-Object `
{
    $compname = $_
    If (Test-Connection $compname -Count 1 -Quiet)
    {
        gwmi -computer $compname -credential $creds Win32_OperatingSystem |
         Select @{Expression={$_.Caption};Label="OS Name"},SerialNumber,OSArchitecture | ForEach `
        {
            If ($_."OS Name" -match "xp")
            { $_ | Add-Member -MemberType NoteProperty -Name "Current User" -Value ((gwmi -computer $compname -credential $creds Win32_ComputerSystem).Username) -PassThru }
        }
    }
} |  Export-CSV "xpmachines.csv" -notype

Open in new window

@footech I think the link is self explanatory.  I'd have elaborated but when the link says "use-powershell-to-find-windows-xp-computers-still-alive-in-your-active-directory-domain", I figured it was a no-brainer.

My bad.
Avatar of bbimis
bbimis

ASKER

great just what i was looking for appreciate it!!!
Avatar of bbimis

ASKER

i keep getting this and also footech i asked for the mod to reopen cause i'd like to split the points if thats ok with you pers

i keep getting this error though:
Test-Connection : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Supply
an argument that is not null or empty and then try the command again.
You don't have to give me any points (though if you're using info I provided it'd be appreciated).  The only reason I see that you would get that error is if you have blank lines in your computerlist.txt file.
Also, it would probably be useful for the script if output included the computer name as one of the properties (could easily be added as a calculated property on line 8).
Avatar of bbimis

ASKER

footech
what is wrong with this here please
$creds = Get-Credential
Get-Content C:\homeoffice.txt | ForEach-Object `
{
    $compname = $_
    If (Test-Connection $compname -Count 1 -Quiet)
    {
        gwmi -computer $compname -credential $creds Win32_OperatingSystem |
         Select @{Expression={$_.Caption};Label="OS Name"} | ForEach `
        {
            If ($_."OS Name" -match "xp")
            { $_ | Add-Member -MemberType NoteProperty -Name "PC Name" -Value $compname  | Add-Member -MemberType NoteProperty -Name "Current User" -value (gwmi -computer $compname -credential $creds Win32_ComputerSystem | Format-Table @{Expression={$_.Username};Label="Current User"}) -PassThru
            }
        }
    }
} |  Export-CSV "xpmachines.csv" -notype

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bbimis

ASKER

I think this is a more fair split. Thanks again!