Need Help With PowerShell GUI App Using PowerShell Studio 2014

I made the skeleton of the GUI and all looks great and now I just have to get some code in. I have a multiline text box where users can type in multiple computer names and a Run box. I have the output going to a GridView using the Out-GridView cmdlet.

For testing purposes, I am just running test-connection to see if the output looks ok and it does when I enter one computer, BUT, when I enter multiple computers it sees it as one big computer name and it errors out saying that “Test1ComputerTest2ComputerTest3Computer is offline”.

How do I get PowerShell Studio to see each computer on each line as a separate computer in a multiline text box without it seeing it all as one string?

Thanks

Add a backtick-n (backtick, followed by lowercase letter N, in double quotes) to the end of each line. That’s a “newline” character.

Thanks Don, I am aware of that method, but do I add that in the multiline textbook of the GUI?

That would look a little tacky, if that is the case.

Is there any other way to do this within the code itself?

Here is my Run button’s code:

$RunButton_Click = {
	#TODO: Place custom script here
	
	$NewLine = "`r`n"
	
	$TestConnection = @(
	Foreach ($ComputerName in $ComputerNamesTextBox.Text)
	{
		If ((Test-Connection -ComputerName $ComputerNamesTextBox.Text -Count 1 -Quiet) -eq $true)
		{
			$NewLine
			
			$ComputerName = $ComputerName.ToUpper()
			
			Write-Output "$ComputerName is online!"
			
			$NewLine
		}
		
		Else
		{
			$NewLine
			
			Write-Warning -Message "$ComputerName is offline!"
			
			$NewLine
		}
	}
	)
	
	$TestConnection | Out-GridView

You could argue that a GUI based application trying to parse a multiline textbox and convert its content into an array instead of using the relatively standard combination of a single line textbox, add and remove buttons, and a list box is tacky… :wink:

Regards your code, another possible way of doing this would be something like below. Might need a bit of tidying up, but sure you’ll get the gist of it.


$RunButton_Click = {
    #TODO: Place custom script here
 
    [array] $Computers = $ComputerNamesTextBox.Text -split "`n"

    $Results = Foreach ($ComputerName in $Computers)
    {
        $ComputerName = $ComputerName.ToUpper()

        If ((Test-Connection -ComputerName $ComputerNamesTextBox.Text -Count 1 -Quiet) -eq $true) 
        {
            $Online = $true
        }
 
        Else
        {
            $Online = $False
        }

        $hash = @{
            ComputerName = $ComputerName
            Online       = $Online
        }

        New-Object -TypeName PSObject -Property $hash
    }

    $Results | Out-GridView
} 



I wasn’t trying to come off rude and if I did, then my apologies.

We can go tit for tat here, but I really want to just get this GUI put together and move on to something else.

But yes, you could argue that, it just all depends on who has more of a consensus in the argument.

Anyway, thanks for your help I really appreciate it.

I will give it a shot and get back.

Again, thank you both for your help.

No worries.

You can do that combination I mentioned pretty straightforwardly BTW. The Listbox.Items collection can then be parsed in your for…each script for your Test-Connection scriptblock to execute.

  • Drag two buttons, a textbox, and a listbox item onto your form [on the basis that you’ve already the normal OK, Cancel buttons in place]
  • Rename the text on the textboxes to Add and Remove
  • Configure your add/remove buttons as the following respectively, replacing $lstComputers with the name of the listbox
$Add_Click={
	$lstComputers.Items.Add($textbox1.Text)
}

$buttonRemove_Click={
	$lstComputers.Items.Remove($lstComputers.SelectedItem)
}

NB You can also change the setting for the listbox as to whether you can select more than one item.

Awesome!

Thanks man

Great job. Sir Ask Alot, you can also get help on the PowerShell Studio forum. If you post there and don’t get a quick or helpful answer, be sure to ping me at juneb@sapien.com.

Thanks!
June

June Blender
Technology Evangelist
SAPIEN Technologies, Inc.

Thanks June

I just started asking questions on there as well.

Are there any training videos that Sapien offers with the purchase of this product by any chance?

The learning curve is a tad steep, but I really need to get this down.

I may just end up going Visual Studio and using the PowerShell plugin.

However, I prefer not to do that since I already purchased this product in December.

Thanks