我有一个使用PowerShell的XAML表单。我希望连接到Exchange,以便能够管理Calendar /邮箱权限。
下面是用于尝试引入登录框的脚本片段
Main.ps1脚本
Add-Type -AssemblyName PresentationCore,PresentationFramework,WindowsBase,System.Windows.Forms,WindowsFormsIntegration
. $PSScriptRoot\Functions.ps1
Import-Module -Name 'ExchangeOnlineManagement'
$xaml = Join-Path $PSScriptRoot 'CalWindow.xaml'
$Launch_XAML = [XML](Get-Content $xaml)
$xamlReader_Launch = New-Object System.Xml.XmlNodeReader $Launch_XAML
$Window = [Windows.Markup.XamlReader]::Load($xamlReader_Launch)
$btnConnect = $Window.FindName('btnConnect')
[void]$Window.showDialog()
[void]$window.Activate()
$appContext = New-Object System.Windows.Forms.ApplicationContext
[void][system.windows.forms.application]::Run($appContext)Functions.ps1脚本
function btnConnect {
switch($btnConnect.Content) {
"Connect" {
Connect-ExchangeOnline
$btnConnect.Content = "Disconnect"
$lblStatus.Content = "Connected as "
}
"Disconnect" {
Disconnect-ExchangeOnline -force
$btnConnect.Content = "Connect"
$lblStatus.Content = "Not Connected"
}
}
}只要单击按钮,程序就会出现冻结状态。当向-verbose添加Connect-ExchangeOnline标志时,我得到以下信息
----------------------------------------------------------------------------
The module allows access to all existing remote PowerShell (V1) cmdlets in addition to the 9 new, faster, and more reliable cmdlets.
|--------------------------------------------------------------------------|
| Old Cmdlets | New/Reliable/Faster Cmdlets |
|--------------------------------------------------------------------------|
| Get-CASMailbox | Get-EXOCASMailbox |
| Get-Mailbox | Get-EXOMailbox |
| Get-MailboxFolderPermission | Get-EXOMailboxFolderPermission |
| Get-MailboxFolderStatistics | Get-EXOMailboxFolderStatistics |
| Get-MailboxPermission | Get-EXOMailboxPermission |
| Get-MailboxStatistics | Get-EXOMailboxStatistics |
| Get-MobileDeviceStatistics | Get-EXOMobileDeviceStatistics |
| Get-Recipient | Get-EXORecipient |
| Get-RecipientPermission | Get-EXORecipientPermission |
|--------------------------------------------------------------------------|
To get additional information, run: Get-Help Connect-ExchangeOnline or check https://aka.ms/exops-docs
Send your product improvement suggestions and feedback to exocmdletpreview@service.microsoft.com. For issues related to the module, contact Microsoft support. Don't use the feedback alias for problems or support issues.
----------------------------------------------------------------------------
VERBOSE: ExchangeEnvironment : O365Default
VERBOSE: ConnectionUri :
VERBOSE: AzureADAuthorizationEndpointUri :
VERBOSE: DelegatedOrganization :
VERBOSE: Prefix :
VERBOSE: FormatTypeName :*
VERBOSE: CommandName :*
VERBOSE: Importing cmdlet 'Add-EXOClientTelemetryWrapper'.
VERBOSE: Importing cmdlet 'New-EXOClientTelemetryFilepath'.
VERBOSE: Importing cmdlet 'Push-EXOTelemetryRecord'.
VERBOSE: Importing cmdlet 'Clear-ActiveToken'.
VERBOSE: Importing cmdlet 'New-ExoPSSession'.
VERBOSE: Importing cmdlet 'Test-ActiveToken'.我是否遗漏了一些东西,可以让它与表单一起工作,有时当我关闭窗口时,它会带来登录框,但这是不可靠的。在使用-Credential $creds标志定义凭据时,也会遇到同样的问题。
如果我试图在脚本加载时进行连接,它似乎运行良好,但我需要它处理不同的帐户登录。
发布于 2022-01-19 16:32:18
我也遇到过这个问题。我想我已经把它的范围缩小到了造成它的原因和一些解决办法。
它似乎与ExchangeOnlineManagement模块的2.0.4和2.0.5版本以及在脚本中放置Connect和WindowsWindows5.1有关。我发现使用2.0.3或更早版本并没有这个问题。对于2.0.4和2.0.5,下面是一些测试/确认问题的简单代码:
外接式-AssemblyName System.Windows.Forms $FormMain =新对象System.Windows.Forms.Form连接-ExchangeOnline
问题是“新对象”cmdlet。如果在Connect-ExchangeOnline之前调用它,问题就会发生(在2.0.4和2.0.5中)。如果在“任意”和“所有添加类型的cmdlet”之前调用Connect-ExchangeOnline,那么它将工作。
如果使用PowerShell 7.x,那么它也能工作。
因此,我的建议是使用2.0.3 (但随后使用更新模块发行版的任何新好处)或重新排序代码,以确保您先进行身份验证,然后使用Add。或者切换到PowerShell 7.x。
https://stackoverflow.com/questions/69371555
复制相似问题