Little Background:-
There was request from client to segregate the different types of account exists in the customer internal domain. It may be difficult if those accounts are not defined already with their type of usage for example : Daily user normal account, Administrative account, Workstation Administrator and Shared accounts.
So, we decided clean-up and setup procedure to update the "EmployeeType" attribute for all the users..
As a initial step, we have exported all the enabled accounts and add new column named Employeetype.
By naming convention, we could different account types and update the new column.
Once it's completed, we prepare the Input file with header "Username" & AccountType"
Before processing, I take note of the existing employeetype for given user in input file.
Sample cmdlet for easy reference:
Import-csv .\Userslist.csv | Foreach-object {Get-Aduser -Identity $_.Username -property Employeetype | select Name,Samaccountname,employeetype}
After executing the script :-
# *****************Start of Script ********************
# Make sure that input CSV file have header like Username, AccountType
#Preparing the parameter input file
Param (
[Parameter(Mandatory=$True)]
[string]$InputFile
)
# Preparing the Log file naming format ( Default stored in same place where script is executed )
$logdate = (Get-Date -Format yyyyMMdd-hh-mm)
$log= $logdate+"_EmployeeType_Update"+".log"
# Importing Active Directory Module & Getting Logged on Domain Name information
Import-Module ActiveDirectory
# Looping through the each line of user from the Input file
ForEach ($Eachline in (Import-csv $InputFile)){
$Sam = $Eachline.Username
$Acctype = $Eachline.AccountType
$ADUser = Get-ADUser -Filter {SamAccountName -eq $sam}
if ( $ADuser -ne $null )
{
Set-aduser $ADUser -replace @{Employeetype="$Acctype"} -Verbose
$Info= "EmployeeType field updated for $ADUser.Samaccountname"
$Info | out-file -Append $log
}
else
{
$info="$Sam ID not found in Domain. Please recheck"
$info | out-file -Append $log
}
}
# *****************END of Script ********************
No comments:
Post a Comment