Cannot Display Remote Test-Netconnection results

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
jplunkett
Posts: 6
Last visit: Thu Sep 20, 2018 1:35 pm

Cannot Display Remote Test-Netconnection results

Post by jplunkett »

So I'm trying to return the results of an Invoke-Command -Scriptblock {Test-Netconnection} into a Label and I keep running into problems.

I know it's a little tricky returning things back through an invoke command, but what my main goal is to show the results of the Test-NetConnection in either a textbox or a label field in a gui.

Below is what my code looks like right now.

Any input would be awesome! Thanks!

Code: Select all

[Codebox=powershell file=Untitled.ps1]$Submit_Click = {
	
	foreach ($server in $serverlist.Lines)
	{
		
		Invoke-Command -ComputerName $server -Credential $username.Text -ScriptBlock {
			$date = (Get-Date -format g)
			$file = "$env:windir\System32\drivers\etc\hosts"
			$host1 = $using:HostIP1.Text + "`t" + $using:HostName1.Text
			$status = $using:statuslabel.Text
			
			Add-Content -Value "`n`n#### The Following Entries Were Entered By:$env:USERNAME on $date #####" -Path $file
			Add-Content -Value $Host1 -Path $file
			Add-Content -Value "####################################################################" -Path $file
			
			$statuslabel.Text = Test-NetConnection -ComputerName $HostName1.Text 
			
		}
	}
	
}[/Codebox]
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Cannot Display Remote Test-Netconnection results

Post by davidc »

[TOPIC MOVED TO THE POWERSHELL GUIS FORUM BY MODERATOR]
David
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Cannot Display Remote Test-Netconnection results

Post by jvierra »

You cannot assign external variables in an Invoke-Command. You can only return a value or object.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Cannot Display Remote Test-Netconnection results

Post by jvierra »

Here is how to get your output into a form:

Code: Select all

$Submit_Click = {
	
	foreach ($server in $serverlist.Lines){
		
		$statuslabel.Text = Invoke-Command -ComputerName $server -Credential $username.Text -ScriptBlock {
			$date = Get-Date -format g
			$file = "$env:windir\System32\drivers\etc\hosts"
			$host1 = $using:HostIP1.Text + "`t" + $using:HostName1.Text
			$status = $using:statuslabel.Text
			
			Add-Content -Value "`n`n#### The Following Entries Were Entered By:$env:USERNAME on $date #####" -Path $file
			Add-Content -Value $Host1 -Path $file
			Add-Content -Value "####################################################################" -Path $file
			
			Test-NetConnection -ComputerName $using:HostName1.Text 
			
		}
	}
	
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Cannot Display Remote Test-Netconnection results

Post by jvierra »

Here is a much easier way of initiating a remote ping.

$statuslabel.Text = Test-Connection -ComputerName $targetComputer -Source $sourceComputer

This causes "Source" to ping "ComputerName". You do not need to use a remote session and the result can be directly assigned to a forms control.
Currently you are outputting to a file in the Windows protected folders. Users and applications are not allowed to do this as those folders are owned by the OS. Also all of your files are stored on remote systems which is not very efficient.
User avatar
jplunkett
Posts: 6
Last visit: Thu Sep 20, 2018 1:35 pm

Re: Cannot Display Remote Test-Netconnection results

Post by jplunkett »

jvierra wrote: Wed Sep 19, 2018 5:10 pm Here is a much easier way of initiating a remote ping.

$statuslabel.Text = Test-Connection -ComputerName $targetComputer -Source $sourceComputer

This causes "Source" to ping "ComputerName". You do not need to use a remote session and the result can be directly assigned to a forms control.
Currently you are outputting to a file in the Windows protected folders. Users and applications are not allowed to do this as those folders are owned by the OS. Also all of your files are stored on remote systems which is not very efficient.
That's exactly what I was looking for! I didn't know there was a -Source parameter. I must have missed that. THank you!
This topic is 5 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked