I am trying to modify the Manager attribute from a CSV file I was given that only has the users “First Last” and the managers “First Last” names in it. I am trying to figure out what the best way to do this is, should I use a separate script to get those names into a CSV that has the samAccountName and DistinguishedName and then just use a simple script to bring that in or is there a way I can modify this script I found from kunaludapi on Github?
Are you trying to pull the data from AD into a report or are you trying to update the attribute in AD using the data from the report you have? if you want to update AD from the CSV file then this script should do the trick for you. You would run it like this,
Import-Csv -Path X.csv | foreach-object{Update-ADManager -UserFullName $.UserColumnHeader -ManagerFullName $.ManagerColumnHeader -Whatif}
Replace UserColumnHeader and ManagerColumnHeader with whatever the headers are from your CSV
The Whatif is there to stop it making changes until you are happy with what it is changing.
function Update-ADManager {
[cmdletbinding(supportsshouldprocess=$true,ConfirmImpact='Medium')]
Param (
[Parameter(Mandatory,ValueFromPipelineByPropertyName,ValueFromPipeline)]
[string[]]$UserFullName,
[parameter(Mandatory,ValueFromPipelineByPropertyName,ValueFromPipeline)]
[string[]]$ManagerFullName
)
Begin {
if ((Get-Module ActiveDirectory) -eq $False) {
Import-Module ActiveDirectory
}
}
Process {
$UserDetails = Get-Aduser -Filter {Name -like $UserFullName}
$ManagerDetails = Get-Aduser -Filter {Name -like $ManagerFullName}
if (($UserDetails.count) -gt 1) {
Throw "Multiple users found for $UserFullName"
}
if (($ManagerDetails.count) -gt 1) {
Throw "Multiple users found with $ManagerFullName"
}
if ($PSCmdlet.ShouldProcess($UserFullName,"Set-Aduser -Manager $ManagerFullName")) {
Try {
Set-ADuser -Identity $UserDetails.SamaccountName -Manager $ManagerDetails.SamaccountName
}
Catch {
Write-Error "Unable to set AD manager for $UserFullName"
}
}
}
End {
}
}
function Update-ADManager {
[cmdletbinding(supportsshouldprocess=$true,ConfirmImpact='Medium')]
Param (
[Parameter(Mandatory,ValueFromPipelineByPropertyName,ValueFromPipeline)]
[string[]]$UserFullName,
[parameter(Mandatory,ValueFromPipelineByPropertyName,ValueFromPipeline)]
[string[]]$ManagerFullName
)
Begin {
if ((Get-Module ActiveDirectory) -eq $False) {
Import-Module ActiveDirectory
}
}
Process {
Foreach ($User in $UserFullName) {
$UserDetails = Get-Aduser -Filter {Name -like $User}
$ManagerDetails = Get-Aduser -Filter {Name -like $ManagerFullName}
if (($UserDetails.count) -gt 1) {
Throw "Multiple users found for $UserFullName"
}
if (($ManagerDetails.count) -gt 1) {
Throw "Multiple users found with $ManagerFullName"
}
if ($PSCmdlet.ShouldProcess($UserFullName,"Set-Aduser -Manager $ManagerFullName")) {
Try {
Set-ADuser -Identity $UserDetails.SamaccountName -Manager $ManagerDetails.SamaccountName
}
Catch {
Write-Error "Unable to set AD manager for $UserFullName"
}
}
}
}
End {
}
}
The -Whatif is there so you can confirm it works as expected before allowing it to actually update the manager of the user. Replace the UserColumnHeader and ManagerColumnHeader with the header names from your CSV
An important part of this is how the names are formatted in AD. If the users are formatted Last, First MiddleInit, then you need to reformat the names and the LIKE doesn’t have any wildcards. If the First and Last name is provided, you should search those attributes in Active Directory. Here is a modified version of Jonathon’s function:
function Set-ADManager {
[cmdletbinding(supportsshouldprocess=$true,ConfirmImpact='Medium')]
Param (
[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
[string]$UserFirstName,
[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
[string]$UserLastName,
[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
[string]$ManagerFirstName,
[parameter(Mandatory,ValueFromPipelineByPropertyName)]
[string]$ManagerLastName
)
Begin {
if ((Get-Module ActiveDirectory) -eq $False) {
Import-Module ActiveDirectory
}
}
process {
Write-Verbose ("Searching for user {0} {1}" -f $UserFirstName, $UserLastName)
$UserDetails = Get-Aduser -Filter {(GivenName -eq $UserFirstName) -and (SurName -eq $UserLastName)}
if (@($UserDetails).Count -eq 1) {
Write-Verbose ("Searching for manager {0} {1}" -f $ManagerFirstName, $ManagerLastName)
$ManagerDetails = Get-Aduser -Filter {(GivenName -eq $ManagerFirstName) -and (SurName -eq $ManagerLastName)}
if (@($ManagerDetails).Count -eq 1) {
if ($PSCmdlet.ShouldProcess(('{0} ({1})' -f $UserDetails.Name, $UserDetails.SamAccountName) ,('Set-Aduser -Manager {0} ({1})' -f $ManagerDetails.Name, $ManagerDetails.SamAccountName))) {
try {
$params = @{
Identity = $UserDetails
Manager = $ManagerDetails
ErrorAction = "Stop"
}
Set-ADuser @params
}
catch {
$msg = 'Error setting manager for user {0} {1}. {2}' -f $UserFirstName, $UserLastName, $_
Throw $msg
}
}
}
else {
$msg = 'Error finding user {0} {1}, found {2} Active Directory records' -f $ManagerFirstName, $ManagerLastName, @($ManagerDetails).Count
Throw $msg
}
}
else {
$msg = 'Error finding user {0} {1}, found {2} Active Directory records' -f $UserFirstName, $UserLastName, @($UserDetails).Count
Throw $msg
}
}
end {
}
}
$csv = Import-CSV C:\Scripts\Managers.csv
$csv | Set-ADManager -Verbose -WhatIf
Keep in mind, this assumes your CSV column headers are UserFirstName, UserLastName, ManagerFirstName, ManagerLastName. If they are not, you can change the header with Import-CSV