Link to home
Start Free TrialLog in
Avatar of MattyS82
MattyS82

asked on

Powershell to Get operating system version from a list of servers. How to mark not found servers

Hi,

From a list of server host names I am trying to get a list of the operating systems installed.I am using the following command in powershell;
 Get-Content "servers.txt" | Get-ADComputer -Properties Name, OperatingSystem
| Select Name, OperatingSystem > ServerOperatingSystems.txt

Open in new window

This works to an extent and populates the text file with servers it is able to locate. For the servers it's unable to locate or obtain the operating system I would like this to be marked with "Not found". At the moment, there is no mention of these servers in the output text file. Essentially something like this;

Server 1 Windows Server 2012
Server 2 Windows Server 2012
Server 3 "Not found"


Thanks in advance.
ASKER CERTIFIED SOLUTION
Avatar of Joe Klimis
Joe Klimis
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
if you have many servers this could take a while

make sure you are using Powershell V3 or about , you can do it this way too,

workflow get-OsVersion 
{
 param( [string[]]$Computers )
  foreach -parallel ($computer in $computers) 
  {
    if((Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
        {
        $OS = (Get-WmiObject -Computer $computer -Class Win32_OperatingSystem ).caption
		"$computer $os"
		}		
		Else { "$computer  - not responding or found" }
  }
}
get-OsVersion -computers (get-content "Servers.txt")

Open in new window

Avatar of MattyS82
MattyS82

ASKER

perfect ! Thanks
Great help thanks for that. Will keep this one handy for the future
Avatar of footech
You could avoid querying all the machines, and just use the information from AD.
Get-Content "servers.txt" | % `
{
  try {
    $name = $_
    Get-ADComputer $name -Properties Name, OperatingSystem -errorAction Stop | Select Name, OperatingSystem
  }
  catch {
    ""| Select @{n="Name";e={$name}},@{n="OperatingSystem";e={"Not Found"}}
  }
} | Out-File ServerOperatingSystems.txt

Open in new window