Kawaljeet Singh
Kawaljeet Singh

Reputation: 63

how to get a variable value in select command and redirected to a file in powershell

I am bit new in powershell and experiencing one problem and description is as follows: I am having one file in which there are server names i want to check for connection with port number and also want to redirect output to a file (i want to append data). If connection is successful or unsuccessful i want to write output to file. I am using below code to do that and there is one customized variable which i also want to write in file.


 $server = Get-Content ('C:\my\dir\srv.txt')

 Foreach ($s in $server)
 
 {  $result = (Test-NetConnection -Port 443 -ComputerName $s).TcpTestSucceeded

        If(!$Result){
                $Remark = "TCP connect to computer Successful"
                Test-NetConnection -Port 443 -ComputerName $s | select TcpTestSucceeded,ComputerName,RemoteAddress,$Remark| Out-File status.txt

            }

            Else{ $Remark= "WARNING: TCP connect to computer either failed or timeout"

                Test-NetConnection -Port 443 -ComputerName $s | select TcpTestSucceeded,ComputerName,RemoteAddress,$Remark | Out-File status.txt    

            }

       }

out file status.txt should be like this. I don't mind capturing this information in two different files as well, one for true and one for false TcpTestSucceeded

TcpTestSucceeded ComputerName RemoteAddress Remark 
---------------- ------------ ------------- ------------
            True VAFIC81-123   172.0.0.64  TCP connect to computer Successful
            False VAFIC81-678   172.0.0.64  WARNING: TCP connect to computer either failed or timeout

Upvotes: 1

Views: 2231

Answers (2)

Theo
Theo

Reputation: 61168

There should be no reason to execute Test-NetConnection multiple times like this. Once per server should be enough.

$server = Get-Content 'C:\my\dir\srv.txt'

$tcpTest = foreach ($s in $server) {
    # maybe you would also not wish to see warnings.
    # in that case add '-WarningAction SilentlyContinue' aswell
    $result = Test-NetConnection -Port 443 -ComputerName $s -ErrorAction SilentlyContinue
    $remark = if ($result.TcpTestSucceeded -eq $true) {
                "TCP connect to computer Successful"
              }
              else {
                "WARNING: TCP connect to computer either failed or timeout"
              }
    # output as object with added property 'Remark' to be collected in variable '$tcpTest'
    $result | Select-Object TcpTestSucceeded,ComputerName,RemoteAddress,
                            @{Name = 'Remark'; Expression = {$remark}}
}

# output on screen
$tcpTest | Format-Table -AutoSize

# output to CSV file
$tcpTest | Export-Csv -Path 'C:\my\dir\status.csv' -UseCulture -NoTypeInformation

Upvotes: 3

wasif
wasif

Reputation: 15488

Use a calculated property for this:

$server = Get-Content ('C:\my\dir\srv.txt')

 Foreach ($s in $server)
 
 {  $result = (Test-NetConnection -Port 443 -ComputerName $s).TcpTestSucceeded

        If(!$Result){
                $Remark = "TCP connect to computer Successful"
                Test-NetConnection -Port 443 -ComputerName $s | select TcpTestSucceeded,ComputerName,RemoteAddress,@{n="Remark";e={$Remark}} | Out-File status.txt

            }

            Else{ $Remark= "WARNING: TCP connect to computer either failed or timeout"

                Test-NetConnection -Port 443 -ComputerName $s | select TcpTestSucceeded,ComputerName,RemoteAddress,@{n="Remark";e={$Remark}} | Out-File status.txt    

            }

       }

A calculated property creates a custom property in Select-Object. It uses a hashtable where n is the header and e is the Expression.

Upvotes: 1

Related Questions