Solved

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

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

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
powershell - out of memory issues on process.exe 4 50
Insert Powershell variable into XML 4 34
Powershell Increment url 3 27
Powershell knowledge 2 25
Utilizing an array to gracefully append to a list of EmailAddresses
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 …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

912 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

18 Experts available now in Live!

Get 1:1 Help Now