Author Posts

August 11, 2018 at 12:04 pm

Hello everyone,

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?

https://github.com/kunaludapi/powershell-change-aduser-manager/blob/master/Update-ADUserManger.ps1

 

Thank you

August 11, 2018 at 1:11 pm

Hi Llamllam,

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 {

}
}

August 11, 2018 at 1:38 pm

Hi Llamllam,
This should accomplish what you want, it will accept input from a csv via the pipeline. You would use it like this,

Import-CSV -Path X.csv | Foreach-Object {Update-AdManager -UserFullName $_.UserColumnHeader -ManagerFullName $_.ManagerColumnHeader -WhatIf}
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

August 11, 2018 at 3:40 pm

Johnathan,

Wow thank you so much,  I owe you big time!

I do have one issue though, I'm wondering if you could still help me out with, I'm getting this error:

Get-Aduser : Invalid type 'System.String[]'.
Parameter name: name
At line:18 char:27
+ ...    $ManagerDetails = Get-Aduser -Filter {Name -like $ManagerFullName}
+                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Comm
ands.GetADUser

What if: Performing the operation "Set-Aduser -Manager Boss Hoss" on target "Rosco Coltrane".

August 11, 2018 at 3:54 pm

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

Import-CSV C:\Scripts\Manager.csv -Header UserFirstName, UserLastName, ManagerFirstName, ManagerLastName

August 11, 2018 at 9:32 pm

Rob, you are the man.  Worked perfectly!

Thank you so much for this, I'm still pretty green with PS so this is a huge help!