Rather simple test-netconnection script, but my $_.node can't be used to create output

Hi

I'm struggling with the output of the following script.

$liste=import-csv -useculture 'C:\01_ITK_TOOLS\sitescope\serverliste.csv'

$nodes=Convertto-Json $liste

 

$liste| ForEach-Object {

$err=Test-NetConnection -computername $_.node -port $_.port -InformationLevel Detailed

if ($err.TCPTestSucceeded -match 'False') { Write-EventLog -LogName 'Windows PowerShell' -Source Powershell -EventId 666 -Message "$_.node NICHT ERREICHBAR $_.comment" -EntryType Warning}}

The script purpose is to parse a file, iterate over the ip's and create an eventlog entry if the ip's port is not available.

The eventlog get's created, but not with the intended information. It looks like this...

The following information was included with the event:

 

@{node=server1; port=80; comment=lalalal}.node NICHT ERREICHBAR @{node=server1; port=80; comment=lalalal}.comment

I guess it's with the variable being used within the if statement, but I don't really find anything helpful. Any suggestions?!

the csv file looks like this

node;port;comment
ip;port;some blabla
another ip;another port ;some more blabla

  • As for this...

    "I guess it's with the variable being used within the if statement"

    Not really, there are a few things that seem a bit out of place, IMHO. Other will have their take as well, and this is a good thing, since there is always more than one way to do X or Y.

    Your file data, is not a conventional CSV file.

    node;port;comment

    ip;port;some blabla

    another ip;another port ;some more blabla

    note the semi-*** is not the default CSV delimiter, so, this import is not valid.
    any use after this would not be valid

    $liste = Import-Csv -UseCulture 'D:\temp\serverliste.csv'

        node;port;comment
        ip;port;some blabla
        another ip;another port ;some more blabla

    Also the way you were importing it is not proper JSON, on your convert

        $nodes = ConvertTo-Json $liste
       
        $nodes
        [
            {
                "node;port;comment":  "ip;port;some blabla"
            },
            {
                "node;port;comment":  "another ip;another port ;some more blabla"
            }
        ]

    So, modifying what you have here, here are my thoughts on things...
    You have to specify the semi-*** as a delimiter, the semi-*** is not the default

    $liste = Import-Csv -Delimiter ';' -Path 'D:\temp\serverliste.csv'

        node       port          comment        
        ----       ----          -------        
        ip         port          some blabla    
        another ip another port  some more blabla

    Again, why are you doing this, you are not using it any where in your post

    $nodes = ConvertTo-Json $liste

        $nodes
        [
            {
                "node":  "ip",
                "port":  "port",
                "comment":  "some blabla"
            },
            {
                "node":  "another ip",
                "port":  "another port ",
                "comment":  "some more blabla"
            }
        ]

    what you are showing here is navigating the CSV elements not the JSON string $nodes. So, why do the JSON thing at all

    $liste | ForEach-Object { $err = Test-NetConnection -ComputerName $_.node -Port $_.port -InformationLevel Detailed

    if ($err.TCPTestSucceeded -match 'False')

    {

    # You are not setting your variables to expand.

    # This is just a string

    # if ($err.TCPTestSucceeded -match 'False') { Write-EventLog -LogName 'Windows PowerShell' -Source Powershell -EventId 666 -Message "$_.node NICHT ERREICHBAR $_.comment" -EntryType Warning}}

    # Expanded variables

    if ($err.TCPTestSucceeded -match 'False')

    {Write-EventLog -LogName 'Windows PowerShell' -Source Powershell -EventId 666 -Message "$($_.node) NICHT ERREICHBAR $($_.comment)" -EntryType Warning}

    }

    }

    Always step through you code one step at a time to make sure you are getting what you'd expect and need.