Custom PSObject Empty?

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of 9sH0W 9sH0W 1 month, 3 weeks ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #49010
    Profile photo of 9sH0W
    9sH0W
    Participant

    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
    Profile photo of Jonathan Warnken
    Jonathan Warnken
    Participant

    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 
    
    
    
    • This reply was modified 1 month, 3 weeks ago by Profile photo of Jonathan Warnken Jonathan Warnken. Reason: typo
    #49030
    Profile photo of 9sH0W
    9sH0W
    Participant

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

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.