none
Test-Netconnection - Source Address

    Question

  • I have a script that I am using Test-Netconnection, creating a PSObject and exporting to a CSV.

    From Test-NetConnection, I am grabbing ComputerName, RemoteAddress, SourceAddress, and TCPTestSucceeded.

    I am having problems with SourceAddress, in my CSV, I get this (all other variables are fine):

    MSFT_NetIPAddress (Name = ";:8?:8;@?8?B55;?55;55;", CreationClassName = "", SystemCreationClassName = "", SystemName = "")

    Code:

    $DomainControllers = (Get-ADDOmain).ReplicaDirectoryServers
    
    $Ports = "135","389","636","3268","3269","53","88","445"
    $ResultOutput = @()
    ForEach ($DC in $DomainControllers)
    	{
    		
    		Write-Host $DC -ForegroundColor Yellow
    		ForEach($Port in $Ports)
    		{
    			
    			$TestResult = $Null
    			$Result = $Null
    			$Result = Test-NetConnection -Port $Port -ComputerName $DC -WarningAction Si
    			
    			$TestResult = $Result.TcpTestSucceeded
    			
    			If ($TestResult -eq $True)
    				{
    					$ResultOutput+=New-Object -TypeName PSObject -Property ([ordered]@{
    					'Target'=$Result.ComputerName;
    					'RemoteAddress'=$Result.RemoteAddress;
    					'SourceAddress'=$Result.SourceAddress;
    					'Port'=$Result.RemotePort;
    					'Status'=$Result.tcpTestSucceeded
    					
    					})
    					
    				}
    			Else {
    
    					$ResultOutput+=New-Object -TypeName PSObject -Property ([ordered]@{
    					'Target'=$Result.ComputerName;
    					'RemoteAddress'=$Result.RemoteAddress;
    					'SourceAddress'=$Result.SourceAddress;
    					'Port'=$Result.RemotePort;
    					'Status'=$Result.tcpTestSucceeded
    					})
    					 
    				}
    				
    		}
    		
    		
    	
    	}
    Write-Output $ResultOutput
    $ResultOutput | Export-csv c:\temp\DomainControllerPortTest.csv

    Friday, May 24, 2019 6:53 PM

