Solved

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

Posted on 2014-04-20
14
263 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
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

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. 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

Suggested Solutions

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Synchronize a new Active Directory domain with an existing Office 365 tenant
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

777 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