启动作业连接到 Exchange Online

发布于 2025-01-20 05:04:57 字数 599 浏览 1 评论 0原文

我需要针对100个邮箱运行并行搜索邮件盒CMDLET,以删除内容,但是它们需要首先符合某些参数,例如启用某些CAS协议和存在的转发地址。我还将其进行了参数化,因此我可以将$ maxjobcount int传递给它,以便跑步者可以指定最大数量的同时运行作业以允许以说明其机器上的资源。

让工作正常,然后到达起始工作组件,这是一个非常简单的功能。

function _StartJob {
    param (
        $mailAddress
    )
    Start-Job -Name $mailAddress -Scriptblock { 
        Get-EXOMailbox $mailAddress -PropertySets Delivery 
    }
}

这是一个错误,说我需要在使用CMDLET之前运行Connect-ExChangeOnline,这是我在启动工作中学习脚本块实际上是新的powershell.exe进程,所以不要继承模块和会话选项。

有人知道解决这个问题吗?在MFA环境中,这意味着要坐在那里并在几百次中粘贴密码,要么说服变更板和SECOPS部门让我设置具有删除权限的图形应用程序...两者都痛苦地

感谢您的任何建议

I need to run parallel Search-Mailbox cmdlets against 100's mailboxes to delete the content but they need to fit certain parameters first like certain CAS protocols enabled and a forwarding address present. I've also parameterised it so I can pass a $maxJobCount int to it so the runner can specify a maximum number of concurrently running jobs to allow so as to account for resources on their machine.

Got the thing working then got to the start-job component which is a pretty simple function.

function _StartJob {
    param (
        $mailAddress
    )
    Start-Job -Name $mailAddress -Scriptblock { 
        Get-EXOMailbox $mailAddress -PropertySets Delivery 
    }
}

That's returning an error saying I need to run Connect-ExchangeOnline before using the cmdlets which is where I learned script blocks in Start-Job are actually new PowerShell.exe processes so don't inherit modules and session options.

Does anyone know an easier way around this? In an MFA environment, it either means sitting there and pasting the password in a few hundred times or convincing the Change board and Secops dept to let me setup a graph application with delete rights... both painful

Thanks for any advice

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画骨成沙 2025-01-27 05:04:57

您只需要根据需要将信用传递到块中。

$kvCertName = 'Cert'
#I am using azure automation here to get the cert its different for keyvault
$kvCertPFX = Get-AutomationCertificate -Name $kvCertName
$tenantid = 'yourcompany.onmicrosoft.com'
$appid = '00000000-46da-6666-5555-33333cfe77ec'
$startDate = ([datetime]::Today).AddDays(-7)

#Build the script block
$block = {
    Param(
        $kvCert,
        $appID,
        $tenantID,
        $n,
        $startdate
    )
    $newCertPFX = [System.Security.Cryptography.X509Certificates.X509Certificate2]::new($kvCert)
    Connect-ExchangeOnline -Certificate ([System.Security.Cryptography.X509Certificates.X509Certificate2]$newCertPFX) -AppID $appID -Organization $tenantID -ErrorAction Stop
    Search-AdminAuditLog -StartDate $startDate.adddays($n) -EndDate $($startDate.AddDays($n) | get-date -Hour 23 -Minute 59 -Second 59) -ExternalAccess:$false -ResultSize 250000
    Disconnect-ExchangeOnline -confirm:$false
}


#Remove all jobs created
Get-Job | Remove-Job


#Run All the Parrallel Jobs
$num = 0..6
$kvCert = $kvCertPFX.Export(3)
foreach($n in $num){Start-Job -Scriptblock $Block -ArgumentList @($kvCert,$appID,$tenantid,$n,$startdate)}


#Wait for all jobs to finish.
do {start-sleep 1}
until ($(Get-Job -State Running).count -eq 0)


#Get information from each job.
$adminPowerShellAuditLog = $null
foreach($job in Get-Job){$adminPowerShellAuditLog+= Receive-Job -Id ($job.Id)}

Write-Output $adminPowerShellAuditLog

You just have to pass in the creds into the block however you want.

$kvCertName = 'Cert'
#I am using azure automation here to get the cert its different for keyvault
$kvCertPFX = Get-AutomationCertificate -Name $kvCertName
$tenantid = 'yourcompany.onmicrosoft.com'
$appid = '00000000-46da-6666-5555-33333cfe77ec'
$startDate = ([datetime]::Today).AddDays(-7)

#Build the script block
$block = {
    Param(
        $kvCert,
        $appID,
        $tenantID,
        $n,
        $startdate
    )
    $newCertPFX = [System.Security.Cryptography.X509Certificates.X509Certificate2]::new($kvCert)
    Connect-ExchangeOnline -Certificate ([System.Security.Cryptography.X509Certificates.X509Certificate2]$newCertPFX) -AppID $appID -Organization $tenantID -ErrorAction Stop
    Search-AdminAuditLog -StartDate $startDate.adddays($n) -EndDate $($startDate.AddDays($n) | get-date -Hour 23 -Minute 59 -Second 59) -ExternalAccess:$false -ResultSize 250000
    Disconnect-ExchangeOnline -confirm:$false
}


#Remove all jobs created
Get-Job | Remove-Job


#Run All the Parrallel Jobs
$num = 0..6
$kvCert = $kvCertPFX.Export(3)
foreach($n in $num){Start-Job -Scriptblock $Block -ArgumentList @($kvCert,$appID,$tenantid,$n,$startdate)}


#Wait for all jobs to finish.
do {start-sleep 1}
until ($(Get-Job -State Running).count -eq 0)


#Get information from each job.
$adminPowerShellAuditLog = $null
foreach($job in Get-Job){$adminPowerShellAuditLog+= Receive-Job -Id ($job.Id)}

Write-Output $adminPowerShellAuditLog
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文