locked
Adding to a Variable from a loop RRS feed

  • Question

  • Hello,

    I am trying to figure out how to add to a variable from a loop in a string without overwriting itself.

    Example...

    $computers = Get-Content "Z:\servers.txt"
    foreach ($computers in $computers)
    {
       Write-Host "Computer: ", $computers
       Test-Connection -BufferSize 16 -Count 1 -ComputerName $computers -Quiet
    Write-Host " " }

    The above is just an example but illustrates the issue.

    The script above checks what servers are up with a True or False statement printed on the screen.  Of course, I can send this to a text file, but what I would like to know how to do, is create a new variable with just the servers that are up. 

     

    So if I have servers A, B, C, D in my text file, and after running the Loop only A and D are up, I would like a new variable to have only A and D in it which I can then use that new variable in the same, but much larger script to only address the servers that are  up.

     

    Any ideas?

    Friday, December 29, 2017 11:44 PM

Answers

  • Store the computer name that passed the test in a variable
    End of the loop, you will have the array of pingable computers

    $computers = Get-Content "Z:\servers.txt"
    $PingableComputers = @()
    foreach ($computer in $computers)
    {
        Write-Host "Computer: ", $computer -NoNewline
        if(Test-Connection -BufferSize 16 -Count 1 -ComputerName "$computer" -Quiet)
        {
            Write-Host " $true" -ForegroundColor Green
            $PingableComputers += $computer
        }
        else
        {
            Write-Host " $false" -ForegroundColor Red
        }
    }
    
    $PingableComputers
    



    sên

    • Proposed as answer by PRASOON KARUNAN V Sunday, December 31, 2017 9:02 AM
    • Marked as answer by LosingSleep Friday, January 12, 2018 12:03 AM
    Saturday, December 30, 2017 1:33 AM

All replies

  • What is this supposed to be?

    foreach ($computers in $computers)<o:p></o:p>

    That won't work. You cannot use the same variable twice.

    help about_foreach


    \_(ツ)_/

    Friday, December 29, 2017 11:52 PM
  • Store the computer name that passed the test in a variable
    End of the loop, you will have the array of pingable computers

    $computers = Get-Content "Z:\servers.txt"
    $PingableComputers = @()
    foreach ($computer in $computers)
    {
        Write-Host "Computer: ", $computer -NoNewline
        if(Test-Connection -BufferSize 16 -Count 1 -ComputerName "$computer" -Quiet)
        {
            Write-Host " $true" -ForegroundColor Green
            $PingableComputers += $computer
        }
        else
        {
            Write-Host " $false" -ForegroundColor Red
        }
    }
    
    $PingableComputers
    



    sên

    • Proposed as answer by PRASOON KARUNAN V Sunday, December 31, 2017 9:02 AM
    • Marked as answer by LosingSleep Friday, January 12, 2018 12:03 AM
    Saturday, December 30, 2017 1:33 AM
  • One of the first things you should learn about PowerShell is that you should not quote everything that doesn't move.  Superstition has no place in the computer world.

    See:

    Write-Host $true -ForegroundColor Green

    Also:

    $computers = Get-Content Z:\servers.txt
    foreach ($computer in $computers) {
        if(Test-Connection $computer -Quiet){
            Write-Host "Success: $computer"
        }else{
            Write-Host "Failed: $computer"
        }
    }
    Quotes are used to expand variables inside of strings.


    \_(ツ)_/


    • Edited by jrv Saturday, December 30, 2017 1:51 AM
    Saturday, December 30, 2017 1:50 AM
  • That works. THank you very much!
    Saturday, December 30, 2017 9:00 PM
  • Hi,

    I'm checking how the issue is going, was your issue resolved?

    And if the replies as above are helpful, we would appreciate you to mark them as answers, and if you resolve it using your own solution, please share your experience and solution here. It will be greatly helpful to others who have the same question.

    Appreciate for your feedback.

    Best Regards,
    Albert

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Monday, January 1, 2018 6:37 AM
  • Yes, the above worked perfectly, and thanks for the advice.

    Friday, January 12, 2018 12:02 AM