2017-01-10 127 views
0

我一直在使用Office365许可证跟踪。实际上它看起来不错,但是花费很多时间来完成这个过程。大部分时间都是由Get-MsolUser花费的,它可能会改进并行计算(在处理用户1时,您已经获取用户2的数据,等等......)顺便说一句,我们有大约3000+云用户如何改进脚本的速度?PowerShell对大用户的处理速度很慢 - 有没有更好的方法?

$T1 = @() 
$O365Users = Get-MsolUser -All 
ForEach ($O365User in $O365Users) 
{ 
    $ADuser = Get-ADUser -Filter { UserPrincipalName -eq $O365User.UserPrincipalName } -Properties whenCreated, Enabled, lastlogondate 
    $O365Stats = Get-MailboxStatistics $O365User.DisplayName -ErrorAction SilentlyContinue 
    $O365Smtp = Get-Recipient $O365User.DisplayName -ErrorAction SilentlyContinue 
    If ($O365Stats -and $O365Smtp) { 
    If (($ADUser.Enabled -eq $true) -and ($O365User.isLicensed -eq $true)) 
    { 
     $T1 += New-Object psobject -Property @{ 
      CollectDate = $(Get-Date); 
      ADUserUPN = $($ADUser.UserPrincipalName); 
      O365UserUPN = $($O365User.UserPrincipalName); 
      ADUserCreated = $($ADUser.whenCreated); 
      ADUserEnabled = $($ADUser.Enabled); 
      ADLastLogonDate = $($ADUser.LastLogonDate); 
      O365Licensed = $($O365User.isLicensed); 
      O365LastLogonTime = $($O365Stats.LastLogonTime); 
      O365SMTPAddress = $($O365Smtp.PrimarySMTPAddress) 
     } 
    } 
} 
} 
$T1 = $T1 | Sort-Object -Property ADUserCreated 
$T1 | Format-Table 
$T1 | Export-Csv -Path $OutputFile -NoTypeInformation 
Write-Host "Output to $OutputFile" 
+0

就像你说的,并行出来 – 4c74356b41

+0

感谢你这样的人说我该如何重建我的脚本?请澄清 – Arbelac

回答

0

给你一个与Powershell中的并行关系。

我希望你能通过PS工作流程。

我们有- 平行在那将帮助你并行呼叫。

除此之外,我们有调用并行

一个功能这是链接吧:Invoke-Parallel Function

注:实例函数本身里面提到。您也可以在编译后使用该函数的get-help。

+0

谢谢,我该如何将我的脚本和Invoke-Parallel函数结合起来?我对么 ? Invoke-Parallel -scriptfile myscript.ps1 -runspaceTimeout 10 -throttle 10 – Arbelac

+0

@Arbelac:是的,应该做你的工作...检查出来 –

0

使用管道,早期筛选,避免追加到一个数组应该已经加快速度显着:

Get-MsolUser -All | Where-Object { 
    $_.IsLicensed 
} | ForEach-Object { 
    $upn = $_.UserPrincipalName 
    Get-ADUser -Filter "UserPrincipalName -eq '$upn'" -Properties whenCreated, Enabled, lastlogondate 
} | Where-Object { 
    $_.Enabled 
} | ForEach-Object { 
    $O365Stats = Get-MailboxStatistics $_.DisplayName -ErrorAction SilentlyContinue 
    $O365Smtp = Get-Recipient $_.DisplayName -ErrorAction SilentlyContinue 
    if ($O365Stats -and $O365Smtp) { 
     New-Object -Type PSObject -Property @{ 
      'CollectDate'  = Get-Date 
      'ADUserUPN'   = $_.UserPrincipalName 
      'O365UserUPN'  = $_.UserPrincipalName 
      'ADUserCreated'  = $_.whenCreated 
      'ADUserEnabled'  = $_.Enabled 
      'ADLastLogonDate' = $_.LastLogonDate 
      'O365Licensed'  = $true 
      'O365LastLogonTime' = $O365Stats.LastLogonTime 
      'O365SMTPAddress' = $O365Smtp.PrimarySMTPAddress 
     } 
    } 
} | Sort-Object -Property ADUserCreated | Export-Csv -Path $OutputFile -NoType 

另外,为什么到底是大家用的子表达式如此痴情?在需要它们的地方使用它们。不要在他们不需要时混淆你的代码。

+0

你是正确的关于不必要的过滤人我会尽快检查。 – Arbelac

+0

我收到如下警告:默认情况下,只返回前1000个项目。使用ResultSize参数指定返回的项目数。要返回所有项目,请指定“-ResultSize Unlimited”。请等待 ,根据实际的项目数量,返回所有项目可能需要很长时间并消耗大量内存。另外,我们不建议将结果存储在变量中。用 代替,将结果传送给另一个任务或脚本以执行批处理更改。你推荐什么? – Arbelac

+0

你究竟在哪里得到这个警告? AFAICS除Get-MsolUser之外的所有cmdlet都应返回单个项目,但根据文档,Get-MsolUser的参数应为-MaxResults(缺省值为500),而不是-ResultSize。 –

相关问题