Forum Discussion
Cannot convert 'System.Object[]' to the type 'Microsoft.ActiveDirectory.Management.ADUser'
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:
Set-ADUser : Cannot convert 'System.Object[]' to the type 'Microsoft.ActiveDirectory.Management.ADUser' required by parameter 'Manager'. Specified method is not supported.
At line:1 char:52
+ Set-ADUser -Identity $user.SamAccountName -Manager $manager
+ ~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-ADUser], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.SetADUser
#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
- DeletedApr 11, 2019... i solved it..
-> "$manager" (quotation marks)
That's it.
- Deleted... i solved it..
-> "$manager" (quotation marks)
That's it.- RandomDaveBrass ContributorAnd 3 years later, you solved my issue. Thanks!