Не работает фрагмент кода в powershell, по перемещению пользователя внутри AD|
Столкнулся с такой проблемой: при отработки скрипта, фрагмент кода в сценарии перемещения выдает ошибку. При этом, если запустить только этот фрагмент задача выполняется. Фрагмент на перемещение
$userSource= 'name1'
$userTarget= 'name2'
$gay=Get-ADUser $userSource -Properties DistinguishedName
$ou=$gay | ForEach-Object {$_.DistinguishedName}
$ou1=$ou -replace '^.*?,(?=[A-Z]{2}=)' > C:\soft\1132.txt # Проверяю, что получил
Get-ADUser $userTarget | Move-ADObject -TargetPath $ou1 #Перемещение
Весь скрипт:
$logfile="c:\soft\CopyAdGroup.log"
$userSource= 'name1'
$userTarget= 'name2'
$gay=Get-ADUser $userSource -Properties DistinguishedName
$ou=$gay | ForEach-Object {$_.DistinguishedName}
$ou1=$ou -replace '^.*?,(?=[A-Z]{2}=)' > C:\soft\1132.txt
Get-ADUser $userTarget | Move-ADObject -TargetPath $ou1
$Time = Get-Date
Add-content $logfile -value $Time -Encoding UTF8
Add-content $logfile -value "Копирование групп AD с пользователя $userSource на $userTarget" -Encoding UTF8
$sourceGroups = (Get-ADPrincipalGroupMembership -Identity $userSource).SamAccountName
foreach ($group in $sourceGroups){
Add-content $logfile -value "Добавление пользователя $userTarget в группу $group" -Encoding UTF8
try{
$log=Add-ADPrincipalGroupMembership -Identity $userTarget -MemberOf $group
Add-content $logfile -value $log -Encoding UTF8
}
catch{
Add-content $logfile $($Error[0].Exception.Message) -Encoding UTF8
Continue
}
}
$Users = Get-ADUser $userSource -Properties Manager,Department,Description,Title,Company,co,City,StreetAddress,ipPhone #or get-adgroupmember -identity "Пользователи домена" |get-aduser |select -exp samaccountname
foreach( $User in $Users ){
try{
Set-aduser -Identity $userTarget -Replace @{Manager= $User.Manager } -ErrorAction Continue
Set-aduser -Identity $userTarget -Replace @{Department= $User.Department} -ErrorAction Continue
#Set-aduser -Identity $userTarget -Replace @{Description= $User.Description} -ErrorAction Continue
Set-aduser -Identity $userTarget -Replace @{Title= $User.Title} -ErrorAction Continue
Set-aduser -Identity $userTarget -Replace @{Company= $User.Company} -ErrorAction Continue
Set-aduser -Identity $userTarget -Replace @{co= $User.co} -ErrorAction Continue
#Set-aduser -Identity $userTarget -Replace @{City= $User.City} -ErrorAction Continue
Set-aduser -Identity $userTarget -Replace @{StreetAddress= $User.StreetAddress} -ErrorAction Continue
Set-aduser -Identity $userTarget -Replace @{ipPhone= $User.ipPhone} -ErrorAction Continue
write host "Успешно"
}
catch{
Write-Warning "$_"
}
}
#Disable-ADAccount -Identity $userSource
#get-content -Path C:\soft\CopyAdGroup.log
Тест ошибки:
Move-ADObject : Не удается проверить аргумент для параметра "TargetPath". Аргумент пустой или имеет значение NULL. Укажите непустой аргумент, не имеющий значение NULL, п
осле чего повторите выполнение команды.
C:\soft\Copy user in AD.ps1:7 знак:52
+ Get-ADUser $userTarget | Move-ADObject -TargetPath $ou1 #Переміщую
+ ~~~~
+ CategoryInfo : InvalidData: (:) [Move-ADObject], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.MoveADObject