Is it possible to use 2 separate modules inside of a Powershell runspace?

57 Views Asked by At

Is it possible to import the Exchange OnPrem and Exchange Online modules into a single runspace? I've been trying the below and then only receiving the on prem data? I want to ensure that I'm not having to authenticate for each runspace either.

To use the below, you'll need to fill in $smtp = "[email protected]"; and of course your own authentication information.

if ($exoSession -eq $null)
{
    $exo_un = Get-Content "xxx";
    $exo_pw = Get-Content "xxx" | ConvertTo-SecureString;
    $exo_creds = New-Object System.Management.Automation.PSCredential -ArgumentList $exo_un,$exo_pw;
    Connect-ExchangeOnline -Credential $exo_creds -Prefix "Ex";
    $exoSessions =  Get-PSSession | Where { ($_.ComputerName -like "outlook.office365*") -and ($_.State -eq "Opened")};
    $exoSession = $exoSessions[0];
    $exoSessionImported = Import-PSSession -Session $exoSession -AllowClobber -CommandName Get-EXOMailbox, Get-EXOMailboxStatistics, Get-EXOMailboxFolderStatistics, Get-Command;
}

if ($exopSession -eq $null)
{
    $uri = "xxx";
    $authenticationType = "Negotiate";  
    $exopSession = new-pssession -ConfigurationName Microsoft.Exchange -ConnectionUri $uri -Authentication $authenticationType;
    $exopSessionImported = Import-PSSession $exopSession -AllowClobber -CommandName Get-Mailbox, Get-MailboxDatabaseCopyStatus, Get-MailboxDatabase, Get-MailboxStatistics, Get-MailboxFolderStatistics, Get-DatabaseAvailabilityGroup, Get-ExchangeServer, Test-ServiceHealth, Get-Recipient, Get-AdServerSettings, Set-AdServerSettings;
    Set-AdServerSettings -ViewEntireForest:$true;
}    

$maxThreads = 50;
[string[]]$modulePaths = $exoSessionImported.Path, $exopSessionImported.Path;
[void]$iss.ImportPSModule($modulePaths);
$runspacePool = [runspacefactory]::CreateRunspacePool(1, $maxThreads, $iss, $host);
$runspacePool.Open();    

$powershell = [powershell]::Create();
$powershell.RunspacePool = $RunspacePool;   

[void]$powershell.AddScript({       
    Param ($smtp, $exopSessionImported, $exoSessionImported)        
    [pscustomobject]@{

        smtp = $smtp
        exopSessionImported = $exopSessionImported
        exoSessionImported = $exoSessionImported
        
    } | Out-Null
    
    $recipientExo = Get-ExMailbox -Identity $smtp;
    $recipientExop = Get-Recipient -Identity $smtp;
    
    $returnValue = New-Object -TypeName PsObject;
    $returnValue | Add-Member -NotePropertyName Online -NotePropertyValue $recipientExo;
    $returnValue | Add-Member -NotePropertyName Onprem -NotePropertyValue $recipientExop;
    
    Write-Host "Smtp: $($smtp) - $($recipientExo.Name) - $($recipientExop.OrganizationalUnit) - $($exopSessionImported.Name) - $($exoSessionImported.Name)";
    
    return $returnValue;

}) # end of add script

$smtp = "[email protected]";
$powershell.AddParameter('smtp', $smtp) | Out-Null;
$powershell.AddParameter('exopSessionImported', $exopSessionImported) | Out-Null;
$powershell.AddParameter('exoSessionImported', $exoSessionImported) | Out-Null;
$returnVal = $PowerShell.BeginInvoke();
$temp = "" | Select powershell,returnVal;
$temp.powershell = $powershell;
$temp.returnVal = $returnVal;
$endInvoke = $temp.powershell.EndInvoke($temp.returnVal);

Write-Host "";
Write-Host "";
Write-Host "endInvoke:";
$endInvoke;
0

There are 0 best solutions below