Blog » By Jack Vamvas on SQL Server DBA
How to check Operating System type with Powershell
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 ResponseTimeToLiveSwitch($TimeToLive){{$_ -le 64} {"Linux"; break}{$_ -le 128} {"Windows"; break}{$_ -le 255} {"UNIX"; break}}