none
Using IF (Test-Connection) across multiple servers RRS feed

  • Question

  • I have multiple servers and clients I am trying to run this script against. I need to check the status of services and App Pools to make sure they are online. However the snag I am hitting is some client have 6 of the same server while others only have 1 or 2. This leads me with unncessary commands when running Test-Connection if a client only has 2 servers. 

    What I am attempting to do is the following..

    Test-Connection server 1 - If finds the server run code and move to server 2

    Test Connection Server 2 - If finds the server run code and move to server 3

    Test Connection Server 3 - If does not find then skip servers 4 5 and 6

    It tends to hang on this section of the code due to the time it takes to check the servers. 

    Previously they were all just separate blocks but I tried adding them under each others ScriptsBlocks however it only finds the first server and stops. Any help would be greatly appreciated.

    #---------Mobility Server 1 ----------#
    IF (Test-Connection -BufferSize 32 -Count 1 -ComputerName $MM1 -Quiet) {
    Write-Host  -Foreground Green "Found Mobility Server $MM1"
    Get-Service -ComputerName $MM1 -DisplayName *ServiceName* | Select-Object -Property DisplayName,MachineName,Status | Sort-object status|format-table -auto
    Invoke-Command -ComputerName $MM1 -credential $credential -ScriptBlock {Import-Module WebAdministration
    $webapps = Get-WebApplication
    $list = @()
    foreach ($webapp in get-childitem IIS:\AppPools\)
    {
    $name = "IIS:\AppPools\" + $webapp.name
    $item = @{}
     
    $item.WebAppName = $webapp.name
    $item.State = (Get-WebAppPoolState -Name $webapp.name).Value
    $item.State = (Get-WebAppPoolState -Name $webapp.name).Value
    $item.UserIdentityType = $webapp.processModel.identityType
    $item.Username = $webapp.processModel.userName
    $obj = New-Object PSObject -Property $item
    $list += $obj}
    $list | Format-Table -a -Property "WebAppName", "State", "UserIdentityType", "Username" | Write-Output
    
    sleep 1
    #---------Mobility Server 2 ----------#
      IF (Test-Connection -BufferSize 32 -Count 1 -ComputerName $MM2 -Quiet) {
      Write-Host  -Foreground Green "Found Mobility Server $MM2"
      Get-Service -ComputerName $MM2 -DisplayName *ServiceName* | Select-Object -Property DisplayName,MachineName,Status | Sort-object status|format-table -auto
      Invoke-Command -ComputerName $MM2 -credential $credential -ScriptBlock {
      Import-Module WebAdministration
    $webapps = Get-WebApplication
    $list = @()
    foreach ($webapp in get-childitem IIS:\AppPools\)
    {
    $name = "IIS:\AppPools\" + $webapp.name
    $item = @{}
     
    $item.WebAppName = $webapp.name
    $item.State = (Get-WebAppPoolState -Name $webapp.name).Value
    $item.State = (Get-WebAppPoolState -Name $webapp.name).Value
    $item.UserIdentityType = $webapp.processModel.identityType
    $item.Username = $webapp.processModel.userName
    $obj = New-Object PSObject -Property $item
    $list += $obj}
    $list | Format-Table -a -Property "WebAppName", "State", "UserIdentityType", "Username" | Write-Output
    sleep 1
    #---------Mobility Server 3 ----------#
        IF (Test-Connection -BufferSize 32 -Count 1 -ComputerName $MM3 -Quiet) {
        Write-Host  -Foreground Green "Found Mobility Server $MM3"
        Get-Service -ComputerName $MM3 -DisplayName *ServiceName* | Select-Object -Property DisplayName,MachineName,Status | Sort-object status|format-table -auto
        Invoke-Command -ComputerName $MM3 -credential $credential -ScriptBlock 
        Import-Module WebAdministration
    $webapps = Get-WebApplication
    $list = @()
    foreach ($webapp in get-childitem IIS:\AppPools\)
    {
    $name = "IIS:\AppPools\" + $webapp.name
    $item = @{}
     
    $item.WebAppName = $webapp.name
    $item.State = (Get-WebAppPoolState -Name $webapp.name).Value
    $item.State = (Get-WebAppPoolState -Name $webapp.name).Value
    $item.UserIdentityType = $webapp.processModel.identityType
    $item.Username = $webapp.processModel.userName
    $obj = New-Object PSObject -Property $item
    $list += $obj}
    $list | Format-Table -a -Property "WebAppName", "State", "UserIdentityType", "Username" | Write-Output
    sleep 1
    #---------Mobility Server 4 ----------#
          IF (Test-Connection -BufferSize 32 -Count 1 -ComputerName $MM4 -Quiet) {
          Write-Host  -Foreground Green "Found Mobility Server $MM4"
          Get-Service -ComputerName $MM4 -DisplayName *ServiceName* | Select-Object -Property DisplayName,MachineName,Status | Sort-object status|format-table -auto
          Invoke-Command -ComputerName $MM4 -credential $credential -ScriptBlock {
          Import-Module WebAdministration
    $webapps = Get-WebApplication
    $list = @()
    foreach ($webapp in get-childitem IIS:\AppPools\)
    {
    $name = "IIS:\AppPools\" + $webapp.name
    $item = @{}
     
    $item.WebAppName = $webapp.name
    $item.State = (Get-WebAppPoolState -Name $webapp.name).Value
    $item.State = (Get-WebAppPoolState -Name $webapp.name).Value
    $item.UserIdentityType = $webapp.processModel.identityType
    $item.Username = $webapp.processModel.userName
    $obj = New-Object PSObject -Property $item
    $list += $obj}
    $list | Format-Table -a -Property "WebAppName", "State", "UserIdentityType", "Username" | Write-Output
    sleep 1
    #---------Mobility Server 5 ----------#
            IF (Test-Connection -BufferSize 32 -Count 1 -ComputerName $MM5 -Quiet) {
            Write-Host  -Foreground Green "Found Mobility Server $MM5"
            Get-Service -ComputerName $MM5 -DisplayName *ServiceName* | Select-Object -Property DisplayName,MachineName,Status | Sort-object status|format-table -auto
            Invoke-Command -ComputerName $MM5 -credential $credential -ScriptBlock {
            Import-Module WebAdministration
    $webapps = Get-WebApplication
    $list = @()
    foreach ($webapp in get-childitem IIS:\AppPools\)
    {
    $name = "IIS:\AppPools\" + $webapp.name
    $item = @{}
     
    $item.WebAppName = $webapp.name
    $item.State = (Get-WebAppPoolState -Name $webapp.name).Value
    $item.State = (Get-WebAppPoolState -Name $webapp.name).Value
    $item.UserIdentityType = $webapp.processModel.identityType
    $item.Username = $webapp.processModel.userName
    $obj = New-Object PSObject -Property $item
    $list += $obj}
    $list | Format-Table -a -Property "WebAppName", "State", "UserIdentityType", "Username" | Write-Output
    #---------Mobility Server 6 ----------#
              IF (Test-Connection -BufferSize 32 -Count 1 -ComputerName $MM6 -Quiet) {
              Write-Host  -Foreground Green "Found Mobility Server $MM6"
              Get-Service -ComputerName $MM6 -DisplayName *ServiceName* | Select-Object -Property DisplayName,MachineName,Status | Sort-object status|format-table -auto
              Invoke-Command -ComputerName $MM6 -credential $credential -ScriptBlock {
              Import-Module WebAdministration
    $webapps = Get-WebApplication
    $list = @()
    foreach ($webapp in get-childitem IIS:\AppPools\)
    {
    $name = "IIS:\AppPools\" + $webapp.name
    $item = @{}
     
    $item.WebAppName = $webapp.name
    $item.State = (Get-WebAppPoolState -Name $webapp.name).Value
    $item.State = (Get-WebAppPoolState -Name $webapp.name).Value
    $item.UserIdentityType = $webapp.processModel.identityType
    $item.Username = $webapp.processModel.userName
    $obj = New-Object PSObject -Property $item
    $list += $obj}
    $list | Format-Table -a -Property "WebAppName", "State", "UserIdentityType", "Username" | Write-Output}}}}}}}}}}}
    Else 
    {Write-Host -Foregroundcolor Red "Mobility not found"
    }
    
    #---------End of Mobility Server Blocks ----------#

    Thursday, July 18, 2019 11:27 PM

