Share via


Powershell Scripts: Check which computers are turned on

Question

Wednesday, July 26, 2017 12:44 PM

Hello to all,

I need to check which computers are still on, within my farm.

I used this script:

$computers = "pcxx1","pcxx2","pcxx3","pcxx4","pcxx5","pcxx6"
Foreach($c in $computers) {
IF (Test-Connection -BufferSize 32 -Count 1 -ComputerName $c -Quiet) {
        Write-Host "The remote computer " $c " is Online"
} Else {
        Write-Host "The remote computer " $c " is Offline"
}}

RESULT:
The remote computer pcxx1 is Online
The remote computer pcxx2 is Online
The remote computer pcxx3 is Offline
The remote computer pcxx4 is Online
The remote computer pcxx5 is Offline
The remote computer pcxx6 is Offline

That's how it works.

I must, however, import a .csv file containing all the computers and users assigned to them. With the "import-csv" command
The result is wrong .. all are offline:

The remote computer pcxx1 is offline
The remote computer pcxx2 is offline
The remote computer pcxx3 is offline
The remote computer pcxx4 is offline
The remote computer pcxx5 is offline
The remote computer pcxx6 is offline

Where am I wrong?

I also need to export the result to a .xml file

Target is that, for the truth, only see computers turned on .. of those offline I do not care much about it ..

Thanks for your help

All replies (8)

Wednesday, July 26, 2017 1:05 PM

Could you post all of the script and the csv headers ?(or columns you have)


Thursday, July 27, 2017 7:15 AM

Use the Test-Connection cmdlet to send a ping (icmp packet) to the remote computer.

If you specify the –Quiet parameter, it returns only True or False.

Test-Connection -BufferSize 32 -Count 1 -ComputerName 192.168.0.41 -Quiet

Learn how to use Windows PowerShell to query Active Directory for computers, ping for status, and display:
https://blogs.technet.microsoft.com/heyscriptingguy/2011/11/19/query-ad-for-computers-and-use-ping-to-determine-status/

Get computers ping status, IP, OS version, sp version, uptime and last boot gui: https://gallery.technet.microsoft.com/scriptcenter/Display-computers-status-c8ff289d

Is this thing on? Using the PowerShell Test-Connection CmdLet

Powershell script to detect when PC is turned on & Connected to domain
Hope this helps!

Solution for Active Directory auditing, monitoring and management.


Thursday, July 27, 2017 7:59 PM

I'm not a fan of those cmdlets.  Or this code formatting.

Function get-ping  {

  Param (
    [parameter(ValueFromPipeline)]
    [string]$hostname
  )

  Begin {
    $ping = New-Object System.Net.Networkinformation.ping
    $timeout = 500
  }

  Process {
    $ping.Send($hostname, $timeout) | select @{name='hostname'
      expression={$hostname}},address,status,roundtriptime
  }
}


PS C:\> echo c011,c012,c013 | get-ping

hostname Address         Status RoundtripTime
           
c011     198.18.188.10  Success             1
c012                   TimedOut             0
c013                   TimedOut             0

Thursday, July 27, 2017 8:59 PM

I think I might know what is happening. The command

Test-Connection -BufferSize 32 -Count 1 -ComputerName servername -Quiet

can only have two outputs (because of the -quiet), True, or False

The if statement is saying "if there is a result, do this". There is ALWAYS a result, it is true or false. You should probably change it to something like

$variable = Test-Connection -BufferSize 32 -Count 1 -ComputerName $c -Quiet

if($variable -eq "true")

{Write-Host "The remote computer " $c " is Online"}

else

{Write-Host "The remote computer " $c " is offline"}


Thursday, July 27, 2017 9:13 PM

All commands that return a Boolean are designed to be tested with an "if" or any logical expression.  That is how programming in any language works.

This construct:

$variable = Test-Connection -BufferSize 32 -Count 1 -ComputerName $c -Quiet
if($variable -eq "true")

is redundant and is prone to errors when the concept is used in other contexts.  We never need to test a Boolean against true/false.

if(Test-Connection -BufferSize 32 -Count 1 -ComputerName $c -Quiet){ ...

is how the "Test" CmdLets are designed to work.

The issue here is likely due to the CSV file and how it is being used.  Since we do not have that code it is not possible to speculate on what is happening.

\(ツ)_/


Friday, July 28, 2017 5:00 PM

All commands that return a Boolean are designed to be tested with an "if" or any logical expression.  That is how programming in any language works.

This construct:

$variable = Test-Connection -BufferSize 32 -Count 1 -ComputerName $c -Quiet
if($variable -eq "true")

is redundant and is prone to errors when the concept is used in other contexts.  We never need to test a Boolean against true/false.

if(Test-Connection -BufferSize 32 -Count 1 -ComputerName $c -Quiet){ ...

is how the "Test" CmdLets are designed to work.

The issue here is likely due to the CSV file and how it is being used.  Since we do not have that code it is not possible to speculate on what is happening.

\(ツ)_/

Oh yeah you're right, thanks. I'm a noob at this too.


Friday, July 28, 2017 5:06 PM

I recommend this: https://www.amazon.com/Windows-PowerShell-TFM-Jason-Helmick/dp/0982131461

Also search amazon for books.  There are many excellent ones.  I suggest reading and doing all examples and exercises in at least two of them up to at least the middle chapters. You will become a guru overnight.

\(ツ)_/


Friday, August 11, 2017 4:24 AM

Hi,
Just checking in to see if the information provided was helpful. Please let us know if you would like further assistance.

Best Regards,

Frank

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
If you have feedback for TechNet Subscriber Support, contact [email protected].