none
Add remarks to script output RRS feed

  • Question

  • I created a PS script for my techs to check if various computers are offline, but since they do not always know the physical location of the computer based on the name alone, I would like to add a remark for each computer entry that will show in the output results to help them out. Is this possible? Below is my sample script:

    $servers = "TEST01", "TEST02", "TEST03"
    Foreach($s in $servers)
    
    {
    
    if(!(Test-Connection -Cn $s -BufferSize 16 -Count 1 -ea 0 -quiet))
    
    {
    
      write-host "$s OFFLINE" -foregroundcolor “red”
      
    } # end if
    
    ELSE {write-host "$s ONLINE" -foregroundcolor “green”}
    
    } # end foreach
    Read-Host -Prompt "Press Enter to exit"

    Currently, the results will display in a window as such:

    TEST01 ONLINE
    TEST02 ONLINE
    TEST03 OFFLINE
    Press Enter to exit:

    But I would prefer to see something like this:

    TEST01 ONLINE Location: Conference room 1
    TEST02 ONLINE Location: Demo area
    TEST03 OFFLINE Location: Reception area
    Press Enter to exit:

    Wednesday, February 17, 2016 10:25 PM

Answers

  • Close... "I want to find all computers from a list I create, ping them all, and write a console output only for the computers that do not respond to a ping, and I want to include the location property in the output". How to I modify your script to check my list rather than the AD?

    Here is one way:


    $computerList = Get-Content "ComputersToPing.txt"
    foreach ( $computerName in $computerList ) {
      if ( -not (Test-Connection $computerName -Count 1 -Quiet) ) {
        $location = (Get-ADComputer $computerName -properties Location).Location
        Write-Host -ForegroundColor Red ("{0} - Location '{1}' - OFFLINE" -f $computerName, $location)
      }
    }
    

    In this case, put the computers you want to ping in the ComputersToPing.txt file, one per line, no blank lines. This also presumes that the location attribute is set in AD for each of the computers.

    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by S Frank 312 Thursday, February 18, 2016 4:59 PM
    Thursday, February 18, 2016 4:33 PM
    Moderator

