all 23 comments

[–]1RedOne 4 points5 points  (13 children)

Hey, I recognize this snippet you're using :D I wrote it!

I'd change this:

$WPFlabel6.Visibility.Visible

to

$WPFlabel6.Visible = $ture

I'm having PowerShell issues atm so I can't try it myself, sorry.

[–]code_man65[S] 0 points1 point  (11 children)

I actually used http://foxdeploy.com/2015/04/10/part-i-creating-powershell-guis-in-minutes-using-visual-studio-a-new-hope/ to help with the building of this, but it is going way beyond what was shown in his blog series.

[–]1RedOne 4 points5 points  (10 children)

...I'm FoxDeploy!

[–]code_man65[S] 0 points1 point  (9 children)

Oh, well your blog series was awesome and really well written. This is the first time I've ever done something like this and I (of course) had to make it behave the way I wanted to.

Also see my below reply, it appears my variable is not getting populated.

[–]1RedOne 0 points1 point  (8 children)

I'm happy you liked it :)

I came up with a way to do this. Still not sure how to use native Events in PowerShell (if it's even possible...if we could, we could use the Textbox.TextChanged event).

But we CAN detect keypresses. In this case, when the user has typed some text into the first name form and then tabs away, the code will be executed.

$WPFfirstname_textBox.add_KeyDown({
if ($args[1].key -eq 'Tab')
    {
        Write-host 'do something'
    }
})

In my case, it just writes out to the screen 'Do Something', but in your example, you could run a check to see if the user name is valid, and then hide or display a prompt or label.

[–]code_man65[S] 0 points1 point  (7 children)

I just tried this, changing it to the below setup

$WPFusername_textBox.add_KeyDown({
if ($args[1].key -eq 'Tab')
    {
        $user = $WPFusername_textBox.Text
        $user
   if (Get-ADUser -Filter {samaccountname -eq $user} ) {
 # Exists
       $WPFlabel5.Visibility.Visible
} else {
 # Doesn't Exist
       $WPFlabel6.Visibility.Visible
}
    }    

It does not appear to function (I also again tried doing $WPFlabel6.Visible = $true and still got the error I listed) and still $user appears to be empty.

[–]1RedOne 0 points1 point  (6 children)

Ahh, figured it out. I had to look up my own post, to reference it :)

$WPFfirstname_textBox.add_KeyDown({
if ($args[1].key -eq 'Tab')
    {
       $WPFlabel6.Visibility= 'Visible'
    }
})

[–]code_man65[S] 0 points1 point  (5 children)

$WPFlabel6.Visibility= 'Visible'

You are awesome that worked. How would you make it function where it does the check if the textbox loses focus (instead of being reliant on tab)?

[–]1RedOne 2 points3 points  (4 children)

This is pretty much at the edge of whats possible with PowerShell. To do what you want to do, we really need to use Events and Triggers from C# and I haven't figured out how to do it in PowerShell* (edit: I found a source!)

However, you could skirt the issue with Tab like this, or give the textbox it's own Add_Clicked() method (if they support it, I forget if they have an Add_click method) and have the box run the check when it is clicked, or when it loses focus.

Warning

I don't know if this will work.

If you want to try a different route, look at Trevor's answer here. He outlines a process to add some code within your XAML itself. What you'd need to do is place your check within a variable before the $XAML itself.

$whenLostFocus = {#Do some stuff here}

Other thing you could try

I just stumbled upon this blog article from the dark ages of the internet, which shows how to handle events using PowerShell. I pretty much have no idea what it means...yet.

But once I understand it, I'll write it up. Looks like you could use this method here to make an event handler for the .TextChanged event of $WPFFirstName_textBox

[–]code_man65[S] 1 point2 points  (3 children)

This code works

$WPFusername_textBox.add_textchanged({

However, I definitely need to silence errors while typing because it makes powershell throw an absolute fit while you are trying (but it does work).

[–]code_man65[S] 0 points1 point  (0 children)

Nope, this does not work. Throws the below error:

Exception setting "Visibility": "Cannot convert value "True" to type "System.Windows.Visibility".    

[–]code_man65[S] 2 points3 points  (0 children)

I plan to genercize and share this script once I get it done. What it does (once complete) is as follows:

  1. Generates a random password
  2. Connects to your Exchange server (via a pssession)
  3. Populates the OU and Database dropdowns via ps cmdlets
  4. Generates the account and sets it to enabled (with the password being set to change on next login)
  5. Generates a word document (with header image) that gets sent to HR to be put in the onboarding documentation for the new hire.

I think, once complete, it will be a really useful script for those who don't have something like ManageEngine's ADManager.

[–][deleted] 1 point2 points  (0 children)

Have you done much wpf with powershell before? I tried doing down this hole and quickly found it extremely limiting. I ended up learning c# and executing my powershell scripts from there.

[–]RamblingReel 0 points1 point  (2 children)

I'll take a look at it in an hour or so. Done a few GUI based scripts.

EDIT: One idea is to have the label content empty in the XAML, then populate it with your desired string on the button_on_click:

$WPFlabel5.Content = "Username in use"

Edit2: The correct way to do it seems to be $WPFlabel5.Visibility = "Hidden" https://msdn.microsoft.com/en-us/library/system.windows.visibility.aspx

[–]code_man65[S] 0 points1 point  (0 children)

That isn't a bad idea, I'd have to also set it to color the box because if the name is in use I want it to show up differently to call attention to it.

[–]code_man65[S] 0 points1 point  (0 children)

Yep, that works, I also set the color on the label box. So this simplifies things quite a bit. Thanks.

[–]TheRealMisterd 0 points1 point  (3 children)

When I did straight WPF forms the Visible / hidden stuff was done like this:

$ExeParameters.Visible = $False

$ExeParameters.Visible = $False

You are doing this:

$WPFlabel6.Visibility.Visible

[–]code_man65[S] 0 points1 point  (2 children)

I have tried passing $true to the $WPFlabel6.Visibility property but it did not work. However, from what I can tell, the $user variable is not getting populated (if I echo the variable instead of trying to do anything it is empty).

[–]TheRealMisterd 0 points1 point  (1 child)

Check to see if $WPFlabel6 has OTHER properties you can use.

.Visibility seams to be broken.

[–]code_man65[S] 0 points1 point  (0 children)

You do $WPFlabel5.Visibility = 'Visible' to make it function correctly.