Powershell and Windows Forms

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 11 months ago.

  • Author
    Posts
  • #5355

    by ratty67 at 2012-12-16 20:37:45

    Guys,

    Am creating a Windows Form for AD Account Creation. It has taken me a while (knew nothing about FORMS but a bit about Powershell). I now have the basics. Form collects data and then on hitting the CREATE button, will create the user. This creation process can take a while and I would like to put some type of progress indicator on the form for the helpdesk user. I want the form to stay open, as I have button options to close or create another user.

    I have utilised functions thoughout my form and the createUser function basically has 6 steps (create (from a template), enable, create filsesystem homedrive and modify user homedrive info, copy group membership (from template), create mailbox (using external dos command) and, set filesystem rights (waited for filesystem folder creation))

    I am aiming for a progress update after each of these steps – so my progress meter/bar will likely show 16/33/50/67/83/100.

    Anyone have an idea on how to do this ? Do I have to spawn my createuser parts as JOBS, so I can use the current 'thread' for write-progress, or is there another way ??

    Thanks in advance

    by coderaven at 2012-12-17 12:25:28

    Creating forms like that can get very complex. I would recommend looking into something like PrimalStudio. It has a cost to it but if you add up the time savings in just the creating and debugging of a forms application like this, the cost is recovered quickly. Other than that, using jobs is a good way to go and you may want to consider workflows which has progress indicators for each activity built in assuming you would want to display the PowerShell window during execution in non-interactive mode.

    by nohandle at 2012-12-18 04:27:21

    Hi, If you need to support users on WindowsXP then the Windows Forms is way to go. You can get free copy of Visual Studio Express and design the form there, then you just have to export the definitions and change the calls to comply with powershell syntax.
    If you are sure your users are on vista or newer you can use WPF (windows presentation foundation) to create the form. There is lot of tools to do it, the Visual studio is one of them. As output you get the design in XAML (XML) form that you can import to the powershell. Then you just define appropriate functions. I saw the whole process described in the Powershell in Action book so I am not really aware of any really good article about it.
    Here is one point to start form: http://blogs.msdn.com/b/powershell/arch ... f-wpf.aspx
    and here is codeplex project that could help you: http://powerwpf.codeplex.com/

    You are gonna need to avoid the WPK described here http://msdn.microsoft.com/en-us/magazine/hh288074.aspx because it requires installation.

    On the form you want Progress bar component and you need to instruct it to progress after finishing each task.

    In my opinion the tasks won't benefit from running in parallel, but the Jobs concept can give you nice instrumentation to manage the tasks.

    by ratty67 at 2012-12-18 20:27:24

    Thanks for the info Allan and Jakub. I was trying to use the system.windows.forms.progressbar but was having issues – I have now sorted these and I get a progress bar as desired.

    Although, I am using the progressbar.step() method and my form doesn't seem to be in sync with code. Each "step" of my code I step the progressbar, update and refresh the form but it seems to lag behind – any ideas ?

    I am now also trying to 'log' progress with a multiline textbox (both rich and normal while I test). My richtext box is of a certain height and I would like the contents to scroll off the top so the last line of text is always visible at the bottom of the richtext box. Similarly, for the textbox, which is of single line height, I would like the contents to update with the current output. Can anyone suggest a solution (or point me to some doco – can't seem to find what I'm looking for on Google)

    by nohandle at 2012-12-19 06:11:07

    [quote="ratty67"]Each "step" of my code I step the progressbar, update and refresh the form but it seems to lag behind – any ideas ?[/quote]Hard to tell, without hands-on. If you step the code (in debug mode) is the status bar updated properly?

    2) you basically mean redirecting all the output to a text box?

    by ratty67 at 2012-12-19 12:50:42

    Jakub,

    The example function below hopefully makes things clearer on both counts. The function will return text to the textboxes well before the progress bar itself updates (ie textbox can be up to step 3 while progressbar has only made 1 step when it should have done 3). The actual form code is just basic PrimalForms CE generated.

    As for the textboxes, I guess I would like the singleline textbox to display the last line of output. The multiline textbox I would like to see scroll through the output with the last output always visible.

    function Count
    {
    $steps = 6
    $progressBar1.value = 0
    $progressBar1.maximum = 6
    $progressBar1.step = 1
    $richTextBox1.lines = "Beginning Step 1"
    $richTextBox1.scrolltocaret
    $TextBox1.text = "Beginning Step 1"
    $TextBox1.scrolltocaret
    $form1.update(); $form1.refresh()
    for ($x = 1; $x -le 6; $x++){sleep 1}
    $progressBar1.performstep()
    $richTextBox1.lines = $richTextBox1.lines + "Completed Step 1"
    $richTextBox1.select($richTextBox1.text.length,0)
    $richTextBox1.scrolltocaret
    $TextBox1.text = "Completed Step 1"
    $TextBox1.update()
    $TextBox1.scrolltocaret
    $form1.update(); $form1.refresh()
    $richTextBox1.lines = $richTextBox1.lines + "Beginning Step 2"
    $richTextBox1.select($richTextBox1.text.length,0)
    $richTextBox1.scrolltocaret
    $TextBox1.text = "Beginning Step 2"
    $TextBox1.update()
    $TextBox1.scrolltocaret
    $form1.update(); $form1.refresh()
    for ($x = 1; $x -le 6; $x++){sleep 1}
    $progressBar1.performstep()
    $richTextBox1.lines = $richTextBox1.lines + "Completed Step 2"
    $TextBox1.update()
    $richTextBox1.scrolltocaret
    $TextBox1.text = "Completed Step 2"
    $TextBox1.select($TextBox1.text.length,0)
    $TextBox1.scrolltocaret
    $form1.update(); $form1.refresh()
    $richTextBox1.lines = $richTextBox1.lines + "Beginning Step 3"
    $richTextBox1.select($richTextBox1.text.length,0)
    $richTextBox1.scrolltocaret
    $TextBox1.text = "Beginning Step 3"
    $TextBox1.update()
    $TextBox1.scrolltocaret
    $form1.update(); $form1.refresh()
    for ($x = 1; $x -le 6; $x++){sleep 1}
    $progressBar1.performstep()
    $richTextBox1.lines = $richTextBox1.lines + "Completed Step 3"
    $richTextBox1.select($richTextBox1.text.length,0)
    $richTextBox1.scrolltocaret
    $TextBox1.text = "Completed Step 3"
    $TextBox1.update()
    $TextBox1.scrolltocaret
    $form1.update(); $form1.refresh()
    $richTextBox1.lines = $richTextBox1.lines + "Beginning Step 4"
    $richTextBox1.select($richTextBox1.text.length,0)
    $richTextBox1.scrolltocaret
    $TextBox1.text = "Beginning Step 4"
    $TextBox1.update()
    $TextBox1.scrolltocaret
    $form1.update(); $form1.refresh()
    for ($x = 1; $x -le 6; $x++){sleep 1}
    $progressBar1.performstep()
    $richTextBox1.lines = $richTextBox1.lines + "Completed Step 4"
    $richTextBox1.select($richTextBox1.text.length,0)
    $richTextBox1.scrolltocaret
    $TextBox1.text = "Completed Step 4"
    $TextBox1.update()
    $TextBox1.scrolltocaret
    $form1.update(); $form1.refresh()
    $richTextBox1.lines = $richTextBox1.lines + "Beginning Step 5"
    $richTextBox1.select($richTextBox1.text.length,0)
    $richTextBox1.scrolltocaret
    $TextBox1.text = "Beginning Step 5"
    $TextBox1.update()
    $TextBox1.scrolltocaret
    $form1.update(); $form1.refresh()
    for ($x = 1; $x -le 6; $x++){sleep 1}
    $progressBar1.performstep()
    $richTextBox1.lines = $richTextBox1.lines + "Completed Step 5"
    $richTextBox1.select($richTextBox1.text.length,0)
    $richTextBox1.scrolltocaret
    $TextBox1.text = "Completed Step 5"
    $TextBox1.update()
    $TextBox1.scrolltocaret
    $form1.update(); $form1.refresh()
    $richTextBox1.lines = $richTextBox1.lines + "Beginning Step 6"
    $richTextBox1.select($richTextBox1.text.length,0)
    $richTextBox1.scrolltocaret
    $TextBox1.text = "Beginning Step 6"
    $TextBox1.update()
    $TextBox1.scrolltocaret
    $form1.update(); $form1.refresh()
    for ($x = 1; $x -le 6; $x++){sleep 1}
    $progressBar1.performstep()
    $richTextBox1.lines = $richTextBox1.lines + "Completed Step 6"
    $richTextBox1.select($richTextBox1.text.length,0)
    $richTextBox1.scrolltocaret
    $TextBox1.text = "Completed Step 6"
    $TextBox1.update()
    $TextBox1.scrolltocaret
    $form1.update(); $form1.refresh()
    }

    ps there may be a better way to achieve what I am after – I have only been looking into this for a few weeks and knew NOTHING about forms previously. Any advice would be greatly appreciated.

    Thanks

    EDIT: Found error for single line textbox – using $textbox1.text = $textboxt.lines + "text" when should have been using $textbox.text = "text" (cut and paste error). Code amended above.

    by Davidc at 2012-12-20 13:35:20

    I wrote a few blog articles you might find useful:

    Creating Responsive Loops

    Creating Responsive Forms

    Spotlight on the TextBox Control

    Hopefully they will help answer some of your questions.

    David

    by nohandle at 2012-12-21 03:18:17

    [quote="Davidc"]I wrote a few blog articles you might find useful]
    These are great. 🙂

    by ratty67 at 2012-12-23 15:49:08

    Although those articles are good they don't seem to relate to my issue.

    I have stepped through the code and it seems that the form only updates after the second progressbar.performstep(). This is when I see the result of the first step. The last step update occurs at the completion of the function.

    UPDATE: I changed the initial value to "1" and it seems to work as expected. I do not fully understand the logic as to why this is.

You must be logged in to reply to this topic.