All replies

  • And the location data would come from where?

    -- Bill Stewart [Bill_Stewart]

    Wednesday, February 17, 2016 10:58 PM
    Moderator
  • Hi,

    so the question is: where is this Information...Do you have it in an Attribute in Active Directory?! If so, you can get it using:

    Get-ADComputer

    have a look here: https://technet.microsoft.com/en-us/library/ee617192.aspx?f=255&MSPPError=-2147217396

    Or should the Location be fixed in the script?!

    Regards

    Eric


    Microsoft MVP Cloud and Datacenter Management
    Microsoft Partner Technical Solutions Professional (P-TSP)
    --
    www.ericberg.de
    @ericberg_de
    --
    MCSE: Enterprise Devices and Apps | MCSE: Private Cloud | MCSE: Server Infrastructure | MCSE: Desktop Infrastructure

    Wednesday, February 17, 2016 11:02 PM
  • I agree with Bill 
    do you save the computer location info in AD? ( Location tab )
    If so please try this:

    $servers = "TEST01", "TEST02", "TEST03" Foreach($s in $servers) {

    $Location = (Get-ADComputer -Identity $s -Properties Location).Location if(!(Test-Connection -Cn $s -BufferSize 16 -Count 1 -ea 0 -quiet)) { write-host "$s OFFLINE" -foregroundcolor “red” } # end if ELSE {write-host "$s ONLINE Location: $Location" -foregroundcolor “green”} } # end foreach Read-Host -Prompt "Press Enter to exit"


    Regards




    • Proposed as answer by Alvaro Saenz Wednesday, February 17, 2016 11:22 PM
    • Edited by Alvaro Saenz Wednesday, February 17, 2016 11:23 PM
    Wednesday, February 17, 2016 11:03 PM
  • This worked for me:

    $Servers = "TEST01", "TEST02", "TEST03"
    ForEach($S In $Servers)
    {
        $Location = (Get-ADComputer -Identity $S -Properties location).location
        If(!(Test-Connection -Cn $S -BufferSize 16 -Count 1 -ea 0 -quiet))
        {
            Write-Host "$S OFFLINE Location: $Location" -ForegroundColor "red"
        }
        Else {Write-Host "$S ONLINE Location: $Location" -ForegroundColor "green"}
    }
    Read-Host -Prompt "Press Enter to exit"
    


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Wednesday, February 17, 2016 11:21 PM
    Moderator
  • The computers are in the AD, but I'm only trying to check the status on a list of about 10-20 systems, and those systems will change from time to time.
    Thursday, February 18, 2016 3:35 PM
  • Did you have any additional questions?

    -- Bill Stewart [Bill_Stewart]

    Thursday, February 18, 2016 3:50 PM
    Moderator
  • No, as I actually need to display the location for the computers that are offline, and this is pulling info from the AD for the computers that are online.

    The goal of my script is to allow my techs to see which systems are offline and where they are located (so they may be manually reset these systems). With my current script, they need to reference a database to see where each system is currently installed. Not a big deal, but time consuming.

    Thursday, February 18, 2016 3:58 PM
  • So your question is really this:

    "I want to find all computers in Active Directory, ping them all, and write a console output only for the computers that do not respond to a ping, and I want to include the location property in the output." Is that correct?

    Here's a script that does that:


    $params = @{
      "Filter" = { Name -like '*' }
      "Properties" = "Location"
    }
    Get-ADComputer @params | ForEach-Object {
      if ( -not (Test-Connection $_.Name -Count 1 -Quiet) ) {
        Write-Host -ForegroundColor Red ("{0} - Location '{1}' - OFFLINE" -f $_.Name, $_.Location)
      }
    }
    


    -- Bill Stewart [Bill_Stewart]



    Thursday, February 18, 2016 4:07 PM
    Moderator
  • Close... "I want to find all computers from a list I create, ping them all, and write a console output only for the computers that do not respond to a ping, and I want to include the location property in the output". How to I modify your script to check my list rather than the AD?

    There are probably 1500+ computers in my AD, and so there could easily be 200+ computers offline in my AD... So besides taking way too long, I don't care about most that are offline, which is why I am working from my own custom list rather than pinging the entire AD.

    Thursday, February 18, 2016 4:21 PM
  • I'm sorry, but the script I posted retrieves the location from AD whether the computer is online or offline. The only way the location will not be retrieved if is the location attribute of the computer is not populated, the computer was never joined to the domain, or no domain controller is available. I assume the AD modules are available.

    Certainly we can retrieve the value of the location attribute from AD even if the computer itself is unavailable.


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Thursday, February 18, 2016 4:26 PM
    Moderator
  • Close... "I want to find all computers from a list I create, ping them all, and write a console output only for the computers that do not respond to a ping, and I want to include the location property in the output". How to I modify your script to check my list rather than the AD?

    Here is one way:


    $computerList = Get-Content "ComputersToPing.txt"
    foreach ( $computerName in $computerList ) {
      if ( -not (Test-Connection $computerName -Count 1 -Quiet) ) {
        $location = (Get-ADComputer $computerName -properties Location).Location
        Write-Host -ForegroundColor Red ("{0} - Location '{1}' - OFFLINE" -f $computerName, $location)
      }
    }
    

    In this case, put the computers you want to ping in the ComputersToPing.txt file, one per line, no blank lines. This also presumes that the location attribute is set in AD for each of the computers.

    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by S Frank 312 Thursday, February 18, 2016 4:59 PM
    Thursday, February 18, 2016 4:33 PM
    Moderator
  • I merged your script with mine to fit my needs, and now it works beautifully! You're a rock star, thanks!

    Here is my final modified version:

    $servers = "TEST01", "TEST02", "TEST03"
    foreach($s in $servers) {
        if(!(Test-Connection -Cn $s -BufferSize 16 -Count 1 -ea 0 -quiet)) {
          $location = (Get-ADComputer $s -properties Location).Location
            Write-Host -ForegroundColor Red ("{0} - OFFLINE - Location '{1}'" -f $s, $location)
      } # end if
        ELSE {Write-Host -ForegroundColor Green ("{0} - ONLINE - Location '{1}'" -f $s, $location)}
        } # end foreach
    Read-Host -Prompt "Press Enter to exit"

    Thursday, February 18, 2016 4:59 PM