Follow sqlserver-dba.com

Subscribe to RSS feed  Follow @jackvamvas - Twitter

*Use the Comments section for questions

SQLServer-DBA.com Links

Dba_db2_button

dba-ninja.com

SQL Server DBA Jobs

How to check Operating System type with Powershell

01 June,2012 by Tom Collins

Question: I need to determine the Operating System type  of a Linux Server , before deciding a scripted reponse. Is there a quick test to determine if it’s Linux?

 Answer:The Test-Connection cmdlet sends Internet Control Message Protocol (ICMP) echo request packets ("pings") to one or more remote computers and returns the echo response replies. You can use this cmdlet to determine whether a particular computer can be contacted across an Internet Protocol (IP) network.

  You can use the parameters of Test-Connection to specify both the sending and receiving computers, to run the command as a background job, to set a timeout and number of pings, and to configure the connection and authentication.

Unlike the traditional "ping" command, Test-Connection returns a Win32_PingStatus object that you can investigate in Windows PowerShell, but you can use the Quiet parameter to force it to return only a Boolean value.

 

 

$ServerName = "127.0.0.1"
$TimeToLive = Test-Connection $ServerName -Count 1 | select -exp ResponseTimeToLive 
Switch($TimeToLive)
{{$_ -le 64} {"Linux"; break}
 {$_ -le 128} {"Windows"; break}
 {$_ -le 255} {"UNIX"; break}
}







---

If you want to access increased information such as :

name, dnshostname,operatingsystem,operatingsystemservicepack,ip4address,lastlogondate,logoncount

this one-liner  using the Powershell cmdlet get-adcomputer and Test-Connection will supply the information

 

get-adcomputer $ServerName -Property * | Select-Object name,
dnshostname,
operatingsystem,
operatingsystemservicepack,
ipv4address,
lastlogondate,
logoncount, 
@{label = "PingResults"; Expression = {Test-Connection $_.ipv4address}} |sort lastlogondate -Descending

 

 

 

--

Read More on Powershell Scripts, Powershell and CPU

Powershell Scripts for DBA - SQL Server DBA

Powershell - run script on all sql servers - SQL Server DBA

Number of physical processors with Powershell – number of cores ...


Author: Tom Collins (http://www.sqlserver-dba.com)


Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment on How to check Operating System type with Powershell


sqlserver-dba.com | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer