客户端要求我开发一个连接到Office365 exchange的脚本,并扫描所有用户,以确保应用程序的某些邮箱能够访问它们。我已经开发了脚本,它可以工作,但是在脚本的一半,它再次要求凭证。它说它正在为隐式远程处理创建一个新的会话。
我尝试创建一个1200000,它将超时设置为大约2小时。那不起作用
然后,我认为我的凭据对象配置不正确,但是当我将$userCredential变量设置为(get-凭据)时,它也会做同样的事情。
$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分钟左右。
发布于 2019-04-23 18:40:32
我解决了问题。这与我的代码没有多大关系,但我也对代码做了一些修改。
首先也是最重要的是,网络的问题。客户端的环境是将所有端口80/443流量重定向到他们的websense防火墙/负载平衡器。这就是导致连接下降的原因,一旦我提供了异常,脚本就会顺利运行。
但是,有几次脚本不喜欢我正在做的事情,它会停止会话,这是因为
import-PSSession $session一旦我把它改成
Import-PSSession $session -AllowClobber默认情况下,导入除与当前会话中的命令具有相同名称的命令之外的所有命令。若要导入所有命令,请使用AllowClobber参数。
在那之后,剧本运行得很顺利。
发布于 2019-04-23 23:54:38
而不是使用…
Import-PSSession $session -AllowClobber…有时会变得很不灵巧。考虑使用前缀,以便您知道何时运行EXO。
$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这样你就不会踩到东西了。然而,这并不意味着当您使用cmdlet时,必须使用前缀。
Get-ExoMailbox
Get-ExpMailbox帮助文件中有详细信息:
-Prefix在导入命令的名称中指定名词的前缀。 使用此参数可避免会话中的不同命令具有相同名称时可能发生的名称冲突。 例如,如果您指定前缀Remote,然后导入一个Get-Date cmdlet,则该cmdlet在会话中称为Get-RemoteDate,并且它不会与原始的Get-Date混淆。
示例:将Exchange项添加到PowerShell ISE
https://stackoverflow.com/questions/55814944
复制相似问题