Part III – Using Advanced GUI Elements in PowerShell

series_PowerShellGUI

This post is part of the Learning GUI Toolmaking Series, here on FoxDeploy. Click the banner to return to the series jump page!


Welcome back to the GUI Series here on FoxDeploy.com! In the previous weeks, I’ve had a lot of feedback and requests from you guys (which I absolutely love! Assuming I don’t need sleep, I’m content to stay up till the wee hours of the morning helping you guys out and responding to your comments or requests, so keep ’em coming!). As for where this blog is heading…I’ve really been bit hard by the maker bug, and I recently purchased a RaspBerry Pi 2, which is super awesome and incredibly powerful. Direct deploying a program from Visual Studio to this little guy and seeing it light up and project content to my HDMI monitor really makes it feel like I’m doing something tangible. I’ll be making a whole series of posts about cool things you can do with the Pi. If you have any ideas, send ’em over!

We’re going to cover a number of topics in this one, namely ‘how do I do ex’ questions. We’ll have two somewhat silly examples to start, followed by an actually useful GUI to create a user to wrap us up here.

We’ll specifically be hitting all of these guys here, in order.

• Hide a UI Element (by special request)
• Use Checkboxes
• Use radio buttons (by special request)
• Populate a drop-down box automatically

Also, in a big departure from the past, I’ll not be posting full code on here anymore. From now on, code samples from here will always be linked and kept up-to-date on GitHub. You can find the code from this post here: https://github.com/1RedOne/Post_III

Alright, let’s get started. Power up Visual Studio and create a new project, specify ‘WPF’ as your type in the search box.

Hide a UI element

We’ll draw an image and set it to invisible, and then add a button that will make it appear or disappear. You can imagine and endless amount of examples where this could be useful

Now, let’s add a meaningful image (read:skybison) and then make it hidden. Finally, let’s add a button.

The XAML

<Window x:Class="BlogPostIII.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:BlogPostIII"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid x:Name="background" Background="#FF1D3245">
        <Image x:Name="image" HorizontalAlignment="Left" Height="100" Margin="171,154,0,0" VerticalAlignment="Top" Width="100" Source="C:\Users\sred13\Dropbox\My Code\Migmon\htdocs\Appa.png" Visibility="Hidden" />
        <Button x:Name="button" Content="Reveal Hidden Skybisons" HorizontalAlignment="Left" Height="34" Margin="10,277,0,0" VerticalAlignment="Top" Width="155"/>

    </Grid>
</Window>

This will give us a form with an image, and a button. To hook the button up to the image, we just need to add a few snippets of code:

$WPFbutton.Add_Click({
    if ($WPFimage.Visibility -ne 'Visible'){$WPFimage.Visibility = 'Visible'}
    else {$WPFimage.Visibility = 'Hidden'}
})

And…that’s it!

RevealUI Elements

Clicking the button will reveal or hide the image. You could use the same mechanism to set an item from Disabled to Enabled as well, we’re just illustrating the principles here. Moving right along…

Use Checkboxes

Alright, what we’ll do now is draw a check box, and add a second Sky Bison. We’ll make both Bisons invisible, and if you check the box, you’ll get TWO bisons for the price of one! If you uncheck the box, the Bison dies. I’m sorry kid, but that’s just a fact of life.

To save space, I’ll only show you the PowerShell behind making this work. Check the github link if you want the XAML or Project file.

This got a bit confusing, making the second skybison only appear when the first was visible and then checking for the checkbox IsChecked when the ‘show bisons’ button is clicked.

$WPFbutton.Add_Click({
if ($WPFimage.Visibility -ne 'Visible'){
if ($WPFcheckBox.IsChecked -eq $true){$WPFimage.Visibility,$WPFimage_Copy.Visibility = 'Visible','Visible'}
else {$WPFimage.Visibility = 'Visible'}
}
elseif ($WPFimage.Visibility -ne 'Visible' -and {$WPFcheckBox.IsChecked -eq $false})
{$WPFimage.Visibility = 'Visible'}
else{$WPFimage.Visibility,$WPFimage_Copy.Visibility = 'Hidden','Hidden'}
})

