0

I have the below snippet of code that is supposed to give the user (myself) a YesNo box. If I select Yes, it assigns a value to a variable ($Category). If I select No, it is supposed to start the IncOrRitm function, which I then select from two radio buttons. Each button has a variable assigned to it, with a value to for that variable. If No was selected, the value for the variable assigned to whichever radio button I select should be assigned to the $Category variable.

The logic is this:

Correct category?

  • Yes -> $Category = Yes

  • No -> What should ticket have been?

    • Incident -> $Category = Incident

    • RITM -> $Category = RITM

However, only the "Yes" part of this code works. I am not sure if I am missing something, or if the function is nested incorrectly, or what....

# is the ticket correctly listed as an incident/ritm
Add-Type -AssemblyName PresentationCore,PresentationFramework
$ButtonType = [System.Windows.MessageBoxButton]::YesNo
$MessageTitle = "Incident or RITM"
$MessageBody = "Was the category correctly selected?"
$IncOrRITM = [System.Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType)
if ($IncOrRITM -eq "Yes")
    {
        $Category = "Correct"
    }
elseif ($IncOrRITM -eq "No")
    {
        function IncOrRITM{
        [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
        [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 

        $Form = New-Object System.Windows.Forms.Form
        $Form.width = 300
        $Form.height = 170
        $Form.Text = ”What Should The Ticket Have Been?"
        $Font = New-Object System.Drawing.Font("Verdana",11)
        $Form.Font = $Font

        $MyGroupBox = New-Object System.Windows.Forms.GroupBox
        $MyGroupBox.Location = '5,5'
        $MyGroupBox.size = '275,65'

        $RadioButton1 = New-Object System.Windows.Forms.RadioButton
        $RadioButton1.Location = '20,20'
        $RadioButton1.size = '90,30'
        $RadioButton1.Checked = $false 
        $RadioButton1.Text = "Incident"
        $RB1 = "Incorrect - Should be an Incident"
        $RadioButton2 = New-Object System.Windows.Forms.RadioButton
        $RadioButton2.Location = '150,20'
        $RadioButton2.size = '90,30'
        $RadioButton2.Checked = $false
        $RadioButton2.Text = "RITM"
        $RB2 = "Incorrect - Should be an RITM"

        $OKButton = new-object System.Windows.Forms.Button
        $OKButton.Location = '10,90'
        $OKButton.Size = '90,35' 
        $OKButton.Text = 'OK'
        $OKButton.DialogResult=[System.Windows.Forms.DialogResult]::OK
        $CancelButton = new-object System.Windows.Forms.Button
        $CancelButton.Location = '180,90'
        $CancelButton.Size = '90,35'
        $CancelButton.Text = "Cancel"
        $CancelButton.Add_Click({$objForm.Close()})
        $CancelButton.DialogResult=[System.Windows.Forms.DialogResult]::Cancel

        $form.Controls.AddRange(@($MyGroupBox,$OKButton,$CancelButton))
        $MyGroupBox.Controls.AddRange(@($Radiobutton1,$RadioButton2))
        $form.AcceptButton = $OKButton
        $form.CancelButton = $CancelButton
        $form.Add_Shown({$form.Activate()})    
        $dialogResult = $form.ShowDialog()

        if ($DialogResult -eq "OK")
            {
                if ($RadioButton1.Checked){$Category = $RB1}
                if ($RadioButton2.Checked){$Category = $RB2}
            }
        elseif ($DialogResult -eq "Cancel")
            {
                break
            }
    }
    IncOrRITM
}
sbagnato
  • 603
  • 3
  • 11
  • 35
  • Nesting function definitions inside conditionals is not a good idea, and the length and formatting of your code doesn't make that obvious. In the future, please consider providing [MCVE (Minimal, Complete, and Verifiable Example)](http://stackoverflow.com/help/mcve)s in your questions. – mklement0 Feb 20 '18 at 01:34

2 Answers2

1

Within the IncOrRITM function, $Catalog becomes a local variable once you assign to it. You don't return its value, so it is lost.

You can see a detailed explanation of variable scope in this answer on another question.

You should return the value you want from the function, then assign $Category to its call:

function IncOrRITM {
    # Other code

    if ($DialogResult -eq "OK")
            {
                if ($RadioButton1.Checked){ $RB1 }
                if ($RadioButton2.Checked){ $RB2 }
            }
        elseif ($DialogResult -eq "Cancel")
            {
                break
            }
}

$Category = IncOrRITM
briantist
  • 45,546
  • 6
  • 82
  • 127
  • While that makes sense to me logically, it does not change the result. – sbagnato Feb 19 '18 at 18:53
  • However, if I change it to a global variable with global:. that fixes it – sbagnato Feb 19 '18 at 19:06
  • 1
    @sbagnato making the variable global doesn't fix it; it masks the problem, making your code more error prone and harder to debug. I recommend you don't do that. – briantist Feb 20 '18 at 00:50
0

Adding $global: to the variable fixes it.

sbagnato
  • 603
  • 3
  • 11
  • 35
  • 1
    Using `$global:` makes the variable _session-global_, which means it will exist even after your script exits. I suggest you heed @briantist's advice and make the function _output_ the value of interest; for an overview of variable scopes in PowerShell, see the 2nd section of [this answer](https://stackoverflow.com/a/36347515/45375) of mine. – mklement0 Feb 20 '18 at 01:32
  • 1
    Excellent linked answer (as usual) from @mklement0, adding the link to my answer since it's extremely relevant. – briantist Feb 20 '18 at 03:18