Hi Spiceworks,<\/p>\n
First time poster, and aspiring IT project lead. I was tasked with migrating our wonky user-network drive solution to OneDrive, one of the steps will involve a task sequence of moving users and their computers to a different OU. In Active Directory computers are labelled with their appropriate users in the “description” field, I want to update the “managed by” field with the “description” field’s current contents. Here is my preliminary efforts, I’ve tried a few different options but this seems to be the most concise.<\/p>\n
$OU = \"OU=Test,OU=Example,DC=Domain,DC=local\"\n$ComputerNames = Get-ADComputer -Filter * -SearchBase \"$OU\" | Select\n\nFOREACH ($Computer in $ComputerNames) {\n\n$asdf = Get-ADComputer -Identity $Computer -Properties Description | Select Description\n\nSet-ADComputer $Computer -ManagedBy ($asdf)\n<\/code><\/pre>\n
Advertisement
Any and all insight is very much appreciated, thank you much!<\/p>","upvoteCount":8,"answerCount":8,"datePublished":"2019-02-27T20:55:45.000Z","author":{"@type":"Person","name":"smitty-werbenjagermanjensen","url":"https://community.spiceworks.com/u/smitty-werbenjagermanjensen"},"acceptedAnswer":{"@type":"Answer","text":"
Here’s what I would do -<\/p>\n
$OU = \"OU=Test,OU=Example,DC=Domain,DC=local\"\n\n$Computers = Get-ADComputer -Filter * -SearchBase $OU -Properties Description\n\nFOREACH ($C in $Computers) {\n\n $matchProperty = 'DisplayName' # change if want to match Description to a different user attribute\n\n try\n {\n $filter = \"$matchProperty -like '$($c | Select -ExpandProperty Description)'\" # filters work best if you pass the whole filter as a string\n $User= @() # clear the variable\n $User= Get-ADUser -Filter $filter\n }\n catch\n {\n # do nothing, because an error on Get-ADUser is acceptable, and the -ErrorAction parameter on AD cmdlets is ignored.\n }\n\n if ($User) \n {\n Write-Output -InputObject \"Setting $($C.Name) to '$($User.DistinguishedName)'\"\n Set-ADComputer -Identity $C -ManagedBy $User.DistinguishedName \n }\n else\n {\n Write-Warning -Message \"Could not match description for $($c.name) '$($c.description)' to a user account\"\n }\n}\n<\/code><\/pre>\nYou might want to run this script multiple times, so to ensure you’re not updating all the Computer objects that are already set correctly you could do this -<\/p>\n
$OU = \"OU=Test,OU=Example,DC=Domain,DC=local\"\n\n$Computers = Get-ADComputer -Filter * -SearchBase $OU -Properties Description, ManagedBy\n\nFOREACH ($C in $Computers) {\n\n $matchProperty = 'DisplayName' # change if want to match Description to a different user attribute \n\n try\n {\n $filter = \"$matchProperty -like '$($c | Select -ExpandProperty Description)'\" \n $User= @()\n $User= Get-ADUser -Filter $filter\n }\n catch\n {\n # do nothing, because an error on Get-ADUser is acceptable, and the -ErrorAction parameter on AD cmdlets is ignored.\n }\n\n if ($User) \n {\n If ($user.DistinguishedName -eq $C.ManagedBy)\n {\n Write-Verbose -Message \"Skipped $($c.name). Already set correctly.\" -Verbose # remove the -verbose to hide this output\n }\n else\n {\n Write-Output -InputObject \"Setting $($C.Name) to '$($User.DistinguishedName)'\"\n Set-ADComputer -Identity $C -ManagedBy $User.DistinguishedName \n }\n }\n else\n {\n Write-Warning -Message \"Could not match description for $($c.name) '$($c.description)' to a user account\"\n }\n}\n<\/code><\/pre>\nAnd remember to put the “-WhatIf” flag on the Set-ADComputer command on the first run to see what impact it would have, and logic check what it would do.<\/p>","upvoteCount":1,"datePublished":"2019-02-28T18:24:03.000Z","url":"https://community.spiceworks.com/t/advice-on-active-directory-powershell/699772/8","author":{"@type":"Person","name":"iananthony","url":"https://community.spiceworks.com/u/iananthony"}},"suggestedAnswer":[{"@type":"Answer","text":"
Hi Spiceworks,<\/p>\n
First time poster, and aspiring IT project lead. I was tasked with migrating our wonky user-network drive solution to OneDrive, one of the steps will involve a task sequence of moving users and their computers to a different OU. In Active Directory computers are labelled with their appropriate users in the “description” field, I want to update the “managed by” field with the “description” field’s current contents. Here is my preliminary efforts, I’ve tried a few different options but this seems to be the most concise.<\/p>\n
$OU = \"OU=Test,OU=Example,DC=Domain,DC=local\"\n$ComputerNames = Get-ADComputer -Filter * -SearchBase \"$OU\" | Select\n\nFOREACH ($Computer in $ComputerNames) {\n\n$asdf = Get-ADComputer -Identity $Computer -Properties Description | Select Description\n\nSet-ADComputer $Computer -ManagedBy ($asdf)\n<\/code><\/pre>\nAny and all insight is very much appreciated, thank you much!<\/p>","upvoteCount":8,"datePublished":"2019-02-27T20:55:45.000Z","url":"https://community.spiceworks.com/t/advice-on-active-directory-powershell/699772/1","author":{"@type":"Person","name":"smitty-werbenjagermanjensen","url":"https://community.spiceworks.com/u/smitty-werbenjagermanjensen"}},{"@type":"Answer","text":"
Welcome!<\/p>\n
What exactly is not working or the error you are getting with the code you have?<\/p>\n
If you post code, please use the ‘Insert Code’ button. Please and thank you!<\/p>\n