带变量的Powershell的XAML GUI
超级用户的一位成员让我到this guide上创建一个PowerShell脚本的GUI,但是我对这个领域很陌生,所以我需要一些关于编码的指导。带变量的Powershell的XAML 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的输出,但我怎么能做到这一点?
PowerShell的我想在第1步
$credentials = get-credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri URL -Credential $credentials –AllowRedirection
Import-PSSession $Session
set-ADServerSettings -ViewEntireForest $true
Enable-RemoteMailbox -Identity [email protected] -RemoteRoutingAddress [email protected]
Enable-RemoteMailbox -Identity [email protected] -Archive
步骤2
$msolcred = get-credential
connect-msolservice -credential $msolcred
Set-MsolUser -UserPrincipalName [email protected] -UsageLocation GB
$LicOpt = New-MsolLicenseOptions -AccountSkuId company:STANDARDPACK -DisabledPlans MCOSTANDARD
Set-MsolUserLicense -UserPrincipalName [email protected] -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_*
添加事件侦听器,Register-ObjectEvent
并没有为我工作。在回调中,我只是将选定的选项分配到TextBox
,您将希望对选定的值进行区分并执行相应的操作。
整体代码是否正确?我不熟悉脚本和编码,所以我对一些术语有点不熟悉。我是否使用FindName或GUI部件来改变PS脚本? –
@DanielNeocleous:看起来没问题,虽然我通常不使用PowerShell ... –
@DanielNeocleous:增加了一些代码。 –