Solved

write a PS script to query OS of a remote system.

Posted on 2014-04-20
14
265 Views
Last Modified: 2014-04-21
I need to write a PS script to read a server.txt (list of servers) file and return their OS.

I only know the way to do it locally is  (get-wmiobject win32_operatingsystem).name


Please advise.  

Thanks.
0
Comment
Question by:nav2567
  • 6
  • 5
  • 2
  • +1
14 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 40011921
gc serverlist.txt | % { gwmi -computer $_ win32 _operatingsystem | select -expa name}

Open in new window

0
 
LVL 29

Expert Comment

by:becraig
ID: 40011923
Opps had an extra space:

gc serverlist.txt | % { gwmi -computer $_ win32_operatingsystem | select -expa name}

Open in new window

0
 

Author Comment

by:nav2567
ID: 40011945
works.  Thanks.

Can you modify the line that displays also the system names next to each result - the OS?

Thanks again.
0
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
LVL 29

Assisted Solution

by:becraig
becraig earned 200 total points
ID: 40011963
Short answer, since you are already passing in the server name:

gc serverlist.txt | % {$os = gwmi -computer $_ win32_operatingsystem | select -expa name; write-host "$_ $os"}

Open in new window

0
 

Author Comment

by:nav2567
ID: 40012287
Excellent.  

How do I pipe the result to a csv or txt file?
0
 
LVL 8

Expert Comment

by:Narender Gakka
ID: 40012342
0
 

Author Comment

by:nav2567
ID: 40012556
I need to pipe the output to a csv or txt file.  BeCraig's script has already done the job of displaying the output.

Thanks.
0
 
LVL 29

Expert Comment

by:becraig
ID: 40012601
$details = @()
gc serverlist.txt | % {
$os = gwmi -computer $_ win32_operatingsystem | select -expa name
$details += "$_`t$os`n"
} 
write-host $details
$details | out-file serveros.txt

Open in new window



Save the above as a *.ps1 file and run:
E.g. script.ps1
0
 

Author Comment

by:nav2567
ID: 40012659
I have the output file I want.  But I just want to let you know I see a bunch of errors like the below:

Get-WmiObject : 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.
At C:\showos1.ps1:3 char:21
+ $os = gwmi -computer <<<<  $_ win32_operatingsystem | select -expa name
    + CategoryInfo          : InvalidData: (:) [Get-WmiObject], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetWmiObjectCommand
0
 
LVL 16

Accepted Solution

by:
cantoris earned 300 total points
ID: 40012676
Try this:
$List = "C:\ServerList.txt"
Get-Content $List | ForEach-Object {
    $PC = $_.ToUpper()
    Write-Host "Hostname = $PC ..."
    if (Test-Connection -ComputerName $PC -Count 1 -Quiet) {
        $Props = @{
            "ComputerName" = $PC;
            "OS" = (Get-WmiObject -ComputerName $PC -Class Win32_OperatingSystem).Caption
        }
        $obj = New-Object -TypeName PSObject -Property $Props
        Write-Output $obj
    }
} | Export-Csv -Path "c:\Output.csv" -NoTypeInformation
Write-Host "Finished!"

Open in new window


This quietly handles offline PCs without throwing errors and emits proper objects so you can pipe them to Export-Csv like here or pipe them to Out-Gridview or Format-Table or anything else.
0
 
LVL 29

Expert Comment

by:becraig
ID: 40012678
Your input file may have some empty carriage returns.

We can always trim the input to avoid this.
0
 
LVL 16

Assisted Solution

by:cantoris
cantoris earned 300 total points
ID: 40012691
This avoids problems from empty lines and errant spaces:

$List = "C:\ServerList.txt"
Get-Content $List | ForEach-Object {
    $PC = $_.ToUpper().Trim()
    if ($PC -ne "") {
                Write-Host "Hostname = $PC ..."
        if (Test-Connection -ComputerName $PC -Count 1 -Quiet) {
            $Props = @{
                "ComputerName" = $PC;
                "OS" = (Get-WmiObject -ComputerName $PC -Class Win32_OperatingSystem).Caption
            }
            $obj = New-Object -TypeName PSObject -Property $Props
            Write-Output $obj
        }
    }
} | Export-Csv -Path "c:\Output.csv" -NoTypeInformation
Write-Host "Finished!"

Open in new window

0
 
LVL 29

Expert Comment

by:becraig
ID: 40012709
$details = @()
gc serverlist.txt | % {
$server = $_.Trim()
if !(Test-Connection $server)
{}
else {
$os = gwmi -computer $server win32_operatingsystem | select -expa name
$details += "$server`t$os`n"
        }
                                  } 
write-host $details
$details | out-file serveros.txt

Open in new window

                                           
This cleans up your empty entries:
Also adds a ping test.
0
 

Author Closing Comment

by:nav2567
ID: 40013888
Thanks, everyone!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
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 antispam), the admini…

713 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