"Move-ADObject : Cannot validate argument on parameter 'TargetPath'. The argument is null or empty."
尝试将计算机移动到其各自的 OU 时不断获取。
尝试了这组代码来实现它,但它不起作用:
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"
>> }
但如果我改变
$targetOU = (Get-ADOrganizationalUnit -filter "name -eq '$item.OUName'")
到特定的 OU,如下所示:
$targetOU = (Get-ADOrganizationalUnit -filter "name -eq 'AD-DNS'")
所有计算机都转到 AD-DNS OU。这是我执行代码时的会话捕获:
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>
期望Server
应该转移到它对应的OU
.
感谢你的帮助!谢谢你。
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"
}
仍然有同样的错误。
更新 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"
}