Solved

powershell - show all computers that are running xp

Posted on 2014-04-07
10
656 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 40

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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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 40

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 40

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

828 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