Exchange在线会话和运行空间 [英] Exchange Online Session and Runspace

查看:0
本文介绍了Exchange在线会话和运行空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在运行空间中执行Get-MailboxStatistics。 我可以连接到Exchange Online。如果我执行‘Get-Psession’,我可以看到Exchange会话。但是,我如何将此ExchangeOnline会话传递给运行空间以执行Get-MailboxStatistics。 当前它无法识别运行空间中的Get-MailboxStatistics命令。

以下是我的代码(这是一个更大的脚本的一部分):

# Connecting to Exchange Online
$AdminName = "hil119"
$Pass = "password"
$cred_cloud = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $Pass
Connect-ExchangeOnline -Credential $cred_cloud -Prefix Cloud

# Executing Get-MailboxStatistics in a Runspace
$Runspace = [runspacefactory]::CreateRunspace()
$PowerShell = [powershell]::Create()
$PowerShell.runspace = $Runspace
$Runspace.Open()
[void]$PowerShell.AddScript({Get-MailboxStatistics 'd94589'})
$PowerShell.BeginInvoke()

Exchange

经过几天的研究,我发现您可以在本地系统或远程推荐答案服务器上运行线程。 如果您在本地系统上运行它,则每个线程需要自己调用Exchange会话,但如果您在远程交换系统(onprem或云)上运行它,则只能获取一次交换会话并将该会话传递给该线程。您可以使用Invoke命令获取远程会话。 另外,我最终用Poshjob或Runspace编写了脚本。从我所读到的内容来看,POSHJOBS最终是一种Start-JOB和Runspace的结合。

下面是用于在远程服务器上运行线程的代码片段。使用此脚本,您可以将相同的交换会话传递给所有线程。

Function Func_ConnectCloud
{
$AdminName = "r43667"
$AdminPassSecure = "pass"
$Cred_Cloud = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $AdminPassSecure
Connect-ExchangeOnline -Credential $Cred_Cloud
$CloudSession =  Get-PSSession | Where { $_.ComputerName -like "outlook.office365*"}
Return $CloudSession
}

$script_Remote = 
    {
param(
     $Alias,
     $CloudSession
     )

Invoke-Command -session $CloudSession -ArgumentList $Alias  -ScriptBlock {param($Alias); Get-MailboxStatistics $Alias}
    }

$CloudSession = Func_ConnectCloud
$Alias = 'h672892'
$Job1 = Start-RsJob -Name "job_$Alias" -ScriptBlock $ScriptRemote -ArgumentList $Alias, $CloudSession

Receive-RsJob $Job1
Remove-RsJob $Job1

虽然在云服务器上运行时,Microsoft只允许两个线程,但您可以使用此脚本在prem和Cloud上运行线程。如果您运行两个以上的线程,您的Exchange会话将被终止(这与限制不同)。因此,如果您有一个云环境,那么最好在本地运行您的线程。 在https://powershell.org/forums/topic/connecting-to-office-365-in-psjobs/

的脚本中特别引用@postanote

这篇关于Exchange在线会话和运行空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