none
How can I turn this output into a variable RRS feed

  • Question

  • I have a script to query all the servers in AD

    Import-Module active*
    $rtn = $null
    Get-ADComputer -Filter {operatingsystem -like "*server*"} |
    ForEach-Object {
      $rtn = Test-Connection -CN $_.dnshostname -Count 1 -BufferSize 16 -Quiet
      IF($rtn -match 'True') {write-host -ForegroundColor green $_.dnshostname}

    I want to make the output a variable

    Easy way would be:

    $Servers = Get-ADComputer -Filter {operatingsystem -like "*server*"}

    Problem is this would waist time waiting for queries against disabled/powered off machines to error out

    I don't know how to use if statements in variables




    • Edited by mackhand Friday, July 18, 2014 5:52 PM
    Friday, July 18, 2014 5:47 PM

Answers

  • Hi,

    You can do something like this:

    $aliveServers = @()
    
    Get-ADComputer -Filter "OperatingSystem -like '*Server*'" | ForEach {
    
        If (Test-Connection -ComputerName $_.Name -Count 1 -Quiet) {
    
            $aliveServers += $_
    
        }
    
    }
    
    $aliveServers | Sort Name


    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    Friday, July 18, 2014 5:53 PM
  • Another possibility:

    $LiveServers = 
    Get-ADComputer -Filter "OperatingSystem -like '*Server*'" | 
     Where { Test-Connection -ComputerName $_.Name -Count 1 -Quiet } 


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    • Proposed as answer by jrv Friday, July 18, 2014 7:17 PM
    • Marked as answer by ScriptingWifeModerator Tuesday, August 12, 2014 12:12 AM
    Friday, July 18, 2014 6:00 PM
    Moderator

All replies