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?