Copy to Clipboard
$OnlyThisComputer = "" # Use this to limit it to working on a single machine, say 'PC17', or make it blank to apply to all computers starting with 'PC'.
$CheckHostsFile = $true
$Computers = Get-ADComputer -Filter '*' | Select -ExpandProperty Name | Sort
ForEach ($COMPUTER in $Computers){
if ( $true ){
if( ( $OnlyThisComputer.length -lt 1 -or $COMPUTER -eq $OnlyThisComputer ) ){
if (Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet){
# Online
Write-Host "$COMPUTER is on"
if ( Test-Path "\\$($COMPUTER)\c$\Windows\System32\drivers\etc\hosts" ){
$HostsFileContent = Get-Content "\\$($COMPUTER)\c$\Windows\System32\drivers\etc\hosts"
ForEach ($line in $($HostsFileContent -split "`r`n")){
$This = $line.trim()
if ( $This.length -gt 0 -and $This.substring(0,1) -ne "#" ){
Write-host $This
}
}
Write-Host "`r`n"
}
}else{
# Offline
Write-Host "$COMPUTER is offline!"
}
}
}
}
|