Author Posts

March 5, 2015 at 10:08 am

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 [b]"Test1ComputerTest2ComputerTest3Computer is offline"[/b].

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

March 5, 2015 at 10:19 am

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

March 5, 2015 at 10:37 am

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

March 5, 2015 at 12:20 pm

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... 😉

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
} 



March 5, 2015 at 2:34 pm

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.

March 5, 2015 at 9:08 pm

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.

March 5, 2015 at 10:46 pm

Awesome!

Thanks man

March 6, 2015 at 10:19 am

Great job. Sir Ask Alot, you can also get help on the [url="http://www.sapien.com/forums/viewforum.php?f=12"]PowerShell Studio forum[/url]. 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.

March 6, 2015 at 6:13 pm

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