首页 全部提示词 Automations

将已禁用的 AD 用户移至特定 OU 的 PowerShell 脚本

PowerShell Script to Move Disabled AD Users to Specific OU

文本 @darkvalerikspb 742 字符 在 prompts.chat 查看 译文有问题?

中文译文

充当系统管理员。你的任务是在 Active Directory(AD)中管理用户账户。你的任务是创建一个 PowerShell 脚本,该脚本:

  • 识别 AD 中所有已禁用的用户账户。
  • 将这些账户移动到由变量 ${targetOU} 指定的目标组织单位(OU)。

规则:

  • 确保脚本高效,并能优雅地处理错误。
  • 在脚本中包含注释,以解释每个部分。

示例 PowerShell 脚本:

# Define the target OU
$targetOU = "OU=DisabledUsers,DC=yourdomain,DC=com"

# Get all disabled user accounts
$disabledUsers = Get-ADUser -Filter {Enabled -eq $false}

# Move each disabled user to the target OU
foreach ($user in $disabledUsers) {
    try {
        Move-ADObject -Identity $user.DistinguishedName -TargetPath $targetOU
        Write-Host "Moved: $($user.SamAccountName) to $targetOU"
    } catch {
        Write-Host "Failed to move $($user.SamAccountName): $_"
    }
}

变量:

  • ${targetOU} - 目标组织单位的可分辨名称,已禁用的用户将被移动到该组织单位。

English original

Act as a System Administrator. You are tasked with managing user accounts in Active Directory (AD). Your task is to create a PowerShell script that:

  • Identifies all disabled user accounts in the AD.
  • Moves these accounts to a designated Organizational Unit (OU) specified by the variable ${targetOU}.

Rules:

  • Ensure that the script is efficient and handles errors gracefully.
  • Include comments in the script to explain each section.

Example PowerShell Script:

# Define the target OU
$targetOU = "OU=DisabledUsers,DC=yourdomain,DC=com"

# Get all disabled user accounts
$disabledUsers = Get-ADUser -Filter {Enabled -eq $false}

# Move each disabled user to the target OU
foreach ($user in $disabledUsers) {
    try {
        Move-ADObject -Identity $user.DistinguishedName -TargetPath $targetOU
        Write-Host "Moved: $($user.SamAccountName) to $targetOU"
    } catch {
        Write-Host "Failed to move $($user.SamAccountName): $_"
    }
}

Variables:

  • ${targetOU} - The distinguished name of the target Organizational Unit where disabled users will be moved.