#create a function that accepts the string variable of servername
function Ping-Server {
Param([string]$server)
$pingresult = Get-WmiObject win32_pingstatus -f "address='$Server'"
if($pingresult.statuscode -eq 0) {$true} else {$false}
} # end of function ping server
Get-Content "c:\scripts\machines.txt" | % {
$servername = $_
$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
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) }
if ($nicPower.Enable -eq $true)
{
}
else
{
$servername | out-file -append c:\scripts\wolstatus.txt
$nicPower.Enable | out-file -append c:\scripts\wolstatus.txt
$nicPower.Enable = $true
$nicPower.psbase.Put()
#(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