简体   繁体   English

使用Powershell将计算机从csv文件移动到正确的OU

[英]Move computer to the correct OU from csv file using Powershell

Keep getting "Move-ADObject : Cannot validate argument on parameter 'TargetPath'. The argument is null or empty." 不断获取"Move-ADObject : Cannot validate argument on parameter 'TargetPath'. The argument is null or empty." when trying to move computers to their respective OU. 尝试将计算机移至其各自的OU时。

Tried these set of codes to achieve it but it doesn't work: 尝试了以下代码集来实现它,但是它不起作用:

PS C:\temp> cat .\OUs.csv
OUName,Server
AD-DNS,AD-DNS-Server
Apps,App-Server
DBs,DB-Server1
DBs,DB-Server2
Utilities-Servers,Utils-Server
PS C:\temp>
PS C:\temp> $CSVFile = Import-Csv ".\OUs.csv"
PS C:\temp> foreach ($item in $CSVFile){
>> $computer = (Get-ADComputer $item.Server).DistinguishedName
>> $targetOU = (Get-ADOrganizationalUnit -filter "name -eq '$item.OUName'")
>>     Move-ADObject -Identity $computer -TargetPath $targetOU.DistinguishedName -Confirm:$false
>>     Write-Host "Computer $computer has been moved successfully to $targetOU"
>> }

But if I change 但是如果我改变

$targetOU = (Get-ADOrganizationalUnit -filter "name -eq '$item.OUName'")

to a specific OU, like this: 到特定的OU,例如:

$targetOU = (Get-ADOrganizationalUnit -filter "name -eq 'AD-DNS'")

all the computers go to AD-DNS OU. 所有计算机都转到AD-DNS OU。 Here's a session capture when I executed the code: 这是执行代码时的会话捕获:

PS C:\temp>
PS C:\temp> cat .\OUs.csv
OUName,Server
AD-DNS,AD-DNS-Server
Apps,App-Server
DBs,DB-Server1
DBs,DB-Server2
Utilities-Servers,Utils-Server
PS C:\temp>
PS C:\temp> $CSVFile = Import-Csv ".\OUs.csv"
PS C:\temp> foreach ($item in $CSVFile){
>> $computer = (Get-ADComputer $item.Server).DistinguishedName
>> $targetOU = (Get-ADOrganizationalUnit -filter "name -eq '$item.OUName'")
>>     Move-ADObject -Identity $computer -TargetPath $targetOU.DistinguishedName -Confirm:$false
>>     Write-Host "Computer $computer has been moved successfully to $targetOU"
>> }
Move-ADObject : Cannot validate argument on parameter 'TargetPath'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:4 char:51
+ ... t -Identity $computer -TargetPath $targetOU.DistinguishedName -Confir ...
+                                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Move-ADObject], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.MoveADObject

Computer CN=AD-DNS-Server,CN=Computers,DC=msoc,DC=local has been moved successfully to
Move-ADObject : Cannot validate argument on parameter 'TargetPath'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:4 char:51
+ ... t -Identity $computer -TargetPath $targetOU.DistinguishedName -Confir ...
+                                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Move-ADObject], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.MoveADObject

Computer CN=App-Server,CN=Computers,DC=msoc,DC=local has been moved successfully to
Move-ADObject : Cannot validate argument on parameter 'TargetPath'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:4 char:51
+ ... t -Identity $computer -TargetPath $targetOU.DistinguishedName -Confir ...
+                                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Move-ADObject], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.MoveADObject

Computer CN=DB-Server1,CN=Computers,DC=msoc,DC=local has been moved successfully to
Move-ADObject : Cannot validate argument on parameter 'TargetPath'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:4 char:51
+ ... t -Identity $computer -TargetPath $targetOU.DistinguishedName -Confir ...
+                                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Move-ADObject], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.MoveADObject

Computer CN=DB-Server2,CN=Computers,DC=msoc,DC=local has been moved successfully to
Move-ADObject : Cannot validate argument on parameter 'TargetPath'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:4 char:51
+ ... t -Identity $computer -TargetPath $targetOU.DistinguishedName -Confir ...
+                                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Move-ADObject], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.MoveADObject

Computer CN=Utils-Server,CN=Computers,DC=msoc,DC=local has been moved successfully to
PS C:\temp>

Expectation is that Server should get moved to it's corresponding OU . 期望Server应该移到它的相应OU

Appreciate your help! 感谢您的帮助! Thank you. 谢谢。

Update1: I tried changing the code to the following: Update1:​​我尝试将代码更改为以下内容:

$CSVFile = Import-Csv ".\OUs.csv"
foreach ($item in $CSVFile){
$computer = (Get-ADComputer $item.Server).DistinguishedName
$targetOU = (Get-ADOrganizationalUnit -filter "name -eq '$item.OUName'").DistinguishedName
    Move-ADObject -Identity $computer -TargetPath $targetOU -Confirm:$false
    Write-Host "Computer $computer has been moved successfully to $targetOU"
}

still got the same error. 仍然有同样的错误。

Update 2: This works: 更新2:有效:

$CSVFile = Import-Csv ".\OUs.csv"
foreach ($item in $CSVFile){
    $computer = (Get-ADComputer $item.Server).DistinguishedName
    $targetOU = Get-ADObject -Filter "Name -eq '$($item.OUName)'"
    Move-ADObject -Identity $computer -TargetPath $targetOU.DistinguishedName -Confirm:$false
    Write-Host "Computer $computer has been moved successfully to $targetOU"
}

When accessing a property in a variable inside a quoted string, you have to escape it with $(...) to have it evaluated as an expression . 在带引号的字符串中访问变量中的属性时,必须使用$(...)对其进行转义,以将其评估为表达式 So your code becomes: 因此,您的代码变为:

$targetOU = (Get-ADOrganizationalUnit -filter "name -eq '$($item.OUName)'")

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

相关问题 在PowerShell中将用户移动到另一个没有CSV的OU - Move Users in PowerShell to another OU without CSV 使用Powershell将计算机移至SCCM 2012任务序列中的新OU - Move Computer to new OU in SCCM 2012 Task Sequence with Powershell Powershell根据用户主要组将计算机移至OU - Powershell move computer to OU depending on users primary group Powershell - Active Directory - 根据员工 ID 和日期的 CSV 文件将用户移动到 OU - Powershell - Active Directory - Move users to an OU based on CSV file for employeeID and Date 如何从计算机 OU 中动态移动计算机帐户 - How to dynamically move computer accounts from Computers OU 我如何从附加的 CSV 文件中的 DistinguishedName 列中过滤“OU = 服务帐户”:powershell - How can i Filter “OU=Service account” from column DistinguishedName from a CSV file attached : powershell 使用Powershell WMI通过从csv文件评估CPU日期来获取计算机制造日期 - Get Computer Manufacture Date by evaluate CPU Dates from a csv file using Powershell WMI 使用Powershell将Active Directory组移动到另一个OU - Move Active Directory Group to Another OU using Powershell 按员工编号禁用csv文件中的用户,然后进入某个OU - Disable Users in a csv file by employee number and then move into certain OU 在 powershell 导出的 csv 文件中删除“OU=Domain Controller” - Remove "OU=Domain Controller " in exported csv file in powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM