SOLVED

Cannot convert 'System.Object[]' to the type 'Microsoft.ActiveDirectory.Management.ADUser'

%3CLINGO-SUB%20id%3D%22lingo-sub-435723%22%20slang%3D%22en-US%22%3ECannot%20convert%20'System.Object%5B%5D'%20to%20the%20type%20'Microsoft.ActiveDirectory.Management.ADUser'%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-435723%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20together%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ei'm%20quite%20new%20to%20Powershell%20and%20need%20a%20little%20'shove'.%20%3AD%3C%2Fimg%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhat%20i%20want%20to%20achive%3A%3C%2FP%3E%3CP%3ESetting%20the%20Manager%20of%20a%20User%20based%20on%20the%20%22ManagedBy%22%20attribute%20of%20an%20OU.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhat%20my%20Script%20Snippet%20looks%20like%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23%20Getting%20one%20user%20%26amp%3B%20build%26nbsp%3Bcorrect%20variables%20(already%20working%20for%20some%20other%20scripts)%3CBR%20%2F%3E%24user%20%3D%20Get-ADUser%20-Identity%20ABC%3CBR%20%2F%3E%24userOUDistName%20%3D%20(%24user.DistinguishedName%20-split%20%22%3D%22%2C3)%5B-1%5D%3CBR%20%2F%3E%24UserOUName%20%3D%20%24userOUDistName.Split(%22%2C%22)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23%20Explanation%20%23%3C%2FP%3E%3CP%3E%3CSTRONG%3E%24user%3C%2FSTRONG%3E%20contains%20the%20complete%20information%20of%20the%20user%3C%2FP%3E%3CP%3E%3CSTRONG%3E%24userOUDistName%3C%2FSTRONG%3E%20contains%20a%20string%20like%20%22Department%2COU%3DUsers%2COU%3DTEST%2CDC%3DDOMAIN%2CDC%3Dde%22%3C%2FP%3E%3CP%3ESo%20%22Department%22%20is%20the%20name%20of%20OU.%3C%2FP%3E%3CP%3E%3CSTRONG%3E%24userOUName%3C%2FSTRONG%3E%20contains%20the%20splitted%20strings.%3C%2FP%3E%3CP%3E%3CSTRONG%3E%24userOUName%5B0%5D%3C%2FSTRONG%3E%20contains%20the%20exact%20word%20of%20the%20OU%20(in%20this%20case%20%22Department%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CEM%3E%23%23%23%20Test%201%20%23%23%23%3C%2FEM%3E%3C%2FP%3E%3CP%3E%3CEM%3E%23%20Fill%20in%20the%20ManagedBy%20into%20the%20new%20variable%20%24manager%20%23%3C%2FEM%3E%3CBR%20%2F%3E%24manager%20%3D%20(Get-ADOrganizationalUnit%20-Filter%20*%20-Properties%20*%20%7C%20Where-Object%20%7B%24_.name%20-eq%20%24userOUName%5B0%5D%7D).ManagedBy%3C%2FP%3E%3CP%3E%3CEM%3E%23%20so%20after%20this%20i%20know%20who%20should%20be%20the%20manager%20for%20the%20user%20%23%3C%2FEM%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3E%23not%20working%23%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3ESet-ADUser%20-Identity%20%24user.SamAccountName%20-Manager%20%24manager%3C%2FP%3E%3CP%3E%3CU%3EError%3A%3C%2FU%3E%3C%2FP%3E%3CH6%20id%3D%22toc-hId-918711742%22%20id%3D%22toc-hId-918711748%22%20id%3D%22toc-hId-918711748%22%20id%3D%22toc-hId-918711748%22%3ESet-ADUser%20%3A%20Cannot%20convert%20'System.Object%5B%5D'%20to%20the%20type%20'Microsoft.ActiveDirectory.Management.ADUser'%20required%20by%20parameter%20'Manager'.%20Specified%20method%20is%20not%20supported.%3CBR%20%2F%3EAt%20line%3A1%20char%3A52%3CBR%20%2F%3E%2B%20Set-ADUser%20-Identity%20%24user.SamAccountName%20-Manager%20%24manager%3CBR%20%2F%3E%2B%20~~~~~~~~%3CBR%20%2F%3E%2B%20CategoryInfo%20%3A%20InvalidArgument%3A%20(%3A)%20%5BSet-ADUser%5D%2C%20ParameterBindingException%3CBR%20%2F%3E%2B%20FullyQualifiedErrorId%20%3A%20CannotConvertArgument%2CMicrosoft.ActiveDirectory.Management.Commands.SetADUser%3C%2FH6%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3E%23working%23%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%23%20writing%20the%20exact%20same%20value%20by%20hand%20into%20a%20new%20variable%20%23%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%24test%20%3D%20'Department%2COU%3DUsers%2COU%3DTEST%2CDC%3DDOMAIN%2CDC%3Dde'%3CBR%20%2F%3ESet-ADUser%20-Identity%20%24user.SamAccountName%20-Manager%20%24test%3C%2FP%3E%3CP%3E%3CU%3EError%3A%3C%2FU%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3E%23working%23%3C%2FSTRONG%3E%3CBR%20%2F%3ESet-ADUser%20-Identity%20%24user.SamAccountName%20-Manager%20'CN%3DNAME%5C%2C%20NAME%2COU%3DNAME%2COU%3DNAME%2COU%3DNAME%2CDC%3DNAME%2CDC%3Dde'%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20already%20tried%20this%20(the%20same%20results%20as%20before)%3C%2FP%3E%3CP%3E%24manager%20%3D%20(Get-ADOrganizationalUnit%20-Filter%20*%20-Properties%20*%20%7C%20Where-Object%20%7B%24_.name%20-eq%20%24userOUName%5B0%5D%7D).ManagedBy%20%7C%20Out-String%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20provide%20me%20some%20hint..%20%3AD%3C%2Fimg%3E%3C%2FP%3E%3CP%3EThank%20you%20very%20much%20in%20advance.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGreetings%2C%3C%2FP%3E%3CP%3EPatrick%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-435723%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-436466%22%20slang%3D%22en-US%22%3ERe%3A%20Cannot%20convert%20'System.Object%5B%5D'%20to%20the%20type%20'Microsoft.ActiveDirectory.Management.ADUser'%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-436466%22%20slang%3D%22en-US%22%3E...%20i%20solved%20it..%3CBR%20%2F%3E-%26gt%3B%20%22%24manager%22%20(quotation%20marks)%3CBR%20%2F%3EThat's%20it.%3C%2FLINGO-BODY%3E
Highlighted
Deleted
Not applicable

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

1 Reply
Highlighted
Best Response confirmed by Vasil Michev (MVP)
Solution
... i solved it..
-> "$manager" (quotation marks)
That's it.