SHIFT

--- Sjoerd Hooft's InFormation Technology ---

User Tools

Site Tools


adusersadgroups
Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
adusersadgroups [2019/02/08 15:06]
sjoerd created
adusersadgroups [2019/03/15 16:01] (current)
sjoerd
Line 1: Line 1:
-Add AD User or Remove AD User from AD Group =+Organization Change - Bulk AD Changes - Groups - Departments ​=
  
 The scripts below will give you a taste of adding or removing groups based on a CSV inputfile. ​ The scripts below will give you a taste of adding or removing groups based on a CSV inputfile. ​
Line 91: Line 91:
 } }
 Stop-Transcript Stop-Transcript
 +</​code>​
 +
 += Change Department =
 +
 +<code powershell>​
 +### Quick script for adding users in to groups based on a CSV input file
 +
 +Start-Transcript "​outputfiledepartment.log"​
 +
 +### CSV Variables
 +### CSV Format
 +### Header: UserSamAccountName,​DepartmentName
 +### Datalines: samaccountname,​department
 +$csvfile = "​\\networkstorage\dfs\SCRIPT_REPOSITORY$\Org changes\2019-March\departmentinput.csv"​
 +
 +### Get all changes from inputfile
 +$changes = Import-CSV $csvfile
 +
 +### Get number of changes ​
 +$changescount = $changes.count
 +Write-host "​Number of changed departments in csv: $changescount " -ForegroundColor green
 +
 +Read-Host -Prompt 'You are about to add change departments for users in AD. If you are sure, press ENTER to continue or close the script to cancel'​
 +
 +ForEach ($change in $changes){
 + $usersam = $change.UserSamAccountName
 + $department = $change.DepartmentName
 + #Get Current department ​
 + $user = Get-ADUser $usersam -properties samaccountname,​department
 + $currentdepartment = $user.department
 + Write-host "​$usersam : Change $currentdepartment to $department " -ForegroundColor green
 + Set-ADUser $usersam -Department $department ​
 +}
 +Stop-Transcript
 +</​code>​
 +
 += Check =
 +<code powershell>​
 +# get all ad users
 +$adusers = Get-ADUser -Filter * -properties * -SearchBase "​OU=Users,​OU=DELFT,​DC=ad,​DC=shift,​DC=com"​
 +
 +$csv = "​orgcheck.csv"​
 +
 +$allusers = @()
 +
 +Foreach ($user in $adusers){
 + $userinfo = ""​ | select Name,​SamAccountName,​Department,​Title,​enabled,​Groups
 + $userinfo.name = $user.name ​
 + $sam = $user.samaccountname
 + $userinfo.samaccountname = $sam
 + $userinfo.department = $user.department
 + $userinfo.title = $user.title
 + $userinfo.enabled = $user.enabled
 + $groups = "No group Membership"​
 + $groups = Get-ADUser $sam -Properties memberof | select -ExpandProperty memberof
 + $allgroups = $groups -join '; '
 + $userinfo.groups = $allgroups
 + $allusers += $userinfo
 +}
 +
 +$allusers | export-csv -notypeinformation $csv
 +
 +Send-MailMessage -To "​sjoerd_getshifting.com"​ -From "​sjoerd_getshifting.com"​ -SmtpServer "​smtp"​ -Subject "Org change controle csv" -Body "See attachment"​ -BodyAsHtml -Attachments $csv 
 </​code>​ </​code>​
  
 {{tag>​scripts ad}} {{tag>​scripts ad}}
adusersadgroups.1549634772.txt.gz ยท Last modified: 2019/02/08 15:06 by sjoerd