Friday, October 9, 2015

Powershell Script - Change Contractor Info

Crafted this gem today.. I need to change some AD attributes for contracted personnel, namely adding a 'c-' to the beginning of their email, UPN and SAMAccountName. Adding a '(Contractor)' to the end of the DisplayName field and changing a couple proxyaddresses.


First things, the csv used has the following columns:

name, mail, displayname, samaccountname, proxyaddress_0, proxyaddress_1, proxyaddress_2

Code:

#========================================================================
# Created with: SAPIEN Technologies, Inc., PowerShell Studio 2012 v3.1.26
# Created on:   10/9/2015 1:46 PM
# Created by:   Ben Hart
# Organization: UnifiedBrands
# Filename:     Change-ContractorInfo.ps1
#========================================================================


Import-module ActiveDirectory
Import-Csv -Path d:\Users\username\Desktop\test.csv | foreach-object {


$email = $_.mail
$Displayname = $_.displayName
$UPN = $_.mail
$sam = $_.samaccountname
$proxy0 = $_.Proxyaddress_0
$proxy1 = $_.Proxyaddress_1
$proxy2 = $_.Proxyaddress_2


set-aduser -identity $sam -emailaddress $email -UserPrincipalName $email -DisplayName $Displayname



Set-ADUser -Identity $sam -Replace @{proxyaddresses=@("SMTP:"+$email)}
Set-ADUser -Identity $sam -Add @{proxyaddresses="$proxy0"}
Set-ADUser -Identity $sam -Add @{proxyaddresses="$proxy1"}
Set-ADUser -Identity $sam -Add @{proxyaddresses="$proxy2"}
Set-ADUser -identity $sam -Replace @{targetaddress="$email"}

}

No comments:

Post a Comment