Solved

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

Posted on 2014-11-03
5
3,388 Views
Last Modified: 2014-11-03
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.
0
Comment
Question by:MattyS82
[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
  • 2
  • 2
5 Comments
 
LVL 10

Accepted Solution

by:
Joe Klimis earned 500 total points
ID: 40419180
Hi

Try the following and let me know how you get on

Regards
Joe

foreach ($computer in get-content Servers.txt) 
{
        if((Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
        {
        $OS = (Get-WmiObject -Computer $computer -Class Win32_OperatingSystem ).caption
		Write-output "$computer - $os"
		}		
	
		Else { write-output "$computer  - not responding or found" }
}

Open in new window

0
 
LVL 10

Expert Comment

by:Joe Klimis
ID: 40419207
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

0
 
LVL 1

Author Comment

by:MattyS82
ID: 40419232
perfect ! Thanks
0
 
LVL 1

Author Closing Comment

by:MattyS82
ID: 40419235
Great help thanks for that. Will keep this one handy for the future
0
 
LVL 40

Expert Comment

by:footech
ID: 40420464
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

0

Featured Post

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!

Question has a verified solution.

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

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

696 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