我正在处理一个 WPF Powershell 项目(在执行过程中学习并合并相关任务)。我有两个复选框,用于切换通讯组上的外部发件人身份验证。
我希望,如果您想将其设置为允许外部,请勾选该框,然后在文本框中输入组,然后单击按钮来处理更改。然后,它将在文本框中显示一条消息,表示已完成。
我遇到的问题是代码似乎可以运行,但是没有进行任何更改,因此它没有选择选定的复选框也没有实际处理代码。事实上,当我让第一部分工作时,我就能弄清楚它不显示消息的事实。
另一个复选框允许您将其设置为不接收外部(如果它已经允许)。
以下是我目前正在尝试的功能。它似乎执行了,Visual Studio 调试菜单中没有显示错误。
function SetExternalSenderClick
{
param($sender, $e)
#Script prompts for Creds
$PSCred = Get-Credential
#Script connects to Exchange On-Prem
$OnPremExSession = New-PSSession -Credential $PSCred -ConfigurationName Microsoft.Exchange -ConnectionUri http://************/PowerShell/ -Authentication Kerberos
import-pssession $OnPremExSession
if ($SetExternalTrue.IsChecked){
$DG = $ExternalDGNameText.text
Get-DistributionGroup -identity $DG | Set-DistributionGroup -RequireSenderAuthenticationEnabled $False
#Clear the textbox before adding new results
$DGChangesResultsText.Clear()
#Append text to textbox to confrim addition
$DGChangesResultsText.AppendText("External emails set to allow. ")
}
elseif ($SetExternalFalse.IsChecked){
$DG1 = $ExternalDGNameText.text
Get-DistributionGroup -identity $DG1 | Set-DistributionGroup -RequireSenderAuthenticationEnabled $True
#Clear the textbox before adding new results
$DGChangesResultsText.Clear()
#Append text to textbox to confrim addition
$DGChangesResultsText.AppendText("External emails set to deny ")
}
Remove-PSSession $OnPremExSession
}
最小工作示例
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Label Content="DG Names
" HorizontalAlignment="Left" Margin="42,38,0,0" VerticalAlignment="Top" Width="92" Height="27"/>
<Button x:Name="SetExternalSender" Content="Set External Sender" HorizontalAlignment="Left" Margin="59,144,0,0" VerticalAlignment="Top" Width="141" Click="SetExternalSenderClick"/>
<TextBox x:Name="ExternalDGName" HorizontalAlignment="Left" Height="23" Margin="139,42,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="177"/>
<CheckBox x:Name="SetExternalTrue" Content="Set External True" HorizontalAlignment="Left" Margin="59,85,0,0" VerticalAlignment="Top" Width="118" Checked="CheckBox_Checked"/>
<CheckBox x:Name="SetExternalFalse" Content="Set External False" HorizontalAlignment="Left" Margin="59,105,0,0" VerticalAlignment="Top" Width="118" Checked="CheckBox_Checked"/>
<TextBox x:Name="DGChangesResultsText" HorizontalAlignment="Left" Height="75" Margin="59,200,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="335"/>
</Grid>
</Window>
PS码
function Add-ControlVariables {
New-Variable -Name 'SetExternalSender' -Value $window.FindName('SetExternalSender') -Scope 1 -Force
New-Variable -Name 'ExternalDGName' -Value $window.FindName('ExternalDGName') -Scope 1 -Force
New-Variable -Name 'DGChangesResultsText' -Value $window.FindName('DGChangesResultsText') -Scope 1 -Force
}
[System.Reflection.Assembly]::LoadWithPartialName("PresentationFramework") | Out-Null
function Import-Xaml {
[xml]$xaml = Get-Content -Path $PSScriptRoot\WpfWindow1.xaml
$manager = New-Object System.Xml.XmlNamespaceManager -ArgumentList $xaml.NameTable
$manager.AddNamespace("x", "http://schemas.microsoft.com/winfx/2006/xaml");
$xaml.SelectNodes("//*[@x:Name='SetExternalSender']", $manager)[0].RemoveAttribute('Click')
$xamlReader = New-Object System.Xml.XmlNodeReader $xaml
[Windows.Markup.XamlReader]::Load($xamlReader)
}
function Set-EventHandler {
$SetExternalSender.add_Click({
param([System.Object]$sender,[System.Windows.RoutedEventArgs]$e)
SetExternalSenderClick -sender $sender -e $e
})
}
#ERROR: You need to define the x:Name attribute for this control to generate an event handler.
$window = Import-Xaml
Add-ControlVariables
Set-EventHandler
function SetExternalSenderClick
{
param($sender, $e)
#Script prompts for Creds
$PSCred = Get-Credential
#Script connects to Exchange On-Prem
$OnPremExSession = New-PSSession -Credential $PSCred -ConfigurationName Microsoft.Exchange -ConnectionUri http://************/PowerShell/ -Authentication Kerberos
import-pssession $OnPremExSession
if ($SetExternalTrue.IsChecked){
$DG = $ExternalDGNameText.text
Get-DistributionGroup -identity $DG | Set-DistributionGroup -RequireSenderAuthenticationEnabled $False
#Clear the textbox before adding new results
$DGChangesResultsText.Clear()
#Append text to textbox to confrim addition
$DGChangesResultsText.AppendText("External emails set to allow. ")
}
elseif ($SetExternalFalse.IsChecked){
$DG1 = $ExternalDGNameText.text
Get-DistributionGroup -identity $DG1 | Set-DistributionGroup -RequireSenderAuthenticationEnabled $True
#Clear the textbox before adding new results
$DGChangesResultsText.Clear()
#Append text to textbox to confrim addition
$DGChangesResultsText.AppendText("External emails set to deny ")
}
Remove-PSSession $OnPremExSession
}
$window.ShowDialog()
如此简单的事情最终实现了我想要的......
$GroupNameText.text = $UserGroupResults.SelectedItem
将此行添加到点击功能中并且有效。