使用 Powershell 将计算机从 csv 文件移动到正确的 OU
Move computer to the correct OU from csv file using Powershell
尝试将计算机移动到各自的 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"
}
在引用字符串中的变量中访问 属性 时,您必须使用 $(...)
将其转义以将其评估为 expression。所以你的代码变成:
$targetOU = (Get-ADOrganizationalUnit -filter "name -eq '$($item.OUName)'")
尝试将计算机移动到各自的 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"
}
在引用字符串中的变量中访问 属性 时,您必须使用 $(...)
将其转义以将其评估为 expression。所以你的代码变成:
$targetOU = (Get-ADOrganizationalUnit -filter "name -eq '$($item.OUName)'")