Powershell驱动的WPF应用程序未执行某些代码(写输出未按预期工作)

时间:2019-03-30 20:43:14

标签: wpf powershell user-interface

创建一个由Powershell支持的WPF向导,以获取输入参数并执行外部脚本。我的想法是将这个脚本放入一个更大的脚本中,该脚本接受输出(布尔值),并基于out执行/不执行。

底部附近部分的

($ wpf.ConfirmFinish.add_Click)单击完成按钮时,gui将关闭并写入输出。似乎只接受仅与WPF本身相关的命令和参数,并且不会在* add_Checked()或* add.UnChecked()方法上更新局部变量。在自己的PS窗口中将$ props设置为$ true或$ false并进行write-Output时,它将从此脚本中产生预期的结果。

function Get-XamlObject
{
[CmdletBinding()]
param (
    [Parameter(Position = 0,
               Mandatory = $true,
               ValuefromPipelineByPropertyName = $true,
               ValuefromPipeline = $true)]
    [Alias("FullName")]
    [System.String[]]$Path
)

BEGIN
{
    Set-StrictMode -Version Latest

    $wpfObjects = @{ }
    Add-Type -AssemblyName presentationframework, presentationcore

} #BEGIN

PROCESS
{
    try
    {
        foreach ($xamlFile in $Path)
        {
            #Change content of Xaml file to be a set of powershell GUI objects
            $inputXML = Get-Content -Path $xamlFile -ErrorAction Stop
            $inputXMLClean = $inputXML -replace 'mc:Ignorable="d"', '' -replace "x:N", 'N' -replace 'x:Class=".*?"', '' -replace 'd:DesignHeight="\d*?"', '' -replace 'd:DesignWidth="\d*?"', ''
            [xml]$xaml = $inputXMLClean
            $reader = New-Object System.Xml.XmlNodeReader $xaml -ErrorAction Stop
            $tempform = [Windows.Markup.XamlReader]::Load($reader)

            #Grab named objects from tree and put in a flat structure
            $namedNodes = $xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]")
            $namedNodes | ForEach-Object {

                $wpfObjects.Add($_.Name, $tempform.FindName($_.Name))

            } #foreach-object
        } #foreach xamlpath
    } #try
    catch
    {
        throw $error[0]
    } #catch
} #PROCESS

END
{
    Write-Output $wpfObjects
} #END
}

$wpf = Get-ChildItem -Path * -Filter *.xaml -file | Where-Object { $_.Name -ne 'App.xaml' } | Get-XamlObject
#endregion

$wpf.WizardStart.NavigationService.Navigate($wpf.StartPage) | Out-Null

$wpf.StartNext.add_Click({
    $wpf.WizardStart.NavigationService.Navigate($wpf.AdvancedPage)
})

$wpf.AdvancedNext.add_Click({
    $wpf.WizardStart.NavigationService.Navigate($wpf.ConfirmPage)
})

$wpf.AdvancedBack.add_Click({
    $wpf.WizardStart.NavigationService.Navigate($wpf.StartPage)
})

$wpf.ConfirmBack.add_Click({
    $wpf.WizardStart.NavigationService.Navigate($wpf.AdvancedPage)
})
#radio buttons
if($wpf.managedE.IsChecked){
    $wpf.textESET.text = $wpf.managedE.Content
}

$wpf.managedE.add_Checked({
    $wpf.textESET.text = $this.content
})

$wpf.managedNo.add_Checked({
    $wpf.textESET.text = $this.content
})

$wpf.standardApps.add_Checked({
    $wpf.textESET.text = $this.content
})

$wpf.noApps.add_Checked({
    $wpf.textESET.text = $this.content
})

if($wpf.officePro.IsChecked){
    $wpf.textOffice.text = $wpf.officePro.Content
}

$wpf.officePro.add_Checked({
    $wpf.textOffice.text = $this.content
})

$wpf.officeBiz.add_Checked({
    $wpf.textOffice.text = $this.content
})

$wpf.noOffice.add_Checked({
    $wpf.textOffice.text = $this.content
})
# and checkboxes

if($wpf.automate.IsChecked){
    $wpf.textLT.text = 'Automate Enabled'
}

$wpf.automate.add_Checked({
    $wpf.textLT.text = 'Automate Enabled'
})

$wpf.automate.add_UnChecked({
    $wpf.textLT.text = ''
})

if($wpf.screenConnect.IsChecked){
    $wpf.textSC.text = 'ScreenConnect Enabled'
}

$wpf.screenConnect.add_Checked({
    $wpf.textSC.text = 'ScreenConnect Enabled'
})

$wpf.screenConnect.add_UnChecked({
    $wpf.textSC.text = ''
})

if($wpf.qualityChrome.IsChecked){
    $wpf.textQC.text = 'Quality Chrome Settings Enabled'
}

