question

SourSnacks-0437 avatar image
0 Votes"
SourSnacks-0437 asked SourSnacks-0437 commented

PowerShell for Machines Offline

Looking for a PowerShell Script that can output result of all machines that are offline after a massive power outage. I found one script online, sample script below, that outputs all machines that are offline in red, however when trying to output the results to .csv they're all the same default black color. Can a column be added to this script that says "offline" next to the machines that are registering in red? If this isn't an option could a script be made to show this. I'll basically take this list and hand off to others so they can go around and power on these machines since we a number of employees not in the office.


Import-Module active*
$rtn = $null
Get-ADComputer -Filter * |
ForEach-Object {
$rtn = Test-Connection -CN $.dnshostname -Count 1 -BufferSize 16 -Quiet
IF($rtn -match ‘True’) {write-host -ForegroundColor green $
.dnshostname}
ELSE { Write-host -ForegroundColor red $_.dnshostname }
}

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered SourSnacks-0437 commented

Colorizing a CSV isn't possible.

Try this:

 Get-ADComputer -Filter * |
     ForEach-Object {
         if (Test-Connection -CN $_.dnshostname -Count 1 -BufferSize 16 -Quiet){
             [PSCustomObject]@{
                 "HostName" = $_.dnshostname
                 Status = "Online"
             }
         }
         else{
             [PSCustomObject]@{
                 "HostName" = $_.dnshostname
                 Status = "Offline"
             }
         }
     } | Export-CSV c:\junk\test.csv -NoTypeInformation

If you're only interested in the offline machines (which seems likely):

 Get-ADComputer -Filter * |
     ForEach-Object {
         if (-not (Test-Connection -CN $_.dnshostname -Count 1 -BufferSize 16 -Quiet)){
             [PSCustomObject]@{
                 "HostName" = $_.dnshostname
                 Status = "Offline"
             }
         }
     } | Export-CSV c:\junk\test.csv -NoTypeInformation
· 9
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You're 100% correct, I'm only interested in the machines that are "Offline" so the second script is perfect. Thank you.

0 Votes 0 ·

So I ran your second script and have machines listed as offline, but they're turned on in including my machine. I'll try the first one.

0 Votes 0 ·

I ran the first script and the .cvs was blank, and I received a fair number of the below same error.

Test-Connection : Parameter cannot be processed because the parameter name 'T' is ambiguous. Possible matches
include: -ThrottleLimit -TimeToLive.
At line:3 char:79
+ ... Test-Connection -CN $.dnshostname -Count 1 -BufferSize 16 -Quiet -T){
+ ~~
+ CategoryInfo : InvalidArgument: (:) [Test-Connection], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameter,Microsoft.PowerShell.Commands.TestConnectionCommand

0 Votes 0 ·

Remove that stray "-T" on line #3 right before the right-parenthesis-- it's a typo! I'll fix that now.

0 Votes 0 ·

Yeah I ran the first script again and it's showing a fair number, if not all, of our workstation with "Offline" that are actually turned on including mine.

0 Votes 0 ·
Show more comments