使用PowerShell复制广告用户

使用PowerShell复制广告用户,powershell,active-directory,Powershell,Active Directory,我在一个培训课程中看到了下面的例子,我认为这是错误的 代码应复制PowerShell中的Active Directory用户: $userInstance = Get-ADUser -Identity "kmill" $userInstance = New-Object Microsoft.ActiveDirectory.Management.ADUser $userInstance.DisplayName = "Peter Piper" New-ADUser -SAMAccountNam

我在一个培训课程中看到了下面的例子,我认为这是错误的

代码应复制PowerShell中的Active Directory用户:

$userInstance = Get-ADUser -Identity "kmill"

$userInstance = New-Object Microsoft.ActiveDirectory.Management.ADUser

$userInstance.DisplayName = "Peter Piper"

New-ADUser -SAMAccountName "ppiper" -Instance $userInstance
我的理解是,我们将“Kmill”的用户帐户作为对象,并将其存储在第1行的变量中

我的理解是,在第2行中,我们完全覆盖了该变量,基本上删除了第1行中获得的所有数据(因此不会复制现有用户)


我错了吗?

你的推理是正确的,示例中有一个错误。通过打印对象,可以很容易地检查这一点。不需要用户创建。这样,

PS C:\> $u = Get-ADUser -identity someUser
PS C:\> $u # Print the variable contents on console
DistinguishedName : CN=someuser...
Enabled: True
...
PS C:\> $u = New-Object Microsoft.ActiveDirectory.Management.ADUser
PS C:\> $u # Print again, all the properties are empty
GivenName          :
Surname            :
UserPrincipalName  :
PS C:\> 
Technet有一个使用
-Identity
将现有用户设置作为模板传递的模板

PS C:\> $userInstance = Get-ADUser -Identity "saraDavis"
PS C:\> New-ADUser -SAMAccountName "ellenAdams" -Instance $userInstance -DisplayName "EllenAdams"

正如我所怀疑的那样-我尝试了$a.PropertyCount(),它在新对象之后返回了几十个属性,后跟0。