我假设PSRunspaces使用当前活动的PSSession打开但是当我运行下面的脚本时,我收到错误消息,即New / Set-Mailcontact无法识别命令,就像它们在本地运行一样。 首先,我的想法是错的吗?其次,如果是这样,有没有办法在Office365 PSSession中打开运行空间池?
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
$TimeStamp = Get-Date -Format g
Import-PSSession $session
Connect-MsolService -cred $UserCredential
$contacts = Import-Csv "C:\temp\testgal.csv"
$RunspaceCollection = @()
[Collections.Arraylist]$ScriptResults = @()
$RunspacePool = [RunspaceFactory]::CreateRunspacePool(1, [int]$env:NUMBER_OF_PROCESSORS+1)
$RunspacePool.Open()
$ScriptBlock = {
param($c)
$name = $c.displayName
$rawProxy = $c.proxyAddresses
$proxysplit = $rawproxy -split '(?<!\\);'
$proxyquoted = $proxysplit.replace('x500','"x500').replace('x400','"x400').replace('X500','"X500').replace('X400','"X400')
$proxy = $proxyquoted
New-MailContact -ExternalEmailAddress $c.Mail -Name "`"$name`"" -Alias $c.mailNickname -DisplayName $name -FirstName $c.givenName -Initials $c.initials -LastName $c.sn
Set-MailContact -Identity $c.mailNickname -CustomAttribute1 "CreatedWithScript" -CustomAttribute3 $c.extensionAttribute3 -EmailAddresses $proxy
Set-Contact -Identity $c.mailNickname -City $c.l -Company $c.company -Department $c.department -Office $c.physicalDeliveryOfficeName -Phone $c.telephoneNumber -PostalCode $c.postalCode -Title $c.title
return $error[0]
}
Foreach ($c in $contacts) {
$Powershell = [PowerShell]::Create().AddScript($ScriptBlock).AddArgument($c)
$Powershell.RunspacePool = $RunspacePool
[Collections.Arraylist]$RunspaceCollection += New-Object -TypeName PSObject -Property @{
Runspace = $PowerShell.BeginInvoke()
PowerShell = $PowerShell
}
}
While($RunspaceCollection) {
Foreach ($Runspace in $RunspaceCollection.ToArray()) {
If ($Runspace.Runspace.IsCompleted) {
[void]$ScriptResults.Add($Runspace.PowerShell.EndInvoke($Runspace.Runspace))
$Runspace.PowerShell.Dispose()
$RunspaceCollection.Remove($Runspace)
}
}
}
Write-Host "Results of the scriptlogged to C:\temp\Error_Log.txt..."
$ScriptResults | Out-File 'C:\temp\Error_Log.txt' -Append