Apr 11 2019 03:00 AM
Apr 11 2019 03:00 AM
Hi together,
i'm quite new to Powershell and need a little 'shove'. :D
What i want to achive:
Setting the Manager of a User based on the "ManagedBy" attribute of an OU.
What my Script Snippet looks like:
# Getting one user & build correct variables (already working for some other scripts)
$user = Get-ADUser -Identity ABC
$userOUDistName = ($user.DistinguishedName -split "=",3)[-1]
$UserOUName = $userOUDistName.Split(",")
# Explanation #
$user contains the complete information of the user
$userOUDistName contains a string like "Department,OU=Users,OU=TEST,DC=DOMAIN,DC=de"
So "Department" is the name of OU.
$userOUName contains the splitted strings.
$userOUName[0] contains the exact word of the OU (in this case "Department"
### Test 1 ###
# Fill in the ManagedBy into the new variable $manager #
$manager = (Get-ADOrganizationalUnit -Filter * -Properties * | Where-Object {$_.name -eq $userOUName[0]}).ManagedBy
# so after this i know who should be the manager for the user #
#not working#
Set-ADUser -Identity $user.SamAccountName -Manager $manager
Error:
#working#
# writing the exact same value by hand into a new variable #
$test = 'Department,OU=Users,OU=TEST,DC=DOMAIN,DC=de'
Set-ADUser -Identity $user.SamAccountName -Manager $test
Error:
#working#
Set-ADUser -Identity $user.SamAccountName -Manager 'CN=NAME\, NAME,OU=NAME,OU=NAME,OU=NAME,DC=NAME,DC=de'
I already tried this (the same results as before)
$manager = (Get-ADOrganizationalUnit -Filter * -Properties * | Where-Object {$_.name -eq $userOUName[0]}).ManagedBy | Out-String
Please provide me some hint.. :D
Thank you very much in advance.
Greetings,
Patrick
Apr 11 2019 05:15 AM
Apr 11 2019 05:15 AM
SolutionJul 29 2022 02:41 PM