All replies

  • You have copied a piece of code numerous times and edited it.  This is a very poor way to write code. You can just run a loop once for each server.

    I recommend taking time to learn basic coding and how it works then learn basic PowerShell.

    You cannot alter the time it takes to query these servers. A failure takes longer than a success in most cases.

    Before trying complex code it will be necessary for you to learn enough coding and PowerShell to understand what you are doing.  Once you have sufficient technical skill with basic coding you will be able to understand how to make this work and how to ask a question that is clear.  As asked, your question is very vague.

    You cannot post 150+ lines of code and ask us to debug it for you.  You must learn basic coding, debugging and PowerShell.


    \_(ツ)_/


    Friday, July 19, 2019 12:21 AM
    Moderator
  • This is where you need to start then try to learn enough to understand why.

    $sb = {
        Get-Service -DisplayName *ServiceName* |
            Select-Object -Property DisplayName, MachineName, Status |
            Sort-object status |
            format-table -AutoSize
        
        Import-Module WebAdministration
        Get-childitem IIS:\AppPools |
            ForEach-Object{
                [pscustomobject]@{
                    WebAppName = $_.name
                    State = (Get-WebAppPoolState -Name $_.name).Value
                    UserIdentityType = $_.processModel.identityType
                    Username = $_.processModel.userName
                }
            }
    }
    
    $servers = $MM1,$MM2, $MM3, $MM4, $MM5, $MM6
    foreach($server in $servers){
        if(Test-Connection -Count 1 -ComputerName $server -Quiet) {
                Write-Host -ForegroundColor Green "Found Mobility Server $server"
                Invoke-Command -ScriptBlock $sb -ComputerName $server -credential $credential
        } else {
            Write-Host "Server not found $server" -Foregroundcolor Red 
        }
    }
        
    You will also have to learn how to implement error handling.


    \_(ツ)_/




    Friday, July 19, 2019 12:41 AM
    Moderator
  • I appreciate your response and wasn't trying to ask for debug 150+ lines of code just hoping someone could point me in the direction of what I'm doing wrong. 

    I'm 100% self taught by googling and I am not a coder for my profession so this is all new to me. I was only doing this to try and help others validate 500+ servers in a short amount of time.

    Now if I'm not mistaken the code you displayed will only do exactly what I previously had with each server being a test-connection. 


    $servers = $MM1,$MM2, $MM3, $MM4, $MM5, $MM6
    foreach($server in $servers){
        if(Test-Connection -Count 1 -ComputerName $server -Quiet) {
                Write-Host -ForegroundColor Green "Found Mobility Server $server"
                Invoke-Command -ScriptBlock $sb -ComputerName $server -credential $credential
        } else {
            Write-Host "Server not found $server" -Foregroundcolor Red 
        }
    }

    Will this not still send a test-connection to all 6 servers? I'm also in the process of working on error handling as I said I'm new to this so just learning as I read more.

    Thanks.


    Friday, July 19, 2019 1:30 PM
  • I recommend learning basic coding and PowerShell before trying to write scripts. If you cannot understand the answers or the code then we cannot be of much help.

    You  cannot learn PowerShell or any complex technology by "googling".  You must take formal steps to study a tutorial.

          Microsoft Virtual     Academy - Getting Started with Microsoft PowerShell

          PowerShell Documentation

          PowerShell Style   Guidelines


    \_(ツ)_/

    Friday, July 19, 2019 6:32 PM
    Moderator
  • This will be my guess:

    [scriptblock]$global:sb= {
        Import-Module WebAdministration
        $webapps = Get-WebApplication
        $list = @()
        foreach ($webapp in get-childitem IIS:\AppPools\){
            $name = "IIS:\AppPools\" + $webapp.name
            $item = @{}
            $item.WebAppName = $webapp.name
            $item.State = (Get-WebAppPoolState -Name $webapp.name).Value
            $item.State = (Get-WebAppPoolState -Name $webapp.name).Value
            $item.UserIdentityType = $webapp.processModel.identityType
            $item.Username = $webapp.processModel.userName
            $obj = New-Object PSObject -Property $item
            $list += $obj
        }
        $list | Format-Table -a -Property "WebAppName", "State", "UserIdentityType", "Username" 
    }
    
    function Get-ServerInfo{
        [CmdletBinding()]
        param(
            [Parameter(Mandatory=$true,Position=0)]$ServerName,
            [Parameter(Mandatory=$false,Position=1)]$credentials
        )
        process{
            if(Test-Connection -Count 1 -ComputerName $ServerName -Quiet) {
                Write-Host  -Foreground Green "Found Mobility Server ""$ServerName"""
                Get-Service -ComputerName $ServerName -DisplayName *ServiceName* | Select -Property DisplayName,MachineName,Status | Sort-object status
                Invoke-Command -ComputerName $ServerName -credential $credentials -ScriptBlock $global:sb
            }
            else{
                Write-Error "Computer is OFF $ServerName"
            }
        }
        end{
            sleep -Seconds 1
        }
    }
    
    $Global:Credentials= Get-Credential
    $MobilityServers=@("MM1","MM2","MM3","MM4")
    
    foreach($computer in $MobilityServers){
        Invoke-Command -ComputerName $computer -ScriptBlock { Get-ServerInfo } -ArgumentList $computer,$Global:Credentials
    }
    
    

    Friday, July 19, 2019 11:43 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Wednesday, July 31, 2019 7:20 AM
    Moderator