【发布时间】:2018-05-03 23:27:52
【问题描述】:
我确信这是一个非常正常的情况,但在某些情况下,我需要循环访问我的客户端并通过 PowerShell 连接到他们的 Exchange Online 基础架构。目前,我这样做:
# Specifying credentials and connecting to Office 365 module
$Credential = Get-Credential
Connect-MsolService -Credential $Credential
# Getting a list of tenant IDs (clients) used to connect to their environment
$Tenants = (Get-MsolPartnerContract).TenantID.Guid
# Running command against all tenants
ForEach ($Tenant in $Tenants) {
# Get primary domain for the tenant
$Domain = (Get-MsolDomain -TenantId $Tenant `
| Where-Object { `
$_.Name -NotLike "*onmicrosoft.com" -and `
$_.Name -NotLike "*microsoftonline.com" })[0].Name
# Authenticating to Exchange Online for the tenant
$Session = New-PSSession `
-ConfigurationName Microsoft.Exchange `
-ConnectionUri https://outlook.office365.com/powershell-liveid?DelegatedOrg=$Domain `
-Credential $Credential `
-Authentication Basic `
-AllowRedirection
Import-PSSession $Session -ErrorAction 'silentlycontinue'
# Logic goes here...
Remove-PSSession $Session
}
这可能是我刚刚在文档中遗漏的内容,但有没有更快的方法来针对多个会话运行命令?目前,仅连接需要很长时间,当连接到许多不同的租户时,这当然会增加。
【问题讨论】:
标签: powershell office365 powershell-remoting