powershell - 如何更快地执行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 管道并将该行直接输出到日志文件中:
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
推荐阅读
- python - 在包含相同前缀和后缀的字符串列表中获取特定字符串
- android - 使用 BLE 扫描扫描过滤器
- step - 调用 Transfer() 时 Opencascade 崩溃
- bixby - 更新到 6.14 后模拟器中没有语音输出
- c# - c#从不同的类初始化静态变量
- angular - 为组件 AppComponent 指定的模板不是字符串
- vhdl - 在 VHDL 中,如何实现两个使用 PULLUP 驱动同一引脚的三态缓冲器?
- asp.net-mvc - 为什么我的 Sweetalert 一出现就消失了
- c# - StructureMap 在返回之前代理所有实例或修改实例
- c# - 串口“ACK 超时”