?
Solved

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

Posted on 2014-04-20
14
Medium Priority
?
268 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 29

Assisted Solution

by:becraig
becraig earned 800 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
 

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 1200 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 1200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

770 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