A ping function to pre-pend to many powershell remote access scripts and WOL Enable script

in powershell •  8 years ago 

There are many times that you are running a remote access script and you dont want to waste trying to connect to a machine that is not reachable.

This function uses wmi to do a ping result so its not via native os PING. That way if this fails then you know that WMI is broken or denied as well which you will need to know.

Get server list

#create a function that accepts the string variable of servername
function Ping-Server {
Param([string]$server)

wmi call for ping

$pingresult = Get-WmiObject win32_pingstatus -f "address='$Server'"

boolean result. this is neat one thats simple. its saying if the status code is 0 then its true and you may continue

otherwise its false and there will be no result to process

if($pingresult.statuscode -eq 0) {$true} else {$false}
} # end of function ping server

this get-content is a quick way to turn the output of get-content into an array. once it sees the % then each item in the array is associated now with $_ as you can see with the line #$servername = $_

Get-Content "c:\scripts\machines.txt" | % {
$servername = $_

now we call the function above with the variable $_.

 $result = Ping-Server $_
 # here is where that boolean comes in handy. If you do a if( ) like below then you are saying. If its true then continue else if false then end
if($result){

your code would then process in here.
}

Here is a full script using it

Get server list

function Ping-Server {
Param([string]$server)
$pingresult = Get-WmiObject win32_pingstatus -f "address='$Server'"
if($pingresult.statuscode -eq 0) {$true} else {$false}
}

Get-Content "c:\scripts\audit\win7machines-appsense.txt" | % {
$servername = $_
$result = Ping-Server $_

if($result){

#find if netenabled

$nic= gwmi win32_networkadapter -filter "netenabled = 'true'" -cn $servername

$nicPower = gwmi MSPower_DeviceWakeEnable -Namespace root\wmi -cn $servername |

where {$_.instancename -match [regex]::escape($nic.PNPDeviceID) }

check to see if machine has enable power to true

if ($nicPower.Enable -eq $true)
{
}
else
{
$servername | out-file -append c:\scripts\wolstatus.txt
$nicPower.Enable | out-file -append c:\scripts\wolstatus.txt

this sets it so nic can power machine

$nicPower.Enable = $true

this applies the enable = true

$nicPower.psbase.Put()

this forces a reboot

#(gwmi win32_operatingsystem -cn $servername ).reboot()
$servername | out-file -append c:\scripts\wolenabled.txt
"enabled wol on machine" | out-file -append c:\scripts\wolenabled.txt

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Interesting
I will follow you to see your future posts!

Congratulations @salveterram! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 3 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!