Hi guys, i'm new here.
I'm new to powershell (learning it only two months or something like that) and this is my 4th script
which is started in a very basic way, of two commands
1)test-connection
2)test-netconnection
After reading and asking a lot of questions , some people told me that my script have to much text and i can simplify it by using a PSCustomobject, what ended as a new problem, because i had problems with the output, so i asked more question, and finally got a solution that will do what my first script does (almost)
here is the first version of the script
$computers= "localhost" foreach ($pc in $computers){ $test_connection = Test-Connection -ComputerName $pc -Count 2
$test_netconnection = Test-NetConnection $pc -Port 1433
[pscustomobject] @{ LocalPC =$test_connection.PSComputerName; 'now testing server: ' =$test_netconnection.ComputerName Bytes =$test_connection.buffersize Time =$test_connection.ResponseTime RemotePort =$test_netconnection.RemotePort }|ft -AutoSize }
here is the output for this (if i set the "test-connection" count parameter to 1, i'm not getting this result):
LocalPC now testing server: Bytes Time RemotePort------- ------------------- ----- ---- ----------{LEVL-01, LEVL-01} localhost {32, 32} {0, 0} 1433
i didn't knew how to make it show only one line even if i use two tests of test-connection, i understand that powershell store it in Arrey, but i didn't knew what can i do with that, untill this one guy in some forum writed this script:
$computers="localhost"
foreach ($pc in $computers){ $test_netconnection = Test-NetConnection $pc -Port 1433
Test-Connection -ComputerName $pc -Count 2 | ForEach-Object { [pscustomobject] @{ LocalPC =$_.PSComputerName 'Tested-Server' =$test_netconnection.ComputerName Bytes =$_.buffersize Time =$_.ResponseTime RemotePort =$test_netconnection.RemotePort TcpTestSucceeded =$test_netconnection.TcpTestSucceeded
} } | ft -AutoSize #end of Customobject
}
i didn't knew that you use foreach-object like that, until now i've seen on youtube and CBT and some other instructions, a use of that in a some lines that lookssomething like that get-blahblah |foreach-object {blabhalbha}.
can some one explain me what he did here? he used this "$_" sign (i understand what it mean - foreach-object that pass through the pipeline) but how can it be that it's not showing the result as double.
PS: during the writing of the post, i've started to wonder, how can i add "if statement" to this kind of a script where do i add it? inside the pipeline? because if there is some connectivity problem i don't want the script to stop on one server, i need to make it check all the servers to know on which of them there is problem
Thanks a lot for your help.
Firstly, there really is no such thing as an advanced for each. It's just ForEach.
Now, what you do inside a ForEach can be many different things, simple to advanced, but that would be true for any loop type or code block.
This piece of code has been posted / asked on several forums.
Did you see this discussion on this forum here, to see if that helps clear it up for you?'powershell.org/forums/topic/pscustomobject-returrning-strange-out-put'
the thing is, that in each post they are explaining some part of the question, now after i've understood what is going on in that code, i need to add the "if - else" (i don't want to use the "try-catch") because i need it to write "can't connect" (or something like this) and continue the script without stopping, and i don't know how to implement the "if" here.
in the end, this script have to do two tests (or more of test-connection), show result which is look like the following example - no matter how much times the "test-connection" runs, if i add "-count 4" i need that it will show the test 4 times :
LocalPC Tested-Server Bytes Time RemotePort TcpTestSucceeded------- ------------- ----- ---- ---------- ----------------blah-22 localhost 32 0 1433 Falseblah-22 localhost 32 0 1433 False
like the regular test-connection behave
I am not sure I get what you are after. You can use if/then in a try/catch or try/catch in an if/then, etc.
How many times you'd want the responses to happen is of course defined by you and the conditional logic you put in play.
So, you are going to need to be more clear of your use case and the conditions you are trying to address. If you can provide the code block you have pulled together based on the currently posted examples and then a bulleted list of the logic you are after, it may be clearer as to what you are after.
Simply put, if you want the default TNC response logic then there of course is nothing o add to the command, if you are limiting the response to a given set, then of you' have to code for that.
This sounds like an exercise for you in trying to get the error handling correct based on give conditions. Yet, that could be just an assumption on my part.
from one side it is an exercise, because it's have to do something (it did it at the first version, but was to complicated - some one said that it was to much code for so simple task), from the other side, it's going to serve my IT department (and i dont want to run 6 ping commands and 6 test-netconnection commands)this is what i'm tring to achiv, do test-connection and test-netconnection to or three times and write if there is some problem with one of the servers (some thing like "the server is no ok") the script will test three servers in the end (2 times each server through server name and 2 times through ip adress)
this is the code after lots of tests today (for getting the if-else condition to work):
$foreach=foreach ($pc in $computers)
{ $test_netconnection = Test-NetConnection $pc -Port 1433 test-Connection -ComputerName $pc -Count 2 -Quiet | #start: Foreach-object ForEach-Object { #start pscustomobject [pscustomobject] @{ LocalPC =$_.PSComputerName 'Tested-Server' =$test_netconnection.ComputerName Bytes =$_.buffersize Time =$_.ResponseTime RemotePort =$test_netconnection.RemotePort TcpTestSucceeded =$test_netconnection.TcpTestSucceeded
} #end of CustomeObject
}#end of Foreach-Object
}#end of foreach loop
if(($foreach) -eq $true|ft)
{Write-Host "connection and ports are check for the server $pc and it's looks ok"}
else{Write-Host "ther is a problem with $PC"}
i need that the output will be similar to the output of the "test-connection" in table format
thanks alot for your help