首页 > 解决方案 > 如何更快地执行powershell脚本

问题描述

下面的代码试图删除所有共享邮箱的文档和权限。下面的脚本需要更多时间来执行。我想让脚本执行得更快,任何帮助将不胜感激。

 $AmIOnO365=get-Mailbox -identity $User.UserPrincipalName -erroraction SilentlyContinue
    if($AmIOnO365 -ne $null)
        {
        $SharedMailboxes = Get-Mailbox -ResultSize unlimited -Filter {RecipientTypeDetails -eq 'SharedMailbox'}
        #write-host -ForegroundColor Green " All shared mailboxes for full delegation and remove as necessary"
        $Full=$null
        $Counter=0
        $SAM=$null
        foreach ($SharedMailbox in $SharedMailboxes)
            {
            $Counter=$Counter+1
            #write-host -ForegroundColor Green $Counter "/" $SharedMailboxes.count"."$SharedMailbox.PrimarySmtpAddress
            # Full Access Rights
            $Full = Get-MailboxPermission -identity $SharedMailbox.PrimarySmtpAddress | where {$_.IsInherited -eq $False -and $_.User -notlike "NT AUTHORITY*" -and $_.User -notlike "S-1-5-21*" -and $_.User -notlike "NAM*"} 
            If ($Full)
                {
                   Foreach ($DelegatedUser in $Full)
                    {
                    if($DelegatedUser.User -eq $UserPrimarySMTPAddress.PrimarySmtpAddress)
                        {
                        $SAM=$DelegatedUser.User
                                 Write-Host -ForegroundColor Yellow "     Removed full rights: $SAM on the shared mailbox:"$SharedMailbox.PrimarySmtpAddress
                        $LogFileContent+="`n`r`n`rRemoved full rights: "+$SAM+" on the shared mailbox: "+$SharedMailbox.PrimarySmtpAddress
                        #write-host -ForegroundColor White "     Removing" $UserPrimarySMTPAddress.PrimarySmtpAddress "rights of" $DelegatedUser.AccessRights "from " $SharedMailbox.PrimarySMTPaddress
                        Remove-MailboxPermission -Identity $SharedMailbox.PrimarySMTPaddress -user $UserPrimarySMTPAddress.PrimarySmtpAddress -AccessRights $DelegatedUser.AccessRights -Confirm:$False
                        }
                   }
                }
            }
            $LogFileContent > $FileName
        }

标签: powershell

解决方案


关于您的日志记录:

使用递增赋值运算符 ( +=) 创建对象集合一样,您通常应该避免使用递增赋值运算符 (+=) 来构建字符串,因为它的代价是指数级的。

相反,我建议您使用 PowerShell 管道并将该行直接输出到日志文件中:

foreach ($SharedMailbox in $SharedMailboxes) {
    $Counter=$Counter + 1
    # write-host -ForegroundColor Green $Counter "/" $SharedMailboxes.count"."$SharedMailbox.PrimarySmtpAddress
    # Full Access Rights
    $Full = Get-MailboxPermission -identity $SharedMailbox.PrimarySmtpAddress | where {$_.IsInherited -eq $False -and $_.User -notlike "NT AUTHORITY*" -and $_.User -notlike "S-1-5-21*" -and $_.User -notlike "NAM*"} 
    If ($Full) {
       Foreach ($DelegatedUser in $Full) {
            if($DelegatedUser.User -eq $UserPrimarySMTPAddress.PrimarySmtpAddress) {
                $SAM=$DelegatedUser.User
                Write-Host -ForegroundColor Yellow "     Removed full rights: $SAM on the shared mailbox:"$SharedMailbox.PrimarySmtpAddress
                "Removed full rights: " + $SAM + " on the shared mailbox: " + $SharedMailbox.PrimarySmtpAddress
                #write-host -ForegroundColor White "     Removing" $UserPrimarySMTPAddress.PrimarySmtpAddress "rights of" $DelegatedUser.AccessRights "from " $SharedMailbox.PrimarySMTPaddress
                Remove-MailboxPermission -Identity $SharedMailbox.PrimarySMTPaddress -user $UserPrimarySMTPAddress.PrimarySmtpAddress -AccessRights $DelegatedUser.AccessRights -Confirm:$False
            }
       }
    }
} | Add-Content $FileName

推荐阅读