Custom PSObject Empty?

Welcome Forums General PowerShell Q&A Custom PSObject Empty?

Viewing 2 reply threads
  • Author
    Posts
    • #49010
      Participant
      Topics: 1
      Replies: 1
      Points: 0
      Rank: Member

      Hello,

      I am trying to put together a simple script that tests connectivity to certain sites on ports 80/443, and during this process I am creating a custom psobject to store the results. Once this is working I would like to pipe the custom psobject to the ConvertTo-HTML to create a very simple report.

      The problem is that when I run this script the PSObject does not return any information. Does anyone have any idea what I am doing wrong? Thank you in advance for taking the time to look at this.

      
       
      $Host_IP = @{ 'google.com' = '216.58.195.238'; 'gmail.com' = '172.217.3.165'; }
      
      $Host_Port = ('80','443')
      $ConnectInformation = @()
      
      function Test-Port
      {
          Param(
              [parameter(ParameterSetName='IP', Position=0)]
              [System.Net.IPAddress]
              $IPAddress,
      
              [parameter(Mandatory=$true, Position=1)]
              [string]
              $FQDN
              )
      
              foreach ($Port in $Host_Port) { 
                  $testDNS = New-Object System.Net.Sockets.TcpClient;
                  Try
                  {
                      $RHost = "$FQDN`:$Port"
                      $testDNS.Connect($FQDN, $Port);
                      $RHostStatus = "Successful";
      
                  }
                  Catch
                  {
                      $RHostStatus = "Failed";
                  }
                  Finally
                  {
                      $NI = New-Object psobject
                      $NI | Add-Member -type NoteProperty -Name RemoteServer -Value $RHost
                      $NI | Add-Member -type NoteProperty -Name Connection -Value $RHostStatus  
                      $ConnectInformation += $NI
                      Remove-Variable NI
                      $testDNS.Dispose();
                      
                  }
                                                       
                  
              }
       }
      
      
       
      
      
      foreach ($IP in $Host_IP.GetEnumerator()) {
          Test-Port -FQDN $IP.Key -IPAddress $IP.Value
             
      }
      
      $ConnectInformation 
      
      
    • #49027
      Participant
      Topics: 1
      Replies: 169
      Points: 0
      Rank: Member

      let your function return the object rather then trying to append it to variable inside of the function.
      The issue is one of scoping. When you use the variable $ConnectInformation inside of the function it does not update the variable of the same name in the parent scope. see

       get-help about_scopes 
      $Host_IP = @{ 'google.com' = '216.58.195.238'; 'gmail.com' = '172.217.3.165'; }
      
      $Host_Port = ('80','443')
      $ConnectInformation = @()
      
      function Test-Port
      {
          Param(
              [parameter(ParameterSetName='IP', Position=0)]
              [System.Net.IPAddress]
              $IPAddress,
      
              [parameter(Mandatory=$true, Position=1)]
              [string]
              $FQDN
              )
      
              foreach ($Port in $Host_Port) { 
                  $testDNS = New-Object System.Net.Sockets.TcpClient;
                  Try
                  {
                      $RHost = "$FQDN`:$Port"
                      $testDNS.Connect($FQDN, $Port);
                      $RHostStatus = "Successful";
      
                  }
                  Catch
                  {
                      $RHostStatus = "Failed";
                  }
                  Finally
                  {
                      $NI = New-Object psobject
                      $NI | Add-Member -type NoteProperty -Name RemoteServer -Value $RHost
                      $NI | Add-Member -type NoteProperty -Name Connection -Value $RHostStatus  
                      $testDNS.Dispose()
                      $NI
                      
                  }
                                                       
                  
              }
       }
      
      
       
      
      
      foreach ($IP in $Host_IP.GetEnumerator()) {
         $ConnectInformation += Test-Port -FQDN $IP.Key -IPAddress $IP.Value
             
      }
      
      $ConnectInformation 
      
      
      
    • #49030
      Participant
      Topics: 1
      Replies: 1
      Points: 0
      Rank: Member

      That did the trick, thank you Jonathan. Scoping did not even cross my mind.

Viewing 2 reply threads
  • The topic ‘Custom PSObject Empty?’ is closed to new replies.