首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell UI - DialogResult - PropertyAssignmentException

Powershell UI - DialogResult - PropertyAssignmentException
EN

Stack Overflow用户
提问于 2022-01-21 07:58:10
回答 1查看 183关注 0票数 0

我正在为某些脚本创建一个powershell UI。

现在我跑错了。但首先,这是我的表格代码:

代码语言:javascript
复制
$form_appBase = New-Object System.Windows.Forms.Form
        $form_appBase.Text = 'APP Policy creator'
        $form_appBase.Width = 350
        $form_appBase.Height = 150
        $form_appBase.AutoSize = $true

        $label_appFilename = New-Object System.Windows.Forms.Label
        $label_appFilename.Location = '10,10'
        $label_appFilename.Size = '200,15'
        $label_appFilename.Text = 'Program'

        $Textbox_appFilename = New-Object System.Windows.Forms.TextBox
        $Textbox_appFilename.Location = '10,30'
        $Textbox_appFilename.Size = '200,25'

        $label_appVersion = New-Object System.Windows.Forms.Label
        $label_appVersion.Location = '220,10'
        $label_appVersion.Size = '100,15'
        $label_appVersion.Text = 'Version:'

        $Textbox_appVersion = New-Object System.Windows.Forms.TextBox
        $Textbox_appVersion.Location = '220,30'
        $Textbox_appVersion.Size = '100,25'

        $label_appProgPath = New-Object System.Windows.Forms.Label
        $label_appProgPath.Location = '10,60'
        $label_appProgPath.Size = '130,15'
        $label_appProgPath.Text = 'Path to PROG Policy'

        $textbox_appProgPath = New-Object System.Windows.Forms.TextBox
        $textbox_appProgPath.Location = '150,60'
        $textbox_appProgPath.Size = '170,25'

        $Radio_appBaseSW = New-Object System.Windows.Forms.RadioButton
        $Radio_appBaseSW.Location = '10,90'
        $Radio_appBaseSW.Size = '100,15'
        $Radio_appBaseSW.Text = "Software"
        $Radio_appBaseSW.DialogResult = [System.Windows.Forms.DialogResult]::SW

        $Radio_appBaseHW = New-Object System.Windows.Forms.RadioButton
        $Radio_appBaseHW.Location = '10,110'
        $Radio_appBaseHW.Size = '100,15'
        $Radio_appBaseHW.Text = "Hardware"
        $Radio_appBaseHW.DialogResult = [System.Windows.Forms.DialogResult]::HW

        $Button_appConfirm = New-Object System.Windows.Forms.Button
        $Button_appConfirm.Location = '220,95'
        $Button_appConfirm.Size = '100,30'
        $Button_appConfirm.Text = "Create"

        $form_appBase.Controls.Add($label_appFilename)
        $form_appBase.Controls.Add($Textbox_appFilename)
        $form_appBase.Controls.Add($label_appVersion)
        $form_appBase.Controls.Add($Textbox_appVersion)
        $form_appBase.Controls.Add($label_appProgPath)
        $form_appBase.Controls.Add($textbox_appProgPath)
        $form_appBase.Controls.Add($Radio_appBaseSW)
        $form_appBase.Controls.Add($Radio_appBaseHW)
        $form_appBase.Controls.Add($Button_appConfirm)

        $form_appBase.AcceptButton = $Button_appConfirm

        $dialogResultSHW = $form_appBase.ShowDialog()

        }

        $Button_appConfirm.Add_click(
        {

        if ($dialogResultSHW -eq "SW"){$appSHW = "SW"}

        elseif ($dialogResultSHW -eq "HW"){

        $appSHW = "HW"

        $appFilename = $Textbox_appFilename.Text
        $appVersion = $Textbox_appVersion.Text
        $appProgPath = $textbox_appProgPath.Text
        $appUNCPath = "\\zen\netlogon\applocker\Output + '\' + $appSHW + '-' + $appFilename + '-' $appVersion + '.' + 'xml' "

        Write-Host "$appFilename"
        Write-Host "$appVersion"
        Write-Host "$appProgPath"
        Write-Host "$appSHW"
        Write-Host "$appUNCPath"

        powershell.exe -file \\zen\netlogon\applocker\applockerwork.ps1 -application -in $appProgPath -out $appUNCPath 
        
        }
        }
        )

检查完所有内容后,我得到以下错误:

错误

有人能帮我找出为什么这个错误仍然存在吗?我尝试了很多我在WWW上发现的想法,但似乎没有什么效果。

EN

回答 1

Stack Overflow用户

发布于 2022-01-21 15:55:09

首先,[System.Windows.Forms.DialogResult]枚举没有像"SW""HW"这样的值。

删除$Radio_appBaseSW.DialogResult = ...$Radio_appBaseHW.DialogResult = ...行,因为作为评论的准则,RadioButtonClass没有这样的属性。

还将$dialogResultSHW = $form_appBase.ShowDialog()转换为[void]$form_appBase.ShowDialog()

然后,在您的$Button_appConfirm.Add_click({..})事件中,执行

代码语言:javascript
复制
$appSHW = if ($Radio_appBaseSW.Checked) { "SW" } elseif ($Radio_appBaseHW.Checked) {"HW"}

并使用$appSHW的值执行不同的操作,或者简单地执行

代码语言:javascript
复制
if ($Radio_appBaseSW.Checked) { 
    # perform action for choice Software
}
elseif ($Radio_appBaseHW.Checked) {
    perform a different action for choice Software
}
else {
    # no radiobutton was touched... Alert the user to select either the Software or the Hardware radio button
}

最后,当您完成$form_appBase.Dispose()的操作时,不要忘记从内存中删除表单。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70798059

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档