[英]Powershell/Xaml GUI - Variables, Controls, Dynamic Scripts, etc
[英]XAML GUI to Powershell with variables
超级用户的一名同伴让我进入了有关创建Powershell脚本GUI的指南 ,但是我是该领域的新手,因此我需要一些编码方面的指导。
这是Visual Studio 2015生成的XAML代码。
<Window x:Name="Title" x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="New Mailbox" Height="400" Width="420">
<Grid>
<Image x:Name="image" HorizontalAlignment="Left" Height="100" Margin="14,10,0,0" VerticalAlignment="Top" Width="386" Source="C:\Users\Daniel Neocleous\Documents\Visual Studio 2015\Projects\WpfApplication1\WpfApplication1\Images\SibelcoLogo.png"/>
<RadioButton x:Name="radioButton" Content="Step 1" HorizontalAlignment="Left" Margin="150,125,0,0" VerticalAlignment="Top"/>
<RadioButton x:Name="radioButton_Copy" Content="Step 2" HorizontalAlignment="Left" Margin="217,125,0,0" VerticalAlignment="Top"/>
<Button x:Name="button" Content="Create Mailbox" HorizontalAlignment="Left" Margin="150,152,0,0" VerticalAlignment="Top" Width="119" Height="35"/>
<GroupBox x:Name="groupBox" Header="Output" HorizontalAlignment="Left" Height="169" Margin="10,192,0,0" VerticalAlignment="Top" Width="394">
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="147" Margin="0,0,-1.143,-0.714" TextWrapping="Wrap" VerticalAlignment="Top" Width="384"/>
</GroupBox>
</Grid>
现在,根据我的理解,我必须通过删除x和x:Class="WpfApplication1.MainWindow"
字符串来修改此代码,并将以下内容添加到末尾;
#Read XAML
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
catch{Write-Host "Unable to load Windows.Markup.XamlReader. Some possible causes for this problem include: .NET Framework is missing PowerShell must be launched with PowerShell -sta, invalid XAML code was encountered."; exit}
#===========================================================================
# Store Form Objects In PowerShell
#===========================================================================
$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name)}
#===========================================================================
# Shows the form
#===========================================================================
$Form.ShowDialog() | out-null
尽管假设我上面的内容是正确的,但我对这里有些困惑。 如何将所有这些与Gui绑定?
有两个单选按钮,并且要选择哪个按钮,我希望运行其他脚本,并且Powershell输出将显示在底部的文本框中,但是我该如何完成呢?
我要在步骤1上运行的Powershell
$credentials = get-credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri URL -Credential $credentials –AllowRedirection
Import-PSSession $Session
set-ADServerSettings -ViewEntireForest $true
Enable-RemoteMailbox -Identity test@test.com -RemoteRoutingAddress test@test.com.onmicrosoft.com
Enable-RemoteMailbox -Identity test@test.com -Archive
第2步
$msolcred = get-credential
connect-msolservice -credential $msolcred
Set-MsolUser -UserPrincipalName test@test.com -UsageLocation GB
$LicOpt = New-MsolLicenseOptions -AccountSkuId company:STANDARDPACK -DisabledPlans MCOSTANDARD
Set-MsolUserLicense -UserPrincipalName test@test.com -AddLicenses company:STANDARDPACK -LicenseOptions $LicOpt
Remove-PSSession $Session
感谢您的建议。
您的$Form
应该是一个Window
对象,因此应支持其所有方法,因此要对某些事件作出反应,您必须读取控件的状态。
由于这是基于脚本的,因此我只需命名所有相关部分并使用FindName
搜索(您的脚本显然已经为所有命名控件创建了powershell变量)。 然后,您可以访问IsChecked
之类的属性,以区分选择了哪个RadioButton
。 要收听按钮单击,您可能需要Register-ObjectEvent
。 使用ShowDialog
将有一个阻塞调用,当设置DialogResult
时将返回该阻塞调用,因此应在事件侦听器中进行设置。
编辑:我只是玩这个,这是一个显示基本过程的小样本。
ScriptWindow.xaml
<Window x:Name="Title"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel>
<ListBox x:Name="selection" SelectionMode="Single">
<ListBox.ItemTemplate>
<DataTemplate>
<RadioButton IsChecked="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=ListBoxItem}}"
Content="{Binding}"/>
</DataTemplate>
</ListBox.ItemTemplate>
<sys:String>Step 1</sys:String>
<sys:String>Step 2</sys:String>
</ListBox>
<Button x:Name="run">Run</Button>
<GroupBox Header="Output">
<TextBox x:Name="output" IsReadOnly="True"/>
</GroupBox>
</StackPanel>
</Window>
ScriptWindow.ps1
Add-Type –assemblyName PresentationFramework
Add-Type –assemblyName PresentationCore
Add-Type –assemblyName WindowsBase
$xaml="ScriptWindow.xaml"
$xmlReader=(New-Object System.Xml.XmlTextReader $xaml)
$app=(New-Object System.Windows.Application)
$form=[Windows.Markup.XamlReader]::Load( $xmlReader )
$doc=(New-Object System.Xml.XmlDocument)
$doc.Load($xaml)
$run = $form.FindName("run")
$selection = $form.FindName("selection")
$output = $form.FindName("output")
$run.Add_Click({ $output.Text = $selection.SelectedItem })
$app.Run($form)
显然,您也可以通过Add_*
添加事件侦听Add_*
, Register-ObjectEvent
对我不起作用。 在回调中,我只是将选定的选项分配给TextBox
,您需要对选定的值进行区分大小写并执行各自的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.