$wpf.qualityChrome.add_Checked({
    $wpf.textQC.text = 'Quality Chrome Settings Enabled'
})

$wpf.qualityChrome.add_UnChecked({
    $wpf.textQC.text = ''
})

if($wpf.defaultApps.IsChecked){
    $wpf.textDAA.text = 'Default App Associations Enabled'
}

$wpf.defaultApps.add_Checked({
    $wpf.textDAA.text = 'Default App Associations Enabled'
})

$wpf.defaultApps.add_UnChecked({
    $wpf.textDAA.text = ''
})

if($wpf.niceStart.IsChecked){
    $wpf.textNS.text = 'Nice Start and TaskBar Enabled'
}

$wpf.niceStart.add_Checked({
    $wpf.textNS.text = 'Nice Start and TaskBar Enabled'
})

$wpf.niceStart.add_UnChecked({
    $wpf.textNS.text = ''
})

if($wpf.windowsPrint.IsChecked){
    $wpf.textPM.text = 'Disable Windows Print Management'
}

$wpf.windowsPrint.add_Checked({
    $wpf.textPM.text = 'Disable Windows Print Management'
})

$wpf.windowsPrint.add_UnChecked({
    $wpf.textPM.text = ''
})

if($wpf.networkFile.IsChecked){
    $wpf.textNFE.text = 'Remove Network From File Explorer Enabled'
}

$wpf.networkFile.add_Checked({
    $wpf.textNFE.text = 'Remove Network From File Explorer Enabled'
})

$wpf.networkFile.add_UnChecked({
    $wpf.textNFE.text = ''
})

if($wpf.chromeWhitelist.IsChecked -eq $False){
    $wpf.textCW.text = ''
}

$wpf.chromeWhitelist.add_Checked({
    $wpf.textCW.text = 'Chrome Whitelist Enabled'
})

$wpf.chromeWhitelist.add_UnChecked({
    $wpf.textCW.text = ''
})

$wpf.ConfirmFinish.add_Click({
    $props = @{ 'Managed ESET' = $wpf.managedE.IsChecked;
                'Manged No ESET' = $wpf.managedNo.IsChecked;
                'Standard Apps' = $wpf.standardApps.IsChecked;
                'No Apps' = $wpf.noApps.IsChecked;
                'Office ProPlus' = $wpf.officePro.IsChecked;
                'Office Business' = $wpf.officeBiz.IsChecked;
                'No Office' = $wpf.noOffice.IsChecked;
                'Quality Chrome' = $wpf.qualityChrome.IsChecked;
                'Default App Ass.' = $wpf.defaultApps.IsChecked;
                'Nice Start' = $wpf.niceStart.IsChecked;
                'Windows Print MGMT' = $wpf.windowsPrint.IsChecked;
                'No Network File Xpl' = $wpf.networkFile.IsChecked;
                'Chrome Whitelist' = $wpf.chromeWhitelist.IsChecked}
$obj = New-Object -TypeName psobject -Property $props
Write-Output $obj
})
$wpf.WizardWindow.Showdialog() | Out-Null

我期望这种格式的管道输出:

    Default App Ass.    : True
    No Office           : False
    Quality Chrome      : True
    Office ProPlus      : True
    No Apps             : False
    Manged No ESET      : False
    Chrome Whitelist    : False
    No Network File Xpl : True
    Office Business     : False
    Nice Start          : True
    Windows Print MGMT  : True
    Standard Apps       : False
    Managed ESET        : True

但是,我没有输出。 Write-Host确实将这些变量正确地写入了控制台,但是Write-Output却不产生任何结果。

我认为这是代码的麻烦所在:

$wpf.ConfirmFinish.add_Click({
    $props = @{ 'Managed ESET' = $wpf.managedE.IsChecked;
                'Manged No ESET' = $wpf.managedNo.IsChecked;
                'Standard Apps' = $wpf.standardApps.IsChecked;
                'No Apps' = $wpf.noApps.IsChecked;
                'Office ProPlus' = $wpf.officePro.IsChecked;
                'Office Business' = $wpf.officeBiz.IsChecked;
                'No Office' = $wpf.noOffice.IsChecked;
                'Quality Chrome' = $wpf.qualityChrome.IsChecked;
                'Default App Ass.' = $wpf.defaultApps.IsChecked;
                'Nice Start' = $wpf.niceStart.IsChecked;
                'Windows Print MGMT' = $wpf.windowsPrint.IsChecked;
                'No Network File Xpl' = $wpf.networkFile.IsChecked;
                'Chrome Whitelist' = $wpf.chromeWhitelist.IsChecked}
    $obj = New-Object -TypeName psobject -Property $props
    Write-Output $obj
    })
$wpf.WizardWindow.Showdialog() | Out-Null

0 个答案:

没有答案