连接到 microsoft.Exchange Office365 的脚本在中途超时并再次提示输入凭据

script that connects to microsoft.Exchange Office365 times out halfway through and prompts for credentials again

客户要求我开发一个连接到 Office365 exchange 并扫描所有用户的脚本,以确保应用程序的某些邮箱可以访问它们。我已经开发了脚本并且它可以工作,但是在脚本进行到一半时它再次要求提供凭据。它说它正在为隐式远程处理创建一个新会话。

我尝试创建一个 New-PSSessionOption -idleTimeout 1200000,它将超时设置为大约 2 小时。那没用

虽然我的凭据对象配置不正确,但是当我将 $userCredential 变量设置为 (get-credential) 时,它会做同样的事情。

$username = ""
$pwdTxt = gc .\SecureStringPassword.txt
$securePwd = $pwdTxt | ConvertTo-SecureString
$UserCredential = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $securePwd
$so = New-PSSessionOption -IdleTimeout 1200000
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -SessionOption $so -Authentication Basic -AllowRedirection

import-PSSession $session

$mailboxes = get-mailbox -ResultSize Unlimited

foreach ($m in $mailboxes) {
"working on $m"

$SmtpAddress = $m.PrimarySmtpAddress;
$Calendar = $SmtpAddress + ":\Calendar"

Add-MailboxPermission -Identity $m.Alias -User mitel-unified-messaging@contoso.com -AccessRights FullAccess -inheritanceType All -confirm:$false;

Add-RecipientPermission $m.Alias -AccessRights SendAs -Trustee mitel-unified-messaging@contoso.com -confirm:$false

Add-MailboxFolderPermission -Identity $calendar -User courtalert -AccessRights Author -confirm:$false ;

Add-MailboxPermission -Identity $m.Alias -user svcItrezzo@contoso.com -AccessRights FullAccess -inheritanceType All -confirm:$false;

write-host "done with $m" -Foregroundcolor Green
}

Exit-PSSession

预期结果 - 脚本完全运行而不提示输入凭据

实际结果 - 脚本提示输入凭据,通常在大约 2 分钟内。

我想通了。它与我的代码没有太大关系,但我也对代码进行了一些更改。

首先,问题出在网络上。客户端的环境正在将所有端口 80/443 流量重定向到他们的 websense firewall/loadbalancer。这就是导致连接断开的原因一旦我提供了异常,脚本 运行 顺利。

但是,有几次脚本不喜欢我正在做的事情,它会停止会话这是因为

中的一个参数
import-PSSession $session

一旦我将其更改为

Import-PSSession $session -AllowClobber

默认情况下,Import-PSSession 导入所有命令,但与当前会话中的命令同名的命令除外。要导入所有命令,请使用 AllowClobber 参数。

毕竟,脚本运行顺利。

而不是使用……

Import-PSSession $session -AllowClobber

…有时会变得笨拙。考虑改用前缀,这样你就知道你什么时候是 运行 EXO vs EXP.

    $o365Cred = Get-Credential
    $ExoSession = New-PSSession -ConfigurationName Microsoft.Exchange `
    -ConnectionUri https://ps.outlook.com/powershell/ `
    -Credential $o365Cred `
    -Authentication Basic `
    -AllowRedirection
    Import-PSSession $ExoSession -Prefix Exo


    $ExpSession = New-PSSession -ConfigurationName Microsoft.Exchange `
    -ConnectionUri http://mail.$env:USERDNSDOMAIN/PowerShell/ `
    -Authentication Kerberos
    Import-PSSession $ExpSession -Prefix Exp

这样你就不会踩到东西了。然而,这个 doe 意味着当您使用 cmdlet 时,必须使用前缀。

Get-ExoMailbox
Get-ExpMailbox

详细信息在帮助文件中:

Import-PSSession

-Prefix Specifies a prefix to the nouns in the names of imported commands.

Use this parameter to avoid name conflicts that might occur when different commands in the session have the same name.

For instance, if you specify the prefix Remote and then import a Get-Date cmdlet, the cmdlet is known in the session as Get-RemoteDate, and it is not confused with the original Get-Date

例子: Adding Exchange Shell items to PowerShell ISE