Flow - Move user to AD OU

matt_a
Kilo Guru

Afternoon all, I am trying to move a user to a different active directory OU on the execution of a flow (not workflow).

I can see there is a PSscript: ActionMoveADObjecttoOU.ps1

Has anyone managed to get this working for a user rather than a computer  using the AD spoke? And if so, how did you achieve it?

Thanks

1 ACCEPTED SOLUTION

DanielCordick
Mega Patron
Mega Patron

To get this working i created a custom action using this PS script: In my set up, i have a table with all the OU's I need. then dynamically set it in my flow.

 

Create an action | ServiceNow Docs

 

if (test-path env:\SNC_username) {
$UserName = $env:SNC_username;
$OU = $env:SNC_OU;
};


try {

Get-ADUser -Identity "$UserName" | Move-ADObject -TargetPath "$OU"

Write-Host "SUCCESS: Moved $UserName to $OU"

}

catch {

$ErrorMessage = $_.Exception.Message

Write-Host "FAILED to move $UserName to $OU. Error message: $ErrorMessage"

}

 

 

Please mark helpful or correct 🙂 

View solution in original post

14 REPLIES 14

DanielCordick
Mega Patron
Mega Patron

To get this working i created a custom action using this PS script: In my set up, i have a table with all the OU's I need. then dynamically set it in my flow.

 

Create an action | ServiceNow Docs

 

if (test-path env:\SNC_username) {
$UserName = $env:SNC_username;
$OU = $env:SNC_OU;
};


try {

Get-ADUser -Identity "$UserName" | Move-ADObject -TargetPath "$OU"

Write-Host "SUCCESS: Moved $UserName to $OU"

}

catch {

$ErrorMessage = $_.Exception.Message

Write-Host "FAILED to move $UserName to $OU. Error message: $ErrorMessage"

}

 

 

Please mark helpful or correct 🙂 

Worked a treat, thanks for your help Daniel

How did you end up importing all of your OUs into ServiceNow? Also any easy way to do any organization when importing?

Hi,
Type can be "Computer","User","Group", you can remove this filter as well.

$type= $type-replace "%27","'";
$ComputerOUs=Get-ADOrganizationalUnit  -Properties CanonicalName -Filter 'Name -like $type' -Credential $cred -Server $computer
foreach($ou in $ComputerOUs)
{
$Name = $ou.distinguishedname
$outdata+=$Name+"|"
}
Write-Host $outdata


 


Thanks and Regards,

Saurabh Gupta