简体   繁体   English

如何使用PowerShell在AD中移动用户?

[英]How can I use PowerShell to move a user in AD?

I'm in need of a little help. 我需要一些帮助。 I've got little to no PowerShell experience but I'm working with a Pocket Guide by my side and my GoogleFu. 我几乎没有PowerShell经验,但我正在使用Pocket Guide和我的GoogleFu。

Currently, my plan is to prompt for a username and store it, use Get-ADUser with the stored username to get and store the DistinguishedName, use Move-ADObject to move the user from the DistinguishedName to the target path. 目前,我的计划是提示用户名并存储它,使用Get-ADUser和存储的用户名来获取和存储DistinguishedName,使用Move-ADObject将用户从DistinguishedName移动到目标路径。

The problem I'm encountering is storing and calling these things. 我遇到的问题是存储和调用这些东西。 I have this, which gives me the info on a user. 我有这个,它给了我一个用户的信息。 How can I isolate just the distinguished name and store it? 我怎样才能隔离专有名称并存储它?

$name = read-host "Enter user name"
Get-ADUser $name

After storing the DN, can Move-ADObject use the stored value? 存储DN后,Move-ADObject可以使用存储的值吗? I've attempted to store individual values like: 我试图存储个别值,如:

Move-ADobject 'CN=$name,OU=department,OU=company,DC=Domain,DC=net' -TargetPath 'OU=NonActive,OU=company,DC=Domain,DC=net'

But this returns "Directory object not found" as it doesn't use the stored value. 但是这会返回“找不到目录对象”,因为它不使用存储的值。

尝试这个:

Get-ADUser $name| Move-ADObject -TargetPath 'OU=nonactive,OU=compny,DC=domain,Dc=net'

Just an aside on this - 就此而言 -

Powershell can't recognise variables such as $name when they're enclosed in single quotation marks as the shell treats such values as literal strings. 当shell括在单引号中时,Powershell无法识别$name等变量,因为shell将这些值视为文字字符串。 Use double quotes to work with variables: 使用双引号来处理变量:

Eg. 例如。 write-host '$name' would give the output $name , but write-host "$name" would return the value in the variable. write-host '$name'将给出输出$name ,但write-host "$name"将返回变量中的值。

So Move-ADobject "CN=$name,OU=department,OU=company,DC=Domain,DC=net" -TargetPath 'OU=NonActive,OU=company,DC=Domain,DC=net' should work as expected. 所以Move-ADobject "CN=$name,OU=department,OU=company,DC=Domain,DC=net" -TargetPath 'OU=NonActive,OU=company,DC=Domain,DC=net'应按预期工作。 On the other hand, you will learn more interesting stuff by using the pipeline. 另一方面,您将通过使用管道学习更多有趣的东西。

Only for your personal interest: 仅为了您的个人兴趣:

CB., doing the pipeline is passing an object. CB。,管道正在传递一个对象。 You, instead of using a string, should use an object with the command: 您不应使用字符串,而应使用带有以下命令的对象:

Move-ADObject

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM