пятница, 5 февраля 2016 г.

test service presence  in computers from dhcp leases file
проверяем наличие сервиса на компах из лиза dhcp


clear
$ping = new-object System.Net.NetworkInformation.Ping
function Test-Host ($Name)
{
try
{
     if ($ping.send($Name).Status -eq "Success" ) { $True }
}
    catch{ $False }
}

$hashimported=@{}
import-csv c:\temp\dhcp-leases-18.01.16.csv | sort-object name | where {$_.name -like "COMP-*"} | %{$hashimported[$_.name]=$_.ip}

foreach($computer in $hashimported.keys|sort)
{
 if(test-host($computer))
 {
  try{
  $getservice=get-wmiobject -query "select * from win32_service where name like '%zservice%'" -computername $computer -ErrorAction Stop
  }
  catch {($computer)+" "+$error[0].exception.message}

  if(!$getservice)
    {
($computer)+" Сервис не обнаружен!"
    }
  }
  else
  {
  ($computer)+" PING не прошел!"
  }
 }