0

I'm writing a script that writes telnet-outputs into a CSV-File. Here's the script:

#This script retrieves ampere figures from APC-PDUs based on telnet/TCP.
 
$TelnetServers = @("10.202.77.244","10.202.77.247","10.202.77.243","10.202.77.242","10.202.77.251","10.202.77.241","10.202.77.239","10.202.77.240","10.202.77.236","10.202.77.237","10.202.77.235","10.202.77.234","10.202.77.237","10.202.77.233","10.202.77.232","10.202.77.231")
$TelnetPort = "23"
$CsvFilePath = $PSCommandPath.substring(0, $PSCommandPath.length - 4) + ".csv"
$csvRow = (Get-Date -Format "dd.MM.yyyy HH:mm;")

Foreach($TelnetServer in $TelnetServers)
{
    Write-Output("Processing PDU with IP " + $TelnetServer)
    $tcpConnection = New-Object System.Net.Sockets.TcpClient($TelnetServer, $TelnetPort)
    $tcpStream = $tcpConnection.GetStream()
    $tcpReader = New-Object System.IO.StreamReader($tcpStream)
    $tcpWriter = New-Object System.IO.StreamWriter($tcpStream)

    Start-Sleep -Milliseconds 200
    $tcpWriter.WriteLine("apc") | Out-Null
    $tcpWriter.Flush()
    Start-Sleep -Milliseconds 200

    $tcpWriter.WriteLine("apc") | Out-Null
    $tcpWriter.Flush()
    Start-Sleep -Milliseconds 200

    $tcpWriter.WriteLine("Phreading all current") | Out-Null
    $tcpWriter.Flush()
    Start-Sleep -Milliseconds 200

    $searching = $true
    while ($searching) {
        $tcpWriter.WriteLine("")
        $tcpWriter.Flush()
        Start-Sleep -Milliseconds 200
        while ($tcpStream.DataAvailable)
        {
            $text = $tcpReader.ReadLine()
            if ($text.StartsWith("1: ") -or $text.StartsWith("2: ") -or $text.StartsWith("3: "))
            {
                if ($text.StartsWith("3: "))
                {
                    $searching = $false
                }
            
                $ampere = $text.Substring(3, $text.length - 5)
                $csvRow += ($ampere + ";") #| Export-CSV -Path C:\users\output.csv -Append
                Write-Output $ampere
            }
        }
    }

    $tcpWriter.WriteLine("exit")
    $tcpWriter.Flush()
    Start-Sleep -Milliseconds 200

    $tcpReader.Close()
    $tcpWriter.Close()
    $tcpConnection.Close()
}

Add-Content $CsvFilePath $csvRow

The Script itself works, but the output isn't really usable...the problem is, that all outputs are in one line. I'd like to have the output in multiple lines, best would be after every IP-Adress. Here's what I tried to do:

#This script retrieves ampere figures from APC-PDUs based on telnet/TCP.
 
$TelnetServers = @("10.202.77.244","10.202.77.247","10.202.77.243","10.202.77.242","10.202.77.251","10.202.77.241","10.202.77.239","10.202.77.240","10.202.77.236","10.202.77.237","10.202.77.235","10.202.77.234","10.202.77.237","10.202.77.233","10.202.77.232","10.202.77.231")
$TelnetPort = "23"
$CsvFilePath = $PSCommandPath.substring(0, $PSCommandPath.length - 4) + ".csv"
$csvRow = (Get-Date -Format "dd.MM.yyyy HH:mm;")

Foreach($TelnetServer in $TelnetServers)
{
    Write-Output("Processing PDU with IP " + $TelnetServer)
    $tcpConnection = New-Object System.Net.Sockets.TcpClient($TelnetServer, $TelnetPort)
    $tcpStream = $tcpConnection.GetStream()
    $tcpReader = New-Object System.IO.StreamReader($tcpStream)
    $tcpWriter = New-Object System.IO.StreamWriter($tcpStream)

    Start-Sleep -Milliseconds 200
    $tcpWriter.WriteLine("apc") | Out-Null
    $tcpWriter.Flush()
    Start-Sleep -Milliseconds 200

    $tcpWriter.WriteLine("apc") | Out-Null
    $tcpWriter.Flush()
    Start-Sleep -Milliseconds 200

    $tcpWriter.WriteLine("Phreading all current") | Out-Null
    $tcpWriter.Flush()
    Start-Sleep -Milliseconds 200

    $searching = $true
    while ($searching) {
        $tcpWriter.WriteLine("")
        $tcpWriter.Flush()
        Start-Sleep -Milliseconds 200
        while ($tcpStream.DataAvailable)
        {
            $text = $tcpReader.ReadLine()
            if ($text.StartsWith("1: ") -or $text.StartsWith("2: ") -or $text.StartsWith("3: "))
            {
                if ($text.StartsWith("3: "))
                {
                    $searching = $false
                }
            
                $ampere = $text.Substring(3, $text.length - 5)
                $csvRow += ($ampere + ";") | Export-CSV -Path C:\users\output.csv -Append
                Write-Output $ampere
            }
        }
    }

    $tcpWriter.WriteLine("exit")
    $tcpWriter.Flush()
    Start-Sleep -Milliseconds 200

    $tcpReader.Close()
    $tcpWriter.Close()
    $tcpConnection.Close()
}

#Add-Content $CsvFilePath $csvRow

But | Export-CSV -Path C:\users\output.csv -Append just gives me a bunch of random numbers...can you tell me guys what I'm doing wrong?

Tomalak
  • 332,285
  • 67
  • 532
  • 628
Gabe
  • 31
  • 1
  • 1
  • 4
  • Try adding `-NoTypeInformation` to your export-csv command – Doug Maurer Oct 13 '20 at 06:46
  • Nope, unfortunately still just a bunch "4"'s for some reason... – Gabe Oct 13 '20 at 06:48
  • Does changing `$csvRow = (Get-Date -Format "dd.MM.yyyy HH:mm;")` to `$csvRow = ,(Get-Date -Format "dd.MM.yyyy HH:mm;")` solve your problem (added a comma before `(Get-Date` if it's hard to spot)? – notjustme Oct 13 '20 at 06:50
  • Unfortunately still just a bunch of 4's... – Gabe Oct 13 '20 at 07:12
  • My suggestion was for the new line problem. As for the "random numbers", I have no idea what's in your output even to begin with and it seems like a separate question. – notjustme Oct 13 '20 at 07:13
  • Yeah..im not really sure about it, because it works fine with "Add-Content" just in one line. What do you mean as the suggestion for the new line problem, would I need to add the variable somewhere else? – Gabe Oct 13 '20 at 07:21
  • No, I suggested a tiny little change to that line of code. – notjustme Oct 13 '20 at 07:24
  • Use `Add-Content` *inside* the loop, once per line. Newlines are added automatically by this cmdlet. Don't use `Export-CSV` here, your data *already is* CSV, you're building it manually after all. Other than that, if the data you read from that TCP socket contains unwanted characters, that's a separate issue. – Tomalak Oct 13 '20 at 07:51
  • Yes, @Tomalak's suggestion is also valid. Or move the Export-Csv outside the loop and keep building the array with `$csvRow += `, but you'd need to make sure $csvRow is an array that you add your rows to. This current mix of manually building a csv as well as relying on `Export-Csv` to write a proper csv file is a mess. – notjustme Oct 13 '20 at 08:17

0 Answers0