[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

powershell - show all computers that are running xp

Posted on 2014-04-07
10
Medium Priority
?
679 Views
Last Modified: 2014-04-08
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!
0
Comment
Question by:bbimis
  • 4
  • 3
  • 2
9 Comments
 
LVL 1

Assisted Solution

by:PERSJWM
PERSJWM earned 600 total points
ID: 39984069
0
 
LVL 41

Expert Comment

by:footech
ID: 39984206
@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

0
 
LVL 1

Expert Comment

by:PERSJWM
ID: 39984219
@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.
0
SMB Security Just Got a Layer Stronger

WatchGuard acquires Percipient Networks to extend protection to the DNS layer, further increasing the value of Total Security Suite.  Learn more about what this means for you and how you can improve your security with WatchGuard today!

 

Author Comment

by:bbimis
ID: 39984226
great just what i was looking for appreciate it!!!
0
 

Author Comment

by:bbimis
ID: 39984316
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.
0
 
LVL 41

Expert Comment

by:footech
ID: 39984410
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).
0
 

Author Comment

by:bbimis
ID: 39984413
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

0
 
LVL 41

Accepted Solution

by:
footech earned 1400 total points
ID: 39984609
The problems are on line 11.
Add-Member doesn't produce any output unless you include the -passthru parameter so it must be used if you're ever trying to pipe to another command.
Format-Table doesn't provide the right kind of object for what we're doing (creating a new property for an existing object), we need a string object.  Using Select-Object with the -expandproperty parameter would be an option if there was always something in the username property from the gwmi command, but dot-referencing the property name works better here.
I would do something more like this.
$creds = Get-Credential
Get-Content complist.txt | ForEach-Object `
{
    $compname = $_
    If (Test-Connection $compname -Count 1 -Quiet)
    {
        gwmi -computer $compname -credential $creds Win32_OperatingSystem |
         Select @{Expression={$_.Caption};Label="OS Name"}, @{Label="PC Name";Expression={$compname}} | 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

0
 

Author Closing Comment

by:bbimis
ID: 39985909
I think this is a more fair split. Thanks again!
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
After a recent Outlook migration from a 2007 to 2010 environment, some issues with Distribution List owners were realized. In this article, I explain how that was rectified.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline
Suggested Courses

608 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question