此腳本旨在緩衝通過一系列目錄,當DirUnauthorizedAccessError,Microsoft
或PowerShell.Commands.GetChildItemCommand
類型的錯誤被拋出時,它應該調用另一個函數Take-Ownership
,該函數接受目錄的所有權併爲localAdmin和域管理員添加完整權限到文件夾。 (這是真正用於緩解了老用戶配置文件刪除腳本):當我以域管理員身份運行此功能時,爲什麼不會引發錯誤?
function Test-Folder($FolderToTest, $localAdminName) {
# Remeber the old error preference...
$old_ErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = 'SilentlyContinue'
$error.Clear()
# Go through the directories...and capture errors in $error
Get-ChildItem $FolderToTest -Recurse -ErrorAction SilentlyContinue -ErrorVariable errz | Select FullName
Write-Host $errz.count
if ($errz.Count -eq 0) {
Write-Host "blah no errors"
foreach ($err in $errz) {
Write-Host "Error: $err"
if ($err.FullyQualifiedErrorId -eq "DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand") {
Write-Host Unable to access $err.TargetObject -Fore Red
Write-Host Attempting to take ownership of $err.TargetObject -Fore Yellow
Take-Ownership -Folder $err.TargetObject, -LocalAdminName $localAdminName
Test-Folder -FolderToTest $err.TargetObject -localAdminName $localAdminName
}
}
}
$ErrorActionPreference = $old_ErrorActionPreference
}
不幸的是,當我運行它以域管理員它不拋出任何錯誤。我找到了ErrorActionPreferences的一個列表here,但這些錯誤似乎被忽略,並且它輸出blah no errors
我該怎麼做才能確保我收到錯誤,並且我的Take-Ownership
函數實際上被調用了?
如果我不太可能在這裏得到答案,請將其傳輸到serverfault stackexchange。 – leeand00