我有下面的代码片段,应该给用户(我自己)一个YesNo框。如果我选择是,则为变量赋值($ Category)。如果我选择No,它应该启动IncOrRitm功能,然后我从两个单选按钮中选择。每个按钮都有一个分配给它的变量,其值为该变量的值。如果选择否,则分配给我选择的任何单选按钮的变量的值应分配给$ Category变量。
逻辑是:
正确的类别?
是 - > $ Category =是
否 - >票应该是什么?
事件 - > $ Category = Incident
RITM - > $ Category = RITM
但是,只有此代码的“是”部分有效。我不确定我是否遗漏了某些东西,或者该函数是否嵌套错误,或者是什么......
# 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
}
答案 0 :(得分:1)
在IncOrRITM
函数中,$Catalog
在分配给它后成为局部变量。你不会返回它的值,所以它会丢失。
You can see a detailed explanation of variable scope in this answer on another question
您应该从函数返回所需的值,然后将$Category
分配给其调用:
function IncOrRITM {
# Other code
if ($DialogResult -eq "OK")
{
if ($RadioButton1.Checked){ $RB1 }
if ($RadioButton2.Checked){ $RB2 }
}
elseif ($DialogResult -eq "Cancel")
{
break
}
}
$Category = IncOrRITM
答案 1 :(得分:0)
将$ global:添加到变量中会修复它。