Answers

  • Yes that is correct.  The command returns an object. 

    What is your question?

    The object is synthetic and added by the command as a note property.

    PS D:\scripts> Test-NetConnection www.google.com | select sourceaddress |gm
    
    
       TypeName: Selected.TestNetConnectionResult
    
    Name          MemberType   Definition
    ----          ----------   ----------
    Equals        Method       bool Equals(System.Object obj)
    GetHashCode   Method       int GetHashCode()
    GetType       Method       type GetType()
    ToString      Method       string ToString()
    SourceAddress NoteProperty ciminstance SourceAddress=MSFT_NetIPAddress (Name = "?@:;DBmDm::?D@;?nDDm?@A55;;55;55;", CreationClassName = "", System...
    
    
    PS D:\scripts>
    PS D:\scripts> (Test-NetConnection www.google.com).sourceaddress
    
    
    IPAddress         : 2601:8c:c003:614d::c467
    InterfaceIndex    : 11
    InterfaceAlias    : Wi-Fi
    AddressFamily     : IPv6
    Type              : Unicast
    PrefixLength      : 128
    PrefixOrigin      : Dhcp
    SuffixOrigin      : Dhcp
    AddressState      : Preferred
    ValidLifetime     : 6.00:49:23
    PreferredLifetime : 6.00:49:23
    SkipAsSource      : False
    PolicyStore       : ActiveStore
    
    
    
    PS D:\scripts> (Test-NetConnection www.google.com).sourceaddress.GetType()
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     CimInstance                              System.Object
    
    
    PS D:\scripts>

    SiurceAddress is the adapter on the local machine.  It is only useful when you have a multi-homed system.



    \_(ツ)_/


    Friday, May 24, 2019 7:24 PM
    Moderator
  • Hi Chris,

    Please try to extract the property of SourceAddress and select the IP Address as below

    ($result.SourceAddress).IPAddress

    Regards
    Chetan

    Please remember to click "Mark as Answer" on the post if your question was answered appropriately. So, it can benefit all community members who are facing similar issues.
    Friday, May 24, 2019 7:27 PM

All replies

  • Yes that is correct.  The command returns an object. 

    What is your question?

    The object is synthetic and added by the command as a note property.

    PS D:\scripts> Test-NetConnection www.google.com | select sourceaddress |gm
    
    
       TypeName: Selected.TestNetConnectionResult
    
    Name          MemberType   Definition
    ----          ----------   ----------
    Equals        Method       bool Equals(System.Object obj)
    GetHashCode   Method       int GetHashCode()
    GetType       Method       type GetType()
    ToString      Method       string ToString()
    SourceAddress NoteProperty ciminstance SourceAddress=MSFT_NetIPAddress (Name = "?@:;DBmDm::?D@;?nDDm?@A55;;55;55;", CreationClassName = "", System...
    
    
    PS D:\scripts>
    PS D:\scripts> (Test-NetConnection www.google.com).sourceaddress
    
    
    IPAddress         : 2601:8c:c003:614d::c467
    InterfaceIndex    : 11
    InterfaceAlias    : Wi-Fi
    AddressFamily     : IPv6
    Type              : Unicast
    PrefixLength      : 128
    PrefixOrigin      : Dhcp
    SuffixOrigin      : Dhcp
    AddressState      : Preferred
    ValidLifetime     : 6.00:49:23
    PreferredLifetime : 6.00:49:23
    SkipAsSource      : False
    PolicyStore       : ActiveStore
    
    
    
    PS D:\scripts> (Test-NetConnection www.google.com).sourceaddress.GetType()
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     CimInstance                              System.Object
    
    
    PS D:\scripts>

    SiurceAddress is the adapter on the local machine.  It is only useful when you have a multi-homed system.



    \_(ツ)_/


    Friday, May 24, 2019 7:24 PM
    Moderator
  • Hi Chris,

    Please try to extract the property of SourceAddress and select the IP Address as below

    ($result.SourceAddress).IPAddress

    Regards
    Chetan

    Please remember to click "Mark as Answer" on the post if your question was answered appropriately. So, it can benefit all community members who are facing similar issues.
    Friday, May 24, 2019 7:27 PM
  • Here is a little basic guidance on how to write code with PowerShell.

    $Ports = 135, 389, 636, 3268, 3269, 53, 88, 445
    (Get-ADDOmain).ReplicaDirectoryServers |
        ForEach-Object{
            Write-Host $_ -ForegroundColor Yellow
            ForEach ($Port in $Ports) {
                $result = Test-NetConnection -Port $Port -ComputerName $DC
                [pscustomobject]@{
                        Target = $result.ComputerName
                        RemoteAddress = $result.RemoteAddress
                        SourceAddress = $result.SourceAddress
                        Port   = $result.RemotePort
                        Status = $result.tcpTestSucceeded
                    }        
            }
        } | 
    Tee-Object -Variable results | Export-csv c:\temp\DomainControllerPortTest.csv
    $results # output to console


    \_(ツ)_/




    Friday, May 24, 2019 7:34 PM
    Moderator
  • Hi Chris,

    Please try to extract the property of SourceAddress and select the IP Address as below

    ($result.SourceAddress).IPAddress

    Regards
    Chetan

    Please remember to click "Mark as Answer" on the post if your question was answered appropriately. So, it can benefit all community members who are facing similar issues.

    There us no need for parens here:

    $result.SourceAddress.IPAddress


    \_(ツ)_/

    Friday, May 24, 2019 8:08 PM
    Moderator
  • 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.

    Monday, May 27, 2019 6:11 AM
    Moderator
  • Thanks for the reply, TIL about Tee-Object.

    Chris

    Tuesday, May 28, 2019 1:29 PM
  • The reason I want the Source Address is that our Firewall team has a habit of, when updating FW Rules, ports get blocked.

    This way I can run the script and show them Source/Destination and what ports are being blocked.

    Chris

    Tuesday, May 28, 2019 1:31 PM