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.
Locked
User avatar
jplunkett
Posts: 6
Joined: Tue Dec 13, 2016 10:45 am

Cannot Display Remote Test-Netconnection results

Post by jplunkett » Wed Sep 19, 2018 12:58 pm

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: 5546
Joined: Thu Aug 18, 2011 4:56 am

Re: Cannot Display Remote Test-Netconnection results

Post by davidc » Wed Sep 19, 2018 2:02 pm

[TOPIC MOVED TO THE POWERSHELL GUIS FORUM BY MODERATOR]
David
SAPIEN Technologies, Inc.

User avatar
jvierra
Posts: 12962
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Cannot Display Remote Test-Netconnection results

Post by jvierra » Wed Sep 19, 2018 2:14 pm

You cannot assign external variables in an Invoke-Command. You can only return a value or object.

User avatar
jvierra
Posts: 12962
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Cannot Display Remote Test-Netconnection results

Post by jvierra » Wed Sep 19, 2018 5:02 pm

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 
			
		}
	}
	
}

User avatar
jvierra
Posts: 12962
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Cannot Display Remote Test-Netconnection results

Post by jvierra » 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.

User avatar
jplunkett
Posts: 6
Joined: Tue Dec 13, 2016 10:45 am

Re: Cannot Display Remote Test-Netconnection results

Post by jplunkett » Thu Sep 20, 2018 6:32 am

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!

Locked