$WPFcheckBox.Add_Checked({if ($WPFimage.Visibility -eq 'Visible'){$WPFimage_Copy.Visibility = 'Visible'}else{$WPFimage_Copy.Visibility = 'Hidden'}})

And the result :

Clicking the checkbox makes TWO Appa's appear!
Clicking the checkbox makes TWO Appa’s appear!

Use Radio Buttons and auto-populate a Combo Box/Dropdown Box

Beginning with this example, we’ll start by combining some of the above approaches and work towards making a more useful tool, a GUI that wraps around making a new user and makes it a bit easier to set an account expiration date. We’ll use this tool to wrap the New-ADUser Cmdlet, so we should first check the requirements for that command.

We’ll need to define the user’s Given (first name), their surName (last name), account name and PW.

For the components we’ll use:
• Checkbox – click the checkbox to specify the user is a temp and should expire
• ComboBox – a drop-down box filled with acceptable places for a new user to be placed
• Radio buttons – use the radio buttons to specify 7 days, 30 days, 90 days account life
• Textblock – we’ll use these as pretty labels for our tool

You can drag and drop them however you’d like, here’s what I came up with:

Ohhh, so gooey!
Ohhh, so gooey!

So, here we go. We’ve got a first name, last name, logon name and password textboxes. You’ll use those as you would, expect to use them. At first, we’ll be displaying the PW in plaintext but on the next revision, we’ll add the feature to display asterisks and require you to click to reveal the PW. Finally, we have two new classes here, the combo box and the radio button.

ComboBoxes are used to provide a user history (similar to auto-complete in your web browser of choice) or to constrict the user to only certain options. We’ll use the ComboBox to provide three or four places where a contractor can go.

Radio Buttons are used to present the user with a number of preset options, and allow them to only select one of them at a time (usually, there is a weird thing called a tri-state radio button, which means you can select two, but those are uniformly hated and loathed by users and devs alike, and should be killed with fire and/or the delete key).

Let’s code these up!

I wrote a few chunks of code here. First, when the user clicks the checkbox to enable a limited timespan user, I needed to add some logic to enable the radio buttons and pick one of them, which you see here. This is done by adding a script block to the Checkbox using the Add_Checked and Add_Unchecked methods:

#Add logic to the checkbox to enable items when checked
$WPFcheckBox.Add_Checked({
    $WPFradioButton_7.IsEnabled=$true
   $WPFradioButton_30.IsEnabled=$true
   $WPFradioButton_90.IsEnabled=$true
    $WPFradioButton_7.IsChecked=$true
    })
#when this box is unchecked, make sure that none of the option bubbles are selected
$WPFcheckBox.Add_UnChecked({
    $WPFradioButton_7.IsEnabled=$false
   $WPFradioButton_30.IsEnabled=$false
   $WPFradioButton_90.IsEnabled=$false
    $WPFradioButton_7.IsChecked,$WPFradioButton_30.IsChecked,$WPFradioButton_90.IsChecked=$false,$false,$false})

Next, we need to link the Create User button up to the code to make a new user. Because the user may or may not be set to expire, I wanted a method to easily end up with an object called $Hash that contains the needed settings to make a new user account. I ended up writing a helper function called Get-FormField which will gather all of the settings the user specifies into the form, which is then used later on when you click the ‘Create User’ button like so:

$WPFMakeUserbutton.Add_Click({
    #Resolve Form Settings
    $hash = Get-FormFields
    New-ADUser @hash -PassThru
    $Form.Close()})

Finally,  populating the combo box, it’s actually super easy. The combo box name in this example is targetOu_ComboBox, which ends up becoming the PowerShell object $WPFtargetOU_ComboBox. We call its AddChild method to add entries to the list. I ran a quick LDAP query (thanks to this post on SuperUser for showing me the way!) to get the default OU for a new user and stored that in $defaultOU, and then manually typed in the Distinguished Name of the other OU I wanted to provide as an option.

