2

I have created a function that creates a specific UI element with text input forms. This form also has three buttons:

  • One is supposed to display the text inputs again to do the same function again.
  • Another is supposed to finish and close the UI, passing the text input into the variables.
  • The last is supposed to cancel the UI element, doing nothing with anything in the text input forms.

Now I know the loop in the code isn't really complete, but I am having issues with it even performing the loop as well as passing the text forms into the variables. I know its something I am doing but it seems correct to me when I look at it.

  • Changed from an if loop, a while loop, and now a do/while loop.
  • Changed the position of the variables between the do section into the while section. Same for the if and while loops.

    do {
        ChangeDesc
    }
    while ($result -eq [System.Windows.Forms.DialogResult]::Retry)
    {
        $PC = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $HNInputBox.Text
        $PC.Description = $DescInputBox.Text
        $PC.Put()
    }
    
  • ChangeDesc is the name of the function and works just as intended.

  • Expected to work is to loop the function 'ChangeDesc', and then when the 'Retry' or 'Ok' button is pressed, pass those forms to the variables as shown.
  • Currently, it will display the form, and when the 'Retry' button is pressed, the forms are passed properly and then the UI is closed out, the 'Ok' button does not pass any input and the 'Cancel' does the same thing.

Below is the rest of my lines of code for clarification.

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

function ChangeDesc {
    $form = New-Object System.Windows.Forms.Form
    $form.Text = 'Data Entry Form'
    $form.Size = New-Object System.Drawing.Size(300,210)
    $form.StartPosition = 'CenterScreen'

    $AnotherButton = New-Object System.Windows.Forms.Button
    $AnotherButton.Location = New-Object System.Drawing.Point(15,130)
    $AnotherButton.Size = New-Object System.Drawing.Size(75,23)
    $AnotherButton.Text = 'Another?'
    $AnotherButton.DialogResult = [System.Windows.Forms.DialogResult]::Retry
    $form.AcceptButton = $AnotherButton
    $form.Controls.Add($AnotherButton)

    $FinishedButton = New-Object System.Windows.Forms.Button
    $FinishedButton.Location = New-Object System.Drawing.Point(100,130)
    $FinishedButton.Size = New-Object System.Drawing.Size(75,23)
    $FinishedButton.Text = 'Finished'
    $FinishedButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    $form.CancelButton = $FinishedButton
    $form.Controls.Add($FinishedButton)

    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Point(185,130)
    $CancelButton.Size = New-Object System.Drawing.Size(75,23)
    $CancelButton.Text = 'Cancel'
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    $form.CancelButton = $CancelButton
    $form.Controls.Add($CancelButton)

    $HNLabel = New-Object System.Windows.Forms.Label
    $HNLabel.Location = New-Object System.Drawing.Point(10,20)
    $HNLabel.Size = New-Object System.Drawing.Size(280,20)
    $HNLabel.Text = 'Enter Host Name:'
    $form.Controls.Add($HNLabel)

    $HNInputBox = New-Object System.Windows.Forms.TextBox
    $HNInputBox.Location = New-Object System.Drawing.Point(10,40)
    $HNInputBox.Size = New-Object System.Drawing.Size(260,20)
    $form.Controls.Add($HNInputBox)

    $DescLabel = New-Object System.Windows.Forms.Label
    $DescLabel.Location = New-Object System.Drawing.Point(10,70)
    $DescLabel.Size = New-Object System.Drawing.Size(280,20)
    $DescLabel.Text = 'Enter Description:'
    $form.Controls.Add($DescLabel)

    $DescInputBox = New-Object System.Windows.Forms.TextBox
    $DescInputBox.Location = New-Object System.Drawing.Point(10,90)
    $DescInputBox.Size = New-Object System.Drawing.Size(260,20)
    $form.Controls.Add($DescInputBox)

    $form.Topmost = $true

    $form.Add_Shown({$HNInputBox.Select()})
    $result = $form.ShowDialog()
}
4
  • 2
    There is no construct do {...} while (...) {...} in PowerShell. There is a do {...} while (...) and a while (...) {...}, and that's it. Also, if constructs are conditionals, not loops. And $HNInputBox.Text probably belongs on the same line as Get-WmiObject. If you want the $PC code executed after the loop terminates: don't put it in curly brackets. Commented Jun 20, 2019 at 17:42
  • The code executes when you click the 'Retry' button, and does its job normally...but it doesnt repeat at all. Also yes, the $HNInputBox.Text is supposed to be one line up, but it got moved down for some reason. Commented Jun 20, 2019 at 17:45
  • Please create a minimal reproducible example that actually demonstrates the problem you're facing, then maybe we could see what does or doesn't execute. About the code snippet you posted I cannot tell you more than I already have. Commented Jun 20, 2019 at 17:51
  • Other then the body of the function, that is my only lines of code. I am lost on how to properly use the function button results to either repeat the process or exit the loop and the script entirely. I will provide the rest of the code in the original question? Commented Jun 20, 2019 at 17:57

1 Answer 1

1

Your form is already closed when the loop terminates, and the variables you're trying to use are local to your function. Assign the values you're trying to use to script- or global-scope variables at the end of the function, and the code should do what you expect:

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

function ChangeDesc {
    $form = New-Object Windows.Forms.Form
    ...
    $script:result = $form.ShowDialog()

    $script:hostname    = $HNInputBox.Text
    $script:description = $DescInputBox.Text
}

do {
    ChangeDesc
} while ($script:result -eq [Windows.Forms.DialogResult]::Retry)

$PC = Get-WmiObject Win32_OperatingSystem -Computer $script:hostname
$PC.Description = $script:description
$PC.Put()
Sign up to request clarification or add additional context in comments.

3 Comments

OHHHHH!!! I was missing setting my function values to public. That worked for making the code execute as needed but now it wont repeat when I click the 'Retry' button. I thought that in the do {} section, it would execute that code as long as the while () section was true?
@nephsbirth $result is not different from the other variables inside your function. You need $script:result for it to work the way you intend. Updated my answer.
Thank you, that unfogged the mystery behind this and I greatly appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.