Page 1 of 1

Cannot Display Remote Test-Netconnection results

Posted: Wed Sep 19, 2018 12:58 pm
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]

Re: Cannot Display Remote Test-Netconnection results

Posted: Wed Sep 19, 2018 2:02 pm
by davidc
[TOPIC MOVED TO THE POWERSHELL GUIS FORUM BY MODERATOR]

Re: Cannot Display Remote Test-Netconnection results

Posted: Wed Sep 19, 2018 2:14 pm
by jvierra
You cannot assign external variables in an Invoke-Command. You can only return a value or object.

Re: Cannot Display Remote Test-Netconnection results

Posted: Wed Sep 19, 2018 5:02 pm
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 
			
		}
	}
	
}

Re: Cannot Display Remote Test-Netconnection results

Posted: Wed Sep 19, 2018 5:10 pm
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.

Re: Cannot Display Remote Test-Netconnection results

Posted: Thu Sep 20, 2018 6:32 am
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!