【问题标题】:Move computer to the correct OU from csv file using Powershell使用 Powershell 将计算机从 csv 文件移动到正确的 OU
【发布时间】:2019-10-28 06:03:29
【问题描述】:

在尝试将计算机移动到各自的 OU 时不断收到 "Move-ADObject : Cannot validate argument on parameter 'TargetPath'. The argument is null or empty."

尝试了这组代码来实现它,但它不起作用:

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

感谢您的帮助!谢谢。

更新1: 我尝试将代码更改为以下内容:

$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"
}

【问题讨论】:

    标签: powershell csv active-directory ou


    【解决方案1】:

    访问带引号的字符串内的变量中的属性时,您必须使用$(...) 对其进行转义,以使其评估为expression。所以你的代码变成了:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-31
      • 2010-09-09
      相关资源
      最近更新 更多