当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

作者:精品下载站 日期:2024-12-14 20:35:14 浏览:15 分类:玩电脑

通过 Set-ADUser 管理 Active Directory 用户


介绍

管理员有多种选项来管理 Active Directory 用户的属性。 Active Directory 用户和计算机 (ADUC) 控制台可以方便地进行一些基本更改,例如修改用户的描述或办公位置。

但是,如果需要更多功能,请考虑使用 PowerShell。本文介绍了如何使用 PowerShell cmdlet Set-ADUser 解决许多常见用例。

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

设置 ADUser Cmdlet

使用 Set-ADUser cmdlet,您可以修改 Active Directory 用户的许多属性。

请注意,为了从非域控制器的 Windows 计算机运行 PowerShell cmdlet,您需要首先导入 Windows PowerShell 的 Active Directory 模块。

设置 ADUser 参数

下表描述了可用于更改用户属性的 Set-ADUser cmdlet 的参数。

要查看 Set-ADUser cmdlet 的完整参数列表,请运行以下 cmdlet:

Get-help Set-ADUser
ParameterDescription(A)-AccountExpirationDateSets the account expiration date of the user object.-AccountNotDelegatedSpecifies whether the user’s security context is delegated to a service.-AddAdds one or more values to a property that cannot be changed using a cmdlet parameter (i.e., properties for which no parameter is available).-AllowReversiblePasswordEncryptionSpecifies whether the account is allowed to use reversible password encryption.-AuthenticationPolicySpecifies an authentication policy object for Active Directory Domain Services.-AuthenticationPolicySiloSets the authentication policy silo object for Active Directory Domain Services.-AuthTypeSpecifies the authentication method to use: Negotiate (or 0) or Basic (or 1).(C)-CannotChangePasswordSpecifies whether the user can change the account’s password.-CertificatesSpecifies the account’s DER-encoded X.509v3 certificates, such as the public-key certificates issued to this account by the Microsoft Certificate Service.-ChangePasswordAtLogonSpecifies whether the user must change their password at the next login.-CitySpecifies the city or town of the user.-ClearClears one or more values of an attribute that cannot be changed using a cmdlet parameter (i.e., attributes for which no parameter is available).-CompanySets the company attribute of the user account.-CompoundIdentitySupportedSpecifies whether a user’s account supports Kerberos service tickets, which include the user’s device’s authorization data.-ConfirmPrompts you for confirmation before running the cmdlet.-CountrySets the country or region code for the user account.-CredentialSpecifies the user account credentials to use to run the cmdlet. Unless the cmdlet is launched from an Active Directory PowerShell provider drive, the default credentials are those of the logged-on user.(D)-DepartmentSpecifies the department of the user.-DescriptionSpecifies the description of the user account.-DisplayNameSpecifies the display name of the user.-DivisionSpecifies the division of the user.(E)-EmailAddressSpecifies the email address of the user.-EmployeeIDSpecifies the employee ID of the user.-EmployeeNumberSpecifies the employee number of the user.-EnabledSpecifies whether the user account is enabled or disabled.(F)–FaxSpecifies the fax number of the user.(G)-GivenNameSpecifies the first name of the user.(H)-HomeDirectorySpecifies the home directory of the user account.-HomeDriveSets the UNC path of the home directory of the user account.(I)-IdentitySpecifies the user account whose properties need to be changed.
A user can be identified by its:

• Distinguished Name (DN)
• GUID
• Security Identifier (SID)
• Security Account Manager (SAM) account name