$defaultOU = (get-adobject -filter 'ObjectClass -eq "domain"' -Properties wellKnownObjects).wellknownobjects.Split("`n")[-1].Split(':') | select -Last 1

$defaultOU,"OU=Contractors,DC=FOXDEPLOY,DC=local" | ForEach-object {$WPFtargetOU_comboBox.AddChild($_)}

This results in both names pre-populating our dropdown box, like so:

Just use $WPFCombo.AddChild() to add more items to the dropdown
Just use $WPFCombo.AddChild() to add more items to the dropdown

The radio buttons are used to pick 7, 30 or 90 days as the expiration date for this account. We first check to see if the Checkbox for temporary user account is checked, and if so, we then check to see which bubble/radio button is checked. We then reset $Expiration date to equal get-date.AddDays($expirationDate) and pass that along too, using Get-FormFields, this is done in lines 7-10 below.

Here’s the full code for the ‘#Making it work’ block.

$defaultOU = (get-adobject -filter 'ObjectClass -eq "domain"' -Properties wellKnownObjects).wellknownobjects.Split("`n")[-1].Split(':') | select -Last 1
 $WPFDefaultOUMsg.Text = $WPFDefaultOUMsg.Text -replace "@anchor",$defaultOU

#gather all of the settings the user specifies, needed to splat to the New-ADUser Cmd later
function Get-FormFields {
$TargetOU = if ($WPFtargetOU_comboBox.Text -ne $null){$WPFtargetOU_comboBox.Text}else{$defaultOU}
if ($WPFcheckBox.IsChecked){
    $ExpirationDate = if ($WPFradioButton_7.IsChecked -eq $true){7}`
                elseif ($WPFradioButton_30.IsChecked -eq $true){30}`
                elseif ($WPFradioButton_90.IsChecked -eq $true){90}

    $ExpirationDate = (get-date).AddDays($ExpirationDate)

    $HashArguments =
        @{ Name = $WPFlogonName.Text;
           GivenName=$WPFfirstName.Text;
           SurName = $WPFlastName.Text;
           AccountPassword=($WPFpassword.text | ConvertTo-SecureString -AsPlainText -Force);
           AccountExpirationDate = $ExpirationDate;
           Path=$TargetOU;
            }
        }
    else{
    $HashArguments =
       @{ Name = $WPFlogonName.Text;
          GivenName=$WPFfirstName.Text;
          SurName = $WPFlastName.Text;
          AccountPassword=($WPFpassword.text | ConvertTo-SecureString -AsPlainText -Force);
          Path=$TargetOU;
          }
    }
$HashArguments
}

$defaultOU,"OU=Contractors,DC=FOXDEPLOY,DC=local" | ForEach-object {$WPFtargetOU_comboBox.AddChild($_)}

#Add logic to the checkbox to enable items when checked
$WPFcheckBox.Add_Checked({
    $WPFradioButton_7.IsEnabled=$true
   $WPFradioButton_30.IsEnabled=$true
   $WPFradioButton_90.IsEnabled=$true
    $WPFradioButton_7.IsChecked=$true
    })

$WPFcheckBox.Add_UnChecked({
    $WPFradioButton_7.IsEnabled=$false
   $WPFradioButton_30.IsEnabled=$false
   $WPFradioButton_90.IsEnabled=$false
    $WPFradioButton_7.IsChecked,$WPFradioButton_30.IsChecked,$WPFradioButton_90.IsChecked=$false,$false,$false})

#$WPFMakeUserbutton.Add_Click({(Get-FormFields)})
$WPFMakeUserbutton.Add_Click({
    #Resolve Form Settings
    $hash = Get-FormFields
    New-ADUser @hash -PassThru
    $Form.Close()})

For the full source, click here!

