因此,我正在嘗試獲取一個 PowerShell 腳本,該腳本將搜索并禁用已在 x 天內處于非活動狀態的所有帳戶。但我希望它跳過 1 個 OU,它不會搜索和禁用 OU 中的任何用戶。然后將所有禁用的用戶移動到 1 個單一 OU
這是我目前擁有的代碼。我錯過了排除部分。
使用此代碼禁用已閑置 X 天的帳戶
Search-ADAccount -SearchBase “OU=example,DC=example,DC=com” -AccountInactive -TimeSpan (\[timespan\]0d) -UsersOnly | Set-ADUser -Enabled $false
使用此代碼移動用戶
Get-ADuser -SearchBase “OU=example,DC=example,DC=com” -filter {Enabled -eq $false} | Move-ADObject -TargetPath “OU=Computers,OU=example,DC=example,DC=com”
我不是 100% 確定如何使用 exclude 命令,我嘗試使用谷歌搜索并找到其他可能有效的腳本,但到目前為止還沒有
uj5u.com熱心網友回復:
理論上你可以在一個管道中完成所有的程序,但是我沒有親自測驗過。這不是排除特定組織單位的有效方法,但是,正如您在評論中提到的那樣,此程序對您來說只需幾秒鐘,所以應該沒問題。
$ouToExclude = 'OU=example123,DC=example,DC=com'
Search-ADAccount -SearchBase "OU=example,DC=example,DC=com" -AccountInactive -TimeSpan ([timespan]0d) -UsersOnly |
Group-Object { $_.DistinguishedName -replace '^CN=.*(?=OU=)' } |
Where-Object Name -NE $ouToExclude | ForEach-Object Group |
Set-ADUser -Enabled $false -PassThru |
Move-ADObject -TargetPath "OU=Computers,OU=example,DC=example,DC=com"
- 搜索所有非活動用戶物件
- 按用戶的父 OU 對用戶進行分組。這是通過
Group-Object計算運算式腳本塊完成的 - 過濾父 OU不等于
$ouToExclude - 傳遞物件組
Set-ADUser以禁用它們,然后-PassThru我們可以將這些禁用的物件傳遞給Move-ADObject
uj5u.com熱心網友回復:
在沒有看到 OU 的真實可分辨名稱的情況下,我認為最好在此處使用非正則運算式-like運算子來測驗用戶是否在 $exclude 變數中定義的(子)OU 中。
嘗試
$searchBase = "OU=example,DC=example,DC=com" # DN of the OU to search in
$exclude = 'OU=example123,DC=example,DC=com' # DN of the OU to exclude
$destination = 'OU=InactiveUsers,OU=example,DC=example,DC=com' # DN of the OU to move inactive users to
Search-ADAccount -SearchBase $searchBase -AccountInactive -TimeSpan ([timespan]0d) -UsersOnly |
Where-Object {$_.DistinguishedName -notlike "*$exclude"} |
ForEach-Object {
$_ | Set-ADUser -Enabled $false
$_ | Move-ADObject -TargetPath $destination
}
uj5u.com熱心網友回復:
$excludedOU = ‘OU=skip,OU=example,DC=example,DC=com’
Search-ADAccount -SearchBase "OU=example,DC=example,DC=com" -AccountInactive -TimeSpan ([timespan]0d) -UsersOnly |
Group-Object { $_.DistinguishedName -replace '^CN=.*(?=OU=)' } | Get-ADuser -Filter * | ? { $_.DistinguishedName -notmatch $excludedOU -and $_.DistinguishedName -notmatch ‘CN=Admin,OU=Users,OU=example,DC=example,DC=com’}|
Set-ADUser -Enabled $false -PassThru |
Move-ADObject -TargetPath "OU=Computers,OU=example,DC=example,DC=com"
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/454889.html
標籤:电源外壳 活动目录 PowerShell-2.0 powershell-3.0
上一篇:查找計算機和執行策略
