<# Author: Kulverstukas Website: http://9v.lt Date: 2016-04-05 Last update: nil Description: Wakes up computers if they are sleeping and shuts them off. #> # where to put stuff $logfile = "c:\remotePcShutdown_log.txt" # define computer names (with domain) and mac addresses $macs = @{ # computer class 1 "b00.comp.lt" = "AA-AA-AA-AA-AA-AA"; # computer class 2 "d00.comp.lt" = "BB-BB-BB-BB-BB-BB"; } $log = "" $udpClient = New-Object System.Net.Sockets.UdpClient $udpClient.Connect(([System.Net.IPAddress]::Broadcast), 7) foreach ($mac in $macs.GetEnumerator()) { # Write-Host "$($mac.Name).studentas.lsu.lt == $($mac.Value)" $macBytes = $mac.Value -split "[:-]" | ForEach-Object { [Byte] "0x$_" } [Byte[]] $magicPacket = (,0xFF * 6) + ($macBytes * 16) $udpClient.Send($magicPacket, $magicPacket.Length) | Out-Null Start-Sleep -s 5 if (Test-Connection -ComputerName $mac.Name -Count 5 -Quiet) { # Write-Host "Shutting down $($mac.Name)" $log = $log+"Shutting down $($mac.Name)`r`n" Stop-Computer -computer $mac.Name -force -asjob | Out-Null } else { # Write-Host "Computer $($mac.Name) is offline" } } $udpClient.Close() if ($log -ne "") { $today = Get-Date -Format "yyyy-MM-dd HH:mm" "----------- $($today) -----------" | Out-File -FilePath $logfile -Append -Force $log | Out-File -FilePath $logfile -Append -Force }