Открываются ли пространства выполнения Powershell с использованием текущей сессии PSSession?

Я исходил из того, что PSRunspaces открылись с использованием текущей активной PSSession, но когда я запускаю приведенный ниже сценарий, я получаю сообщение об ошибке, что New/Set-Mailcontact не являются распознанными командами, как если бы они выполнялись локально. Во-первых, я ошибаюсь в своих мыслях? Во-вторых, если да, то есть ли способ открыть пул пространства выполнения в сеансе PSSession Office365?

$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

person A.Gialias    schedule 01.06.2017    source источник
comment
Я нашел следующее stackoverflow.com/questions/31935183/ можно ли это переписать для работы в powershell?   -  person A.Gialias    schedule 01.06.2017
comment
ну, они не делятся состоянием с вашим процессом powershell, поэтому вам нужно войти в o365 в каждой среде выполнения.   -  person 4c74356b41    schedule 01.06.2017