Solved

powershell - show all computers that are running xp

Posted on 2014-04-07
10
642 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
10 Comments
 
LVL 1

Assisted Solution

by:PERSJWM
PERSJWM earned 150 total points
ID: 39984069
0
 
LVL 39

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
 

Author Comment

by:bbimis
ID: 39984226
great just what i was looking for appreciate it!!!
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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 39

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 39

Accepted Solution

by:
footech earned 350 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now