Hi guys, i have three servers who are making some trouble in the last few weeks, and we are investigating the issues, my boss wants me to check the connectivity and open ports test to these servers, so i came out with a script that will do the tests for me using the names and the IP of the servers<\/p>\n
At first i write a script which is doing the tests that i want but it looks too complex for such easy tasks, and at the current time i’m learning Power Shell, so I know that i can make powershell to use two simple commands and use PSCustomobject to show me the properties that I want,the thing is that according the results i’m in the middle and the script is half functional, but also according the result, i’m doing something wrong because of a knowledge absens<\/p>\n
here is the code:<\/p>\n
(local host is because of obvious reasons i can’t write the name of the servers)<\/p>\n
$computers= \"localhost\"\n\n \n foreach ($pc in $computers){\n \n $test_connection = Test-Connection -ComputerName $pc\n $test_netconnection = Test-NetConnection $pc -Port 1433 \n\n Test-Connection -ComputerName $pc -Count 3 -ErrorAction Continue|ft\n Test-NetConnection -ComputerName $pc -ErrorAction Continue|ft\n [pscustomobject] @{\n LocalPC =$test_connection.PSComputerName;\n Destination =$test_connection.Remotecomputer\n 'Problematic-server' =$test_connection.IPV4Address\n Bytes =$test_connection.bytes\n Time =$test_connection.Time\n MyLocalIP =$test_netconnection.SourceAddress.IPv4Address\n RemotePort =$test_netconnection.RemoteAdress\n ResolvedAddresses =$test_netconnection.ResolvedAddresses\n }\n }\n \n<\/code><\/pre>\nhere are the results<\/p>\n
WARNING: TCP connect to (::1 : 1433) failed\nWARNING: TCP connect to (127.0.0.1 : 1433) failed\n\nSource Destination IPV4Address IPV6Address Bytes Time(ms) \n------ ----------- ----------- ----------- ----- -------- \nLEVL-01 localhost 127.0.0.1 ::1 32 0 \nLEVL-01 localhost 127.0.0.1 ::1 32 0 \nLEVL-01 localhost 127.0.0.1 ::1 32 0 \n\nComputerName RemotePort RemoteAddress PingSucceeded PingReplyDetails (RTT) TcpTestSucceeded \n------------ ---------- ------------- ------------- ---------------------- ---------------- \nlocalhost 0 ::1 True 0 ms False \n\nLocalPC : {LEVL-01, LEVL-01, LEVL-01, LEVL-01}\nDestination : {$null, $null, $null, $null}\nProblematic-server : {127.0.0.1, 127.0.0.1, 127.0.0.1, 127.0.0.1}\nBytes : {$null, $null, $null, $null}\nTime : {$null, $null, $null, $null}\nMyLocalIP : \nRemotePort : \nResolvedAddresses : {::1, 127.0.0.1}\n\n<\/code><\/pre>\nwhat i’ve tried:<\/p>\n
\n- \n
using FT<\/p>\n<\/li>\n
- \n
using Select object<\/p>\n<\/li>\n<\/ol>\n
i don’t understand why i’m getting the $null and why it’s showing me all these properties, and why near the “MyLocalPC” property i see my computer name so much times<\/p>\n
for general knowledge:<\/p>\n
here is my original code:<\/p>\n
Try {\n\n$computers=\"localhost\"\n\nwrite-host -BackgroundColor Yellow -ForegroundColor Black \" Initiating first test of two - ¿?Ping Test¿?\"\nwrite-host \"`n\"\nwrite-host \"#########################################\"\n#first Foreach Steatment\nforeach ($pc in $computers) {\n \n write-host -BackgroundColor White -ForegroundColor Blue \"Performing a ping test to ->$pc<-\" \n\n Test-Connection -computername $pc -Count 3 -ErrorAction stop|\n Select-Object PSComputerNAme,@{l='problematic-servers';e={$_.Address}},BufferSize|Format-Table -AutoSize\n \n write-host -BackgroundColor green -ForegroundColor Blue \"`nThe connection to ->$pc<- is OK :) !!!\"\n write-host \"`n\"\n }#end of first foreach\n\n #Second Foreach Steatment\n \n Write-Host -BackgroundColor Gray -ForegroundColor Cyan \"Initiating Second test of two - ¿?TelNet Test¿?\"\n write-host \"`n\"\n foreach ($pc in $computers) {\n \n write-host -BackgroundColor Blue -ForegroundColor White \"Performing a Telnet test to ->$pc<-\"\n\n Test-NetConnection -Port 1433 $pc -ErrorAction stop|\n Select-Object @{l='My Local IP Adress';e={$_.SourceAddress}},@{l='problematic-servers';e={$_.ComputerName}},RemoteAddress,RemotePort,TcpTestSucceeded|\n Format-Table -AutoSize\n\n write-host -BackgroundColor green -ForegroundColor Blue \"`nThe ports on ->$pc<- are opend :) !!!\"\n\n }#end of second foreach\n\n }#end of \"Try\"\n\ncatch { Write-Host \"Please check connection to ->$pc<- it's not responding\"} \n\npause\n<\/code><\/pre>","upvoteCount":2,"answerCount":16,"datePublished":"2018-04-05T11:08:24.000Z","author":{"@type":"Person","name":"levleiderman","url":"https://community.spiceworks.com/u/levleiderman"},"acceptedAnswer":{"@type":"Answer","text":"These two lines are creating redundancy and you don’t need them because you’re already storing them in a variable that you’re defining in the lines above. This is what’s spitting out the property list that you don’t want.<\/p>\n
#Test-Connection -ComputerName $pc -Count 1 -ErrorAction Continue\n #Test-NetConnection -ComputerName $pc -ErrorAction Continue\n<\/code><\/pre>\nComment them out by putting a # in front of them, like above. Re-run and you’ll see what I mean.<\/p>\n
Place the -Count 1 in the this line:<\/p>\n
$test_connection = Test-Connection -ComputerName $pc -Count 1\n<\/code><\/pre>\nTry this:<\/p>\n
$computers= \"localhost\"\n\n \n foreach ($pc in $computers){\n \n $test_connection = Test-Connection -ComputerName $pc **-Count 1**\n $test_netconnection = Test-NetConnection $pc -Port 1433 \n\n<b> #Test-Connection -ComputerName $pc -ErrorAction Continue\n #Test-NetConnection -ComputerName $pc -ErrorAction Continue</b>\n [pscustomobject] @{\n LocalPC =$test_connection.PSComputerName;\n 'Problematic-server' =$test_connection.Address\n Bytes =$test_connection.buffersize\n Time =$test_connection.ResponseTime\n MyPC =$test_netconnection.SourceAddress\n 'Tested-Server' =$test_netconnection.RemoteAddress\n RemotePort =$test_netconnection.RemotePort\n ResolvedAddresses =$test_netconnection.ResolvedAddresses\n }\n }\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2018-04-05T12:55:24.000Z","url":"https://community.spiceworks.com/t/understanding-pscustomobject/644495/8","author":{"@type":"Person","name":"tahin","url":"https://community.spiceworks.com/u/tahin"}},"suggestedAnswer":[{"@type":"Answer","text":"Hi guys, i have three servers who are making some trouble in the last few weeks, and we are investigating the issues, my boss wants me to check the connectivity and open ports test to these servers, so i came out with a script that will do the tests for me using the names and the IP of the servers<\/p>\n
At first i write a script which is doing the tests that i want but it looks too complex for such easy tasks, and at the current time i’m learning Power Shell, so I know that i can make powershell to use two simple commands and use PSCustomobject to show me the properties that I want,the thing is that according the results i’m in the middle and the script is half functional, but also according the result, i’m doing something wrong because of a knowledge absens<\/p>\n
here is the code:<\/p>\n
(local host is because of obvious reasons i can’t write the name of the servers)<\/p>\n
$computers= \"localhost\"\n\n \n foreach ($pc in $computers){\n \n $test_connection = Test-Connection -ComputerName $pc\n $test_netconnection = Test-NetConnection $pc -Port 1433 \n\n Test-Connection -ComputerName $pc -Count 3 -ErrorAction Continue|ft\n Test-NetConnection -ComputerName $pc -ErrorAction Continue|ft\n [pscustomobject] @{\n LocalPC =$test_connection.PSComputerName;\n Destination =$test_connection.Remotecomputer\n 'Problematic-server' =$test_connection.IPV4Address\n Bytes =$test_connection.bytes\n Time =$test_connection.Time\n MyLocalIP =$test_netconnection.SourceAddress.IPv4Address\n RemotePort =$test_netconnection.RemoteAdress\n ResolvedAddresses =$test_netconnection.ResolvedAddresses\n }\n }\n \n<\/code><\/pre>\nhere are the results<\/p>\n
WARNING: TCP connect to (::1 : 1433) failed\nWARNING: TCP connect to (127.0.0.1 : 1433) failed\n\nSource Destination IPV4Address IPV6Address Bytes Time(ms) \n------ ----------- ----------- ----------- ----- -------- \nLEVL-01 localhost 127.0.0.1 ::1 32 0 \nLEVL-01 localhost 127.0.0.1 ::1 32 0 \nLEVL-01 localhost 127.0.0.1 ::1 32 0 \n\nComputerName RemotePort RemoteAddress PingSucceeded PingReplyDetails (RTT) TcpTestSucceeded \n------------ ---------- ------------- ------------- ---------------------- ---------------- \nlocalhost 0 ::1 True 0 ms False \n\nLocalPC : {LEVL-01, LEVL-01, LEVL-01, LEVL-01}\nDestination : {$null, $null, $null, $null}\nProblematic-server : {127.0.0.1, 127.0.0.1, 127.0.0.1, 127.0.0.1}\nBytes : {$null, $null, $null, $null}\nTime : {$null, $null, $null, $null}\nMyLocalIP : \nRemotePort : \nResolvedAddresses : {::1, 127.0.0.1}\n\n<\/code><\/pre>\nwhat i’ve tried:<\/p>\n
\n- \n
using FT<\/p>\n<\/li>\n
- \n
using Select object<\/p>\n<\/li>\n<\/ol>\n
i don’t understand why i’m getting the $null and why it’s showing me all these properties, and why near the “MyLocalPC” property i see my computer name so much times<\/p>\n
for general knowledge:<\/p>\n
here is my original code:<\/p>\n
Try {\n\n$computers=\"localhost\"\n\nwrite-host -BackgroundColor Yellow -ForegroundColor Black \" Initiating first test of two - ¿?Ping Test¿?\"\nwrite-host \"`n\"\nwrite-host \"#########################################\"\n#first Foreach Steatment\nforeach ($pc in $computers) {\n \n write-host -BackgroundColor White -ForegroundColor Blue \"Performing a ping test to ->$pc<-\" \n\n Test-Connection -computername $pc -Count 3 -ErrorAction stop|\n Select-Object PSComputerNAme,@{l='problematic-servers';e={$_.Address}},BufferSize|Format-Table -AutoSize\n \n write-host -BackgroundColor green -ForegroundColor Blue \"`nThe connection to ->$pc<- is OK :) !!!\"\n write-host \"`n\"\n }#end of first foreach\n\n #Second Foreach Steatment\n \n Write-Host -BackgroundColor Gray -ForegroundColor Cyan \"Initiating Second test of two - ¿?TelNet Test¿?\"\n write-host \"`n\"\n foreach ($pc in $computers) {\n \n write-host -BackgroundColor Blue -ForegroundColor White \"Performing a Telnet test to ->$pc<-\"\n\n Test-NetConnection -Port 1433 $pc -ErrorAction stop|\n Select-Object @{l='My Local IP Adress';e={$_.SourceAddress}},@{l='problematic-servers';e={$_.ComputerName}},RemoteAddress,RemotePort,TcpTestSucceeded|\n Format-Table -AutoSize\n\n write-host -BackgroundColor green -ForegroundColor Blue \"`nThe ports on ->$pc<- are opend :) !!!\"\n\n }#end of second foreach\n\n }#end of \"Try\"\n\ncatch { Write-Host \"Please check connection to ->$pc<- it's not responding\"} \n\npause\n<\/code><\/pre>","upvoteCount":2,"datePublished":"2018-04-05T11:08:24.000Z","url":"https://community.spiceworks.com/t/understanding-pscustomobject/644495/1","author":{"@type":"Person","name":"levleiderman","url":"https://community.spiceworks.com/u/levleiderman"}},{"@type":"Answer","text":"Run these 2 commands against a remote system:<\/p>\n
$pc = '.'\nTest-Connection -ComputerName $pc -Count 1 | fl *\nTest-NetConnection -ComputerName $pc | fl *\n<\/code><\/pre>\nThey will gives you all of the properties returned. With the names and types. In case some of the properties have different names.<\/p>\n
Are you testing this in the powershell console or in the ISE?<\/p>","upvoteCount":0,"datePublished":"2018-04-05T11:25:36.000Z","url":"https://community.spiceworks.com/t/understanding-pscustomobject/644495/2","author":{"@type":"Person","name":"psophos","url":"https://community.spiceworks.com/u/psophos"}},{"@type":"Answer","text":"