Let’s use my favorite test-user, I like Ham.

My wife HATES ham
My wife HATES ham

And in testing this in my environment…it works!

I titled this picture 'The Expiration of Ham', it cracks me  up
I titled this picture ‘The Expiration of Ham’, it cracks me up

What’s next?

I’d planned to include building a tabbed interface, making a progress bar and finally building an all-in-one management tool using PowerShell in this post, but I’ll be saving those now for part IV of the series, as my fingers are tired and me no-wanto-writey anymore! Until then, please let me know here, on e-mail, or on Twitter if there are any other GUI features you’d like to see me outline!

Part IV – Creating a Tabbed Interface and handling events

48 thoughts on “Part III – Using Advanced GUI Elements in PowerShell

  1. Mattis May 14, 2015 / 5:15 pm

    awesome mate!!!

    • chandreshcpal February 25, 2016 / 6:21 am

      Great.. looking for a snapshot backup for a Particular Folder, With Auto schedule and Incremental backup facility..

  2. mrbrandonboyd May 15, 2015 / 9:12 am

    I feel like George R.R. Martin might be your pen name: seeing a Sky Bison, to DUPLICATING the Sky Bison, to MURDERING the Sky Bison in like two blinks of an eye. GASP.

    Thank you for another great post though!! I link it to all my PS trainees!

  3. nohandle (@nohwnd) May 17, 2015 / 1:26 pm

    Hi fellow MVP :), great articles.

    One thing worries me though, you unfortunately got caught in the trap the VS has prepped for you, and you started dragging and dropping the controls. This is one way to go but it has a lot of drawbacks. One of them is how difficult is to make your GUI stay well aligned even after you decide to add something. Another is that you cannot auto size controls and keep the layout, which becomes quite important once you start to localize your UI.

    The other way is to use some of the WPF layout controls like Grid or DockPanel and define the layout using them.

    If you wan’t to talk about this please contact me on my e-mail (or skype which is the same). I could also write a guest post for your blog covering the topic, if you are interested.

    • FoxDeploy May 17, 2015 / 1:43 pm

      You know, using those controls is a great idea! Thanks for bringing it up! Would you ve interested in doing s guest post? I’d love it and would happily link to your site, twitter, etc.

  4. Mattis May 17, 2015 / 6:04 pm

    me again…is there any chance of “compiling” a PS script as an EXE?

    • FoxDeploy May 17, 2015 / 6:31 pm

      Well, the short answer is no, while the long answer is kind of, but not really.

      However, what we’ll work into is grasping C# for PowerShell experts, which is a series I plan to kick off midsummer. PowerShell syntax is really very close to c#, and I’ll ve doing a number of code samples, like this in powershell equals this in c#. If we build a GUI or scripts in c sharp only, its easy to compile those into an exe.

      Now why am I doing all of this? This is the sort of thing I’m interested, and that I see “my people” (folks like you, Francois, Adam and Matt Graeber) being interested in too. I think over to course of our careers, we’ll naturally be called into infrastructure development/devops roles, and so we’ll need to know how to make tools like this and how to legitimately code for real, to stay competitive.

      Man , I cannot give a short answer!

  5. Angelo Papiccio June 5, 2015 / 8:54 pm

    Thank you for those posts. I have been using powershell for years but am in the middle of creating user friendly versions for our Help Desk team. This has made it very easy for me to take some of the basic GUI concepts and apply them to powershell.

    I am keen to learn more around progress bars (MSDN is never the easiest to follow). I look forward to more of your posts in future.

    • FoxDeploy July 6, 2015 / 11:42 am

      Are you willing to share it with the rest of the class?

  6. Luke June 12, 2015 / 3:29 pm

    This is an awesome series, when do you plan on releasing part IV?

    • FoxDeploy June 12, 2015 / 4:08 pm

      I’m delivering a one week powershell boot camp next week, so part IV will probably be the week after. I did just release my one click domain controller dsc walk through though!

      • Matt August 4, 2015 / 10:54 am

        Ah that explains why you have been so busy! 🙂

        I have been working on my posh scripts first (so I know they work before I trigger them via buttons!) and also working on my XAML GUI.

        I will try to get the preloader bar working too so when a script has been launched it will show the helpdesk agents that something is running.

        I found out how to export the PS1 as a .EXE – its quite simple actually. First you download PowerGUI (its free! – http://powergui.org) then import your script and then select Compile.., adjust your settings (logo, 32/64bit etc) and it will create an executable.

        Make a shortcut of your executable and add to the target this line: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -WindowStyle Hidden -file

        This launches the ps without the annoying console window (well, it closes down after your app launches!) 🙂

  7. Jason Lehr June 29, 2015 / 3:57 pm

    Been trying to simply get a list of AD group members. Nothing happens when I click the button:

    $WPFbutton.Add_Click({
    $WPFlistView.Items.Clear()
    Import-Module activedirectory
    Get-adgroupmember -identity $WPFtextBox.Text | Select name | % {$WPFlistView.AddChild($_)}
    })

    When I run the form it pops up but also throws this error in the ISE:
    You cannot call a method on a null-valued expression.
    At C:\tmp\GroupEnumeratorGui.ps1:56 char:1
    + $WPFbutton.Add_Click({
    + ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

    • FoxDeploy July 6, 2015 / 11:36 am

      The error message here is telling us essentially ‘Hey, You’re trying to call method, with a null expression’. The method is $WPFButton.Add_Click. The expression is the parenthesis and everything that lives inside of it. So, essnetially PowerShell is evaluating this chunk of code and returning a big fat ‘Nothing!’ Try to run this code on it’s own, outside of the whole GUI and see what happens.

      Import-Module activedirectory
      Get-adgroupmember -identity | Select name | % {$WPFlistView.AddChild($_)}

      I’d run through this step by step. Does Import-Modu… that step work? If so, does Get-ADGroupMemb… work?

      I’m betting the problem is that Get-ADGroupMember is returning zero results, and so there’s nothing for the .AddChild() method to actually add.

      • Jason Lehr July 7, 2015 / 2:38 pm

        The command returns users for instance if I ran get-adgroupmember -identity “schema admins” | select name

        it would list the display name of each one per line. What i think is the problem is that the $WPFTextBox.text variable doesn’t appear to be defined anywhere, so how do I get the “on click” to pipe in whatever group name I typed in that blank text field?

        • FoxDeploy July 7, 2015 / 4:39 pm

          $wpftextbox.text gets filled when you type in The value in the text box.

  8. Kirk July 7, 2015 / 1:39 pm

    I’m trying to build a simple interface for copy-item / move-item functions. I’d like to see information about implementing a “Browse” button text box combo & also how to create Next buttons to flip screens. Such as browse to file > next > browse to target location > Finish.

  9. Vignesh July 20, 2015 / 10:22 pm

    can you please explain how to make sure selection of items in one combobox will change the results available in another combobox. For example, combobox 1 contains US, Asia, Europe and if US was selected next combobox should only display Newyork, Chicago.. thanks

    • FoxDeploy July 21, 2015 / 7:49 am

      Sure! I created a simple WPF form with a Primary and Secondary ComboBox. I use the .Add_SelectionChanged() Method for my primary box, and based on the value the user chooses, I add items to the SecondaryComboBox. Below is the code you would add to your snippet.

                                                         
          #Add the items Asia and USA to our First Combo box
          "Asia","USA" | ForEach-Object {$WPFPrimarycomboBox.AddChild($_) }
           
          #When the item changes in PrimaryComboBox, check the value, and then use that to add values to Secondary ComboBox 
          $WPFPrimarycomboBox.Add_SelectionChanged({
              if ($WPFPrimarycomboBox.SelectedItem -eq 'Asia'){
                  "Shanghai","Tokyo" | % {$WPFSecondarycomboBox1.AddChild($_)
                      }
                  }
                  else
                  {
                  "Atlanta","New York" | % {$WPFSecondarycomboBox1.AddChild($_)
                      }
                  }
              })
      
      • Vignesh July 21, 2015 / 9:47 am

        Thanks a lot 🙂 very helpful

      • vignesh July 22, 2015 / 6:57 pm

        If its not too much to ask, how to change OU selection from dropdown to tree structure querying directly from AD.

        • FoxDeploy July 22, 2015 / 7:03 pm

          Uh, I’m not sure about the tree structure. Is there a tool like that?

  10. Jakob October 2, 2015 / 5:15 pm

    Mr Fox,

    Would you say there would be any way to use more than one object in the same inputXML? I thought it might be an easy way to pull data back from another Window by having them both load into the powershell as one and just swap their “Visibility” when i needed to.

  11. Chris December 18, 2015 / 5:52 pm

    Hello,

    I used this post to create a simple password reset application to reset users in Office 365. The script I’ve created, generates a random password and assigns that password to the user.

    My question is how do I hide that password until I hit the reset button?

  12. Bill March 9, 2016 / 12:23 pm

    Hi Stephen,

    Can you provide me an example, how I can create an combo box with thee, or four items? items ( probably I will extend them later with some other one’s)

    Thanks

    • FoxDeploy March 9, 2016 / 4:22 pm

      We cover that in this post. This is the pertinent bit that you’re looking for:

      Finally, populating the combo box, it’s actually super easy. The combo box name in this example is targetOu_ComboBox, which ends up becoming the PowerShell object $WPFtargetOU_ComboBox. We call its AddChild method to add entries to the list. I ran a quick LDAP query (thanks to this post on SuperUser for showing me the way!) to get the default OU for a new user and stored that in $defaultOU, and then manually typed in the Distinguished Name of the other OU I wanted to provide as an option.

      $defaultOU = (get-adobject -filter ‘ObjectClass -eq “domain”‘ -Properties wellKnownObjects).wellknownobjects.Split(“`n”)[-1].Split(‘:’) | select -Last 1

      $defaultOU,”OU=Contractors,DC=FOXDEPLOY,DC=local” | ForEach-object {$WPFtargetOU_comboBox.AddChild($_)}

  13. James A. Wall May 19, 2016 / 10:44 am

    Having a hard time getting this to work … the autocomplete text box.

    Exception calling “AddChild” with “1” argument(s): “The object ‘BAF2’ cannot be added to the TextBox, since it can contain only plain text.”
    At line:1 char:43
    + $objRecordset.GetRows() | ForEach-object {$WPFtextBox7.AddChild($_)}

    Help?!

    Thanks in advance!

  14. James A. Wall May 19, 2016 / 11:46 am

    PS C:\Windows\System32\WindowsPowerShell\v1.0> $WPFtextbox.AutoCompleteSource = ‘CustomSource’
    The property ‘AutoCompleteSource’ cannot be found on this object. Verify that the property exists and can be set.
    At line:1 char:1
    + $WPFtextbox.AutoCompleteSource = ‘CustomSource’
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

      • FoxDeploy June 9, 2016 / 5:50 pm

        Sorry, what are you referring to?

        • JWD June 9, 2016 / 8:36 pm

          Sorry, i thought i was replying to James A. Wall’s comment – I notice he asks about “autocomplete” on a XAML generated textbox in a couple of your posts… i am getting the same errors as him, and it looks like XAML doesn’t natively support it… and while i can programmatically populate a combobox it doesn’t seem to allow me to type in it and thus no “suggest” option.
          so I am thinking that your example of using the System.Windows.Forms is what i might have to do for my project… i started with XAML because your “guides” were the first to come up when i began searching for how to do a GUI with PowerShell and i happened to have VisualStudio 2015 already installed.
          But the guides/tutorials are actually really jam packed with great things i didn’t know, and i really enjoyed making the GUI in VisualStudio and the idea that i can just copy that XAML out and paste into PowerShell is cool, but I hit a roadblock when i came to that AutoComplete hiccup.

      • James June 10, 2016 / 5:36 am

        I had to move to Windows form aswell

        • FoxDeploy June 10, 2016 / 9:43 am

          Autocomplete is not a native feature of WPF, so you’d have to integrate some other code to do this. The easiest way to accomplish this in box is to use a Combobox instead.

        • James A. Wall June 10, 2016 / 9:53 am

          Is it possible to use JQuery or Angular2… The problem with combo box is it doesn’t suggest or rather, I don’t understand how to make it work like an autocomplete box with suggestappend option. Besides all that, Thank you for all the work you have done Stephen!

        • FoxDeploy June 10, 2016 / 8:49 pm

          It’s totally possible in jQuery and Angular, but that is about as far away from PowerShell as you can get :p. jQuery’s textboxes include Autocomplete, and it’s super easy. https://jqueryui.com/autocomplete/

  15. James Wall June 14, 2016 / 10:04 am

    Think you can show me how to do it in powershell with an xaml form? CHeers!

  16. christoph July 15, 2016 / 10:08 am

    hey, is it possible to insert a animated gif while a function is loading?
    i tried to load a gif into , that works, but its not animated.
    then the idea was to use the visibility option from your post above:
    1.) default: hidden
    2.) when you click on a button change to visible
    3.) calling some functions
    4.) at the end of “click action” set it back to hidden
    is this possible with standard commands?

  17. Chris September 21, 2016 / 4:40 am

    Hi Stephen,

    based on your examples I’ve made a really useful application for our Helpdesk. Thank you very much for all the useful and excellent described articels about creating Powershell GUIs !!
    Now I’m struggling with a pretty simple problem I guess: If you do a DoubleClick on a textbox, I would like to send the value of it into the clipboard. I tried something like this, but it doesnt work $WPFtextBox_test.Add_DoubleClick({ $contentoftextbox | Clip }) . Could you give me a advice please?

    • FoxDeploy September 21, 2016 / 8:43 am

      Does $contentoftextbox contain JUST the .Text property of that textbox? Clip will work with system.string plain text, but not with objects.

      • Chris September 21, 2016 / 9:29 am

        Yes, but the error is before: “Method invocation failed because [System.Windows.Controls.TextBox] does not contain a method named ‘Add_DoubleClick’.” (FullyQualifiedErrorId : MethodNotFound).

  18. mpaynter January 4, 2017 / 5:50 pm

    Wanted to know how to have the combobox show a different value than what it delivers to powershell.

    For instance, it would show just the city name and not the full OU value, but would still send the OU to powershell to add the user.

    Thanks!

    • FoxDeploy January 4, 2017 / 6:31 pm

      I would make a custom object with both city and OU properties for each entry you want to put in the combobox.

      To put just the city property in the combobox, select that property and add children to the box.

      Later in your script, use the city name to select the matching object from your array.

      Does that make sense? If not, lemme know.

      • mpaynter January 5, 2017 / 1:48 pm

        That was exactly what I needed.

        I made several new objects with:
        $object1 = [PSCustomObject]@{
        ‘Name’=’Office1’
        ‘OU’=’OU=group,DC=office,DC=local’
        ‘Phone’=’555-555-5555’}

        Added them to the combo box with:
        $object1,$object2,$object3 | Select Name -ExpandProperty Name | ForEach-object {$WPFcomboBoxTargetOU.AddChild($_)}

        Then updated the Get-FormFields function with:
        if($WPFcomboBoxTargetOU.Text -eq ‘Office1’){
        $HashArguments.Add(‘OfficePhone’,$object1.Phone)
        $HashArguments.Add(‘Path’,$object1.OU)
        }elseif…..

  19. FoxDeploy January 5, 2017 / 2:01 pm

    Good job man! That’s exactly what I meant

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s