Solved

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

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

Expert Comment

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

Open in new window

0
 
LVL 28

Expert Comment

by:becraig
Comment Utility
Opps had an extra space:

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

Open in new window

0
 

Author Comment

by:nav2567
Comment Utility
works.  Thanks.

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

Thanks again.
0
 
LVL 28

Assisted Solution

by:becraig
becraig earned 200 total points
Comment Utility
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
Comment Utility
Excellent.  

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

Expert Comment

by:Narender Gakka
Comment Utility
0
 

Author Comment

by:nav2567
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 28

Expert Comment

by:becraig
Comment Utility
$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
Comment Utility
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
Comment Utility
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 28

Expert Comment

by:becraig
Comment Utility
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
Comment Utility
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 28

Expert Comment

by:becraig
Comment Utility
$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
Comment Utility
Thanks, everyone!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
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 …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

771 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

9 Experts available now in Live!

Get 1:1 Help Now