locked
Make computerlist of pc's which reply to ping? RRS feed

  • Question

  • Hi,

    I have this list of computers which I like to ping and create another list of computers with only those pc's which reply to that ping so I can execute a powershell command to it.

    This is what I have untill now

    $computers = get-content C:\computers.txt

    foreach ($Computer in $computers) {
            ping -n 1 -w 2 $Computer
            }

    Computers ho reply at first ping, wait only 2 millesconds should go in c:\computerswhoreplytopoing.txt

    Please advise.
    J.


    Jan Hoedt

    Friday, August 7, 2015 12:27 PM

Answers

  • This did the trick:


    $CollectionName = "Collection"
    $CollectionID = Get-CMDeviceCollection -Name $CollectionName | Select CollectionID
    $Computers = Get-CMDevice -CollectionId $CollectionID.CollectionID | Select -Expand Name
    
    foreach ($Computer in $Computers) {
        if (Test-Connection -ComputerName $Computer -Quiet) { invoke-command -ScriptBlock { Get-AppvClientPackage * | select -Property Name } -ComputerName $Computer -verbose}
    }



    Jan Hoedt

    • Marked as answer by janhoedt Friday, August 7, 2015 3:45 PM
    Friday, August 7, 2015 3:45 PM

All replies

  • Test connection would probably be a better idea  - 

    get-content C:\computers.txt | 
    ForEach-Object {
    
    if(Test-Connection -Count 1 -ComputerName $_ -quiet){
    
    <do stuff>
    
    }else{
    
    
    <do other stuff>
    
    
    }}
    


    [string](0..21 | % {([char][int](22+
    ("5275778524449275827587
    42505554247789249585").Substring(($_*2),2)))})`
    -replace " "

    LinkedIn:   

    Note: Posts are provided “AS IS” without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

    Friday, August 7, 2015 12:39 PM
  • Thanks!

    However, when I execute this

    foreach ($Computer in $Computers) {
        if (Test-Connection -ComputerName $Computer -Quiet) { invoke-command -ScriptBlock { Get-AppvClientPackage * | select -Property Name } -ComputerName $Computer -verbose}
    }

    I get "connecting to remote server remotecomputer1 failed with the following error message : WinRM cannot complete the operation."

    When I execute this:

    invoke-command -ScriptBlock { Get-AppvClientPackage * | select -Property Name } -ComputerName remotecomputer1
    there is no problem.

    Please advise.
    J.


    Jan Hoedt


    • Edited by janhoedt Friday, August 7, 2015 1:22 PM Update
    Friday, August 7, 2015 1:22 PM
  • What is in your text file? There's no reason it would work using a typed string and not work using a variable.

    [string](0..21 | % {([char][int](22+
    ("5275778524449275827587
    42505554247789249585").Substring(($_*2),2)))})`
    -replace " "

    LinkedIn:   

    Note: Posts are provided “AS IS” without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

    Friday, August 7, 2015 1:37 PM
  • This is what I'm using.

    $CollectionName = "SCCMCollection"
    $CollectionID = Get-CMDeviceCollection -Name $CollectionName | Select CollectionID
    $Computers = Get-CMDevice -CollectionId $CollectionID.CollectionID | Select -Expand Name
    
    foreach ($Computer in $Computers) {
        if (Test-Connection -ComputerName $Computer -Quiet) { invoke-command -ScriptBlock { Get-AppvClientPackage *x* | select -Property Name } -ComputerName $Computer -verbose}
    }

    Output of computers is this:

    LAB1
    LAB2
    LAB3



    Jan Hoedt

    Friday, August 7, 2015 1:56 PM
  • This did the trick:


    $CollectionName = "Collection"
    $CollectionID = Get-CMDeviceCollection -Name $CollectionName | Select CollectionID
    $Computers = Get-CMDevice -CollectionId $CollectionID.CollectionID | Select -Expand Name
    
    foreach ($Computer in $Computers) {
        if (Test-Connection -ComputerName $Computer -Quiet) { invoke-command -ScriptBlock { Get-AppvClientPackage * | select -Property Name } -ComputerName $Computer -verbose}
    }



    Jan Hoedt

    • Marked as answer by janhoedt Friday, August 7, 2015 3:45 PM
    Friday, August 7, 2015 3:45 PM