Author Posts

March 18, 2016 at 12:41 pm

Pulling my hair out with a script to change a user's manager field via a .csv file

user.csv – example
userid,streetmanager,physicaldeliveryofficename
user1,manager1,address1

physicaldeliveryofficename works but I can not add to the manager field?

Any assistance would be great.....


If ((Get-PSSnapin | where {$_.Name -match "Quest.ActiveRoles"}) -eq $null)
{
	Add-PSSnapin Quest.ActiveRoles.ADManagement
}


# Import CSV file that is populated with user data

$now=Get-Date -format “dd-MMM-yyyy HH:mm”

# replace : by -

$now = $now.ToString().Replace(“:”, “-”)

$data = import-csv $args[0]

# Loop thru the data from CSV

foreach ($i in $data)

{

$userobject = get-qaduser $i.userid -IncludedProperties “Co”,”C”

# get-qaduser -Identity hillsc -IncludedProperties office | Select office
#$userobject = get-qaduser $i.userid -IncludeAllProperties #“CO”,”C”,"office","manager"

$Title = $userobject.Title
$Address = $userobject.StreetAddress
$City = $userobject.City
$Office = $userobject.physicalDeliveryOfficeName
$Zip = $userobject.PostalCode
$Streetmanager = $userobject.manager



}
###############################################Manager###############################################################

if ($manager -like $null)
{
Write-host $userobject has blank manager
Write-host var i $i
$Log1 = ".\logs\" + "BlankManager” + $now + “.log”
Add-content  $Log1 “$userobject has blank manager”
Get-QADUser $i.userid | Set-QADUser -ObjectAttributes @{Streetmanager = $i.manager.Trim()}
$userobject = get-qaduser $i.userid -IncludedProperties “Co”
$manager = $userobject.manager
Write-host $userobject has * $manager * as manager
$Log3 = ".\logs\" + "SetAddress” + $now + “.log”
Add-content  $Log3 “For $userobject $manager as manager has been set”
} 
else
{
$Log2 = ".\logs\" + "Currentmanager” + $now + “.log”
Write-host $userobject has $manager as Current manager
Add-content  $Log2 “$userobject already has $manager as manager”
}

###############################################Office###############################################################


if ($office -like $null)
{
Write-host $userobject has blank Office name
$Log1 = ".\logs\" + "BlankOfficename” + $now + “.log”
Add-content  $Log1 “$userobject has blank office”
Get-QADUser $i.userid | Set-QADUser -ObjectAttributes @{physicalDeliveryOfficeName = $i.physicalDeliveryOfficeName.Trim()}
$userobject = get-qaduser $i.userid -IncludedProperties “office”
$office = $userobject.office
Write-host $userobject has * $office * as office
$Log3 = ".\logs\" + "SetAddress” + $now + “.log”
Add-content  $Log3 “For $userobject $office as office has been set”
} 
else
{
$Log2 = ".\logs\" + "Currentoffice” + $now + “.log”
Write-host $userobject has $office as Current office
Add-content  $Log2 “$userobject already has $office as office”
}

Code was originally written by Vikas Sukhija and I have just tried to modify it.

March 19, 2016 at 12:10 am

Hi Chris,

Are you sending the Manager attribute as a Distinguished Name?

If not you can add a function to do the translation for you.

https://gallery.technet.microsoft.com/scriptcenter/f7b246a4-95a2-4408-b1a1-73b53003883c

Regards,

Michael

March 19, 2016 at 12:32 pm

Thanks Michael and no was trying as the CN name. Will take a read.

March 21, 2016 at 5:53 am

looks like you've got your attribute and csv column mixed up in your set command. so this:

Get-QADUser $i.userid | Set-QADUser -ObjectAttributes @{Streetmanager = $i.manager.Trim()}

should be this:

Get-QADUser $i.userid | Set-QADUser -ObjectAttributes @{manager = $i.Streetmanager.Trim()}