Alternatively, you can feed a user object through the pipeline to the Identity parameter, or set it to an object variable. For example, you can use the Get-ADUser cmdlet to retrieve a user object, which can be passed via the pipeline to the Set-ADUser cmdlet.-InitialsSpecifies the initials of the user.-InstanceSpecifies an ADUser object that identifies the Active Directory user object to be changed and the modifications to be made to that object. When this parameter is supplied, any changes made to the ADUser object are also propagated to the appropriate Active Directory object. Only the object properties that have changed are updated by the cmdlet.
This parameter is mainly used when you make changes to an object’s properties with a CSV or script using variables. You call the object, make changes to the desired properties, then use Set-ADUser with the -Instance parameter for those changes to take effect.(K)-KerberosEncryptionTypeSpecifies whether the user account is compatible with Kerberos encryption.(L)-LogonWorkstationsSpecifies the logon workstations of the user.(M)-ManagerSpecifies the manager of the user.-MobilePhoneSpecifies the mobile phone number of the user.(O)-OfficeSpecifies the physical office location of the user.-OfficePhoneSpecifies the office phone number of the user.-OrganizationSpecifies the organization of the user.-OtherNameSpecifies any other name property of the user.(P)-PartitionSpecifies the distinguished name of an Active Directory partition. The distinguished name must be one of the naming contexts on the current directory server.-PassThruReturns an object that represents the object you are working with. By default, this cmdlet does not generate any output.-PasswordNeverExpiresSpecifies whether the account password never needs to be updated.-PasswordNotRequiredSpecifies that the user account does not need a password.-POBoxSpecifies the PO box of the user.-PostalCodeSpecifies the postal code of the user.-ProfilePathSpecifies the profile path of the user.(R)-RemoveRemoves one or more values of the properties that cannot be modified using a cmdlet parameter (i.e., properties for which no parameter is available).-ReplaceReplaces one or more values of the properties that cannot be modified using a cmdlet parameter (i.e., properties for which no parameter is available).
This parameter is mostly used for more than one property value, or multiline attributes, particularly those for which there is no parameter available, but you can also use it for properties for which a specific parameter is available.(S)-SamAccountNameProvides the security account manager (SAM) account name of the user on creation.-ScriptPathSpecifies a path to the user’s log on script.-ServerSpecifies the AD DS instance to connect to.-ServicePrincipalNamesSpecifies the service principal name of the user.-SmartcardLogonRequiredSpecifies whether smart card logon is required for the user.-StateSpecifies the state of the user.-StreetAddressSpecifies the street address of the user.(T)-TitleSpecifies the user’s job title.-TrustedForDelegationSpecifies whether the account is trusted for Kerberos delegation. A service running under a trusted Kerberos delegation account can take on the identity of the client requesting the service.(U)-UserPrincipalNameSpecifies the user principal name (UPN) property of a user in the [email protected].(W)-WhatIfShows the output that would result from running the cmdlet, without actually running it.

详细命令

默认情况下,Set-ADUser cmdlet 不产生输出。要获取有关 cmdlet 正在执行的操作的信息,请使用 /verbose 开关。

Verbose 主要与变量和脚本一起使用,但有时也与 cmdlet 一起使用。

设置 ADUser 示例

以下是使用 Set-ADUser cmdlet 的示例。

  • 更改用户的办公室和州属性
  • 更改用户的标题
  • 指定用户管理员
  • 替换用户属性
  • 修改多个用户的多个属性,示例 1
  • 修改多个用户的多个属性,示例 2
  • 修改多个用户的多个属性,示例 3
  • 使用 CSV 文件修改多个用户的多个属性
  • 使用备用凭证
  • 清除属性值
  • 禁用 AD 用户帐户
  • 下次登录时强制更改密码
  • 修改可以具有多个值的属性

更改用户的办公室和州属性

以下 cmdlet 将设置 AbbeyCrawford 对象的 Office 和 State 属性的值:

Set-ADUser -Identity AbbeyCrawford -Office 'Atlanta' -State 'GA'

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

在这里您可以看到用户的 Office 和 State 属性已按指定设置:

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

更改用户的标题

以下 cmdlet 将更新 AbbeyCrawford 用户对象的 Title 属性:

Set-ADUser -Identity AbbeyCrawford -Title 'CIO'

要检查更新是否成功,请使用以下命令:

Get-ADUser -Identity AbbeyCrawford -Properties Name,Department,title | Select-Object -Property Name,Department,title

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

指定用户管理员

以下命令使用 Get-ADUser 获取 AD 用户对象 AbbeyCrawford,该对象将传递给 Set-ADUser cmdlet,后者会更新其 Manager 属性:

Get-ADUser -Identity "AbbeyCrawford" | Set-ADUser -Manager "AbbeyEckels"

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

您可以使用以下 cmdlet 确认更改:

Get-ADUser -Identity AbbeyCrawford -Properties manager | Select-Object -Property manager

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

替换用户属性

要将属性的当前值替换为新值,请使用 Replace 参数。使用 LDAP 显示名称来标识要修改的对象。

此命令更新用户 AbbeyCrawford 的头衔和电子邮件地址:

Set-ADUser -Identity Abbey.Crawford -Replace @{;mail="[email protected]"}

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

您可以使用以下 cmdlet 验证更改:

Get-ADUser -Identity AbbeyCrawford -Properties title,mail | Select-Object -Property title,mail

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

修改多个用户的多个属性,示例 1

以下脚本更新一组特定用户的多个属性:

  • 第一个命令从 Engineering OU 获取 AD 用户。
  • 输出通过管道传输到第二个命令,该命令替换所有这些用户的三个属性。
Get-ADUser -SearchBase " OU=Engineering,OU=Versacorp,DC=corp,DC=com" -filter * -Properties Department,Company,l,st | Set-ADUser -Replace @{Department=”Engineering”;Company="VersaCorp";l=”San Francisco”;St=”CA”}

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

您可以使用以下脚本检查结果:

Get-ADUser -SearchBase "OU=Engineering,OU=Versacorp,DC=corp,DC=com" -Filter * -Properties DisplayName,Department,Company,l,st | select-object -property DisplayName,Department,Company,l,St

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

修改多个用户的多个属性,示例 2

同样,以下脚本更新 Engineering OU 中用户的 DisplayName 属性:

Get-ADUser -Filter 'Name -like "*"' -SearchBase ”OU=Engineering,OU=Versacorp,DC=corp,DC=com”  -Properties DisplayName | % {Set-ADUser $_ -DisplayName ($_.GivenName + ' ' + $_.SurName)}

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

要检查此 cmdlet 的输出,请运行以下脚本:

Get-ADUser -SearchBase "OU=Engineering,OU=Versacorp,DC=corp,DC=com" -Filter * -Properties DisplayName | select-object -property DisplayName

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

修改多个用户的多个属性,示例 3

假设波士顿办公室的所有用户都将迁移到伍斯特办公室,因此您需要更新所有用户的办公室属性。

首先,我们使用适当的用户创建一个变量:

$BostonUsers = Get-ADUser -Filter "physicalDeliveryOfficeName -eq 'Boston'" -Property *

要检查用户及其办公室的列表,我们可以使用以下 cmdlet:

$BostonUsers | Select-Object Name, Office

下一步是更新每个用户的办公位置。我们将再次使用 ForEach 循环:

ForEach ($User in $BostonUsers){Set-ADUser -Identity $User -Office ‘Worcester’}

最后,为了验证更新是否成功,我们可以使用 Get-ADUser cmdlet 显示伍斯特办公室的用户列表:

Get-ADUser -Filter "physicalDeliveryOfficeName -eq 'Worcester'" -Property * | Select-Object Name,Office

使用 CSV 文件修改多个用户的多个属性

或者,您可以使用 CSV 文件提供要修改其属性的 AD 用户的列表。假设您有以下 CSV 文件:

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

我们可以创建“$Users”变量来从 CSV 文件中获取有关用户的信息:

$users = Import-csv -Path c:\hr.csv

然后我们可以使用以下脚本进行所需的更改; “ForEach”循环遍历所有用户对象并更新其部门和职位属性:

foreach ($user in $users) {
Get-ADUser -Filter "employeeID -eq '$($user.employeeID)'" -Properties * -SearchBase " OU=Engineering,OU=Versacorp,DC=corp,DC=com" |Set-ADUser -department $($user.department) -title $($user.title) 
}

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

尽管您可以在 Windows PowerShell 中逐行执行脚本,但建议对较大的脚本使用 Windows PowerShell ISE,如下所示:

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

Windows PowerShell ISE 有两个部分:

  • 脚本编辑器
  • PowerShell 控制台

下图显示了 cmdlet 或脚本的运行方式。有一个工具栏按钮用于播放和停止脚本。

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

使用备用凭证

默认情况下,Set-ADUser 在当前登录用户的上下文中运行。要指定不同的凭据,请使用 -Credential 参数。

以下命令使用 Get-Credential 生成凭证对象:

$credential = Get-Credential

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

然后,我们可以使用 -Credential 参数将该凭据对象传递给 Set-ADUser cmdlet:

Set-ADUser -Identity AbbeyCrawford -Title “Senior Software Developer” -Credential $credential

清除属性值

要清除 Active Directory 用户属性的值,请使用 -Clear 参数。下面的脚本使用 Get-ADUser cmdlet 从指定的 OU 检索用户并将结果传递给 Set-ADUser,这将清除 Department 属性:

Get-ADUser -filter * -SearchBase " OU=Engineering,OU=Versacorp,DC=corp,DC=com" | Set-AdUser -clear department

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

要检查结果,请使用以下脚本:

Get-ADUser -SearchBase "OU=Engineering,OU=Versacorp,DC=corp,DC=com" -Filter * -Properties DisplayName | select-object -property DisplayName,Department

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

禁用 AD 用户帐户

每当用户离开组织时,确保其帐户被停用至关重要。

首先,我们将使用 Get-ADUser cmdlet 来检查离职用户的帐户是否已启用:

Get-ADUser -Identity AbbeyCrawford -Properties Enabled | Select Enabled

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

输出显示该帐户处于活动状态(Get-ADUser Enabled 为 True),因此我们需要禁用它。我们可以使用以下脚本;第一个命令获取 AD 用户的 Enabled 属性,第二个命令禁用用户帐户:

Get-ADUser -Identity AbbeyCrawford -Properties Enabled | Set-ADUser -Enabled $False

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

我们可以通过运行以下 cmdlet 来检查结果:

Get-ADUser -Identity AbbeyCrawford -Properties DisplayName,Enabled | select-object -Property DisplayName,Enabled

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

下次登录时强制更改密码

以下命令将修改指定 OU 中所有用户的 UserAccountControl 属性值,强制他们在下次登录时更改密码:

Get-ADUser -Filter *  -SearchBase "OU=Test,DC=corp,DC=com" | Set-ADUser -ChangePasswordAtLogon $true

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

下图显示了我们刚刚设置的复选框:

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

修改可以具有多个值的属性

某些属性(例如 ProxyAddress 和 OtherTelephone)可以有多个值。要修改它们,请使用 Set-ADUser 的 -Add 和 -Remove 参数。

要删除单个值,请指定属性和要从中删除的值:

Set-ADUser -Identity AbbeyCrawford -Remove @{proxyAddresses="[email protected]"}

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

要验证结果,请使用 Get-ADUser cmdlet:

Get-ADUser -Identity AbbeyCrawford -Properties manager | Select-Object -Property manager

请注意,[email protected] 已从 ProxyAddresses 列表中删除。

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

要添加多个值,请使用逗号分隔的列表,如下所示:

Set-ADUser -Identity AbbeyCrawford -Add @{proxyAddresses="[email protected]","[email protected]"}

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

要检查结果,请使用 Get-ADUser cmdlet:

Get-ADUser -Identity AbbeyCrawford -Properties proxyaddresses | Select-Object -Property proxyaddresses

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

用于管理 AD 用户的 PowerShell 替代方案

Netwrix GroupID 命令行管理程序

Netwrix GroupID Shell 是一个命令行工具,可以轻松管理 Active Directory 和基于 Azure 的身份存储中的对象,以及执行可以使用 PowerShell 执行的其他管理活动。

正如您在此处所看到的,Netwrix GroupID Shell cmdlet 与 PowerShell cmdlet 类似:

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

以下是与 Set-ADUser 类似的 Netwrix GroupID Shell cmdlet:

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

Netwrix GroupID 自助服务门户

借助 Netwrix GroupID,您可以快速创建基于 Web 的门户,使帮助台技术人员和业务用户无需 IT 团队的帮助即可执行某些任务。用户可以:

  • 搜索目录
  • 更新他们的个人资料
  • 创建和更新目录对象和组

当用户管理自己的信息时,数据会更加准确和可靠。不过,管理员可以完全控制数据完整性,因为他们决定用户可以通过门户查看和修改哪些内容。管理员还可以构建充当审核系统的工作流程,以确保在将更改提交到目录之前输入正确的数据。

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

以下是一些可以通过自助服务门户更新的用户属性:

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

[玩转系统] 通过 Set-ADUser 管理 Active Directory 用户

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