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

adusersadgroups [2019/02/08 15:06] (current)
sjoerd created
Line 1: Line 1:
 += Add AD User or Remove AD User from AD Group =
  
 +The scripts below will give you a taste of adding or removing groups based on a CSV inputfile. ​
 +
 += Add Users to Group =
 +<code powershell>​
 +### Quick script for adding users in to groups based on a CSV input file
 +
 +Start-Transcript "​outputfileadd.log"​
 +
 +### CSV Variables
 +### CSV Format
 +### Header: UserSamAccountName,​GroupSamAccountName
 +### Datalines: samaccountname,​samaccountname
 +$csvfile = "​\\filerepository\dfs\SCRIPT_REPOSITORY$\Organizational changes\orgchangeinput-add.csv"​
 +
 +### Get all changes from inputfile
 +$changes = Import-CSV $csvfile
 +
 +### Get number of changes ​
 +$changescount = $changes.count
 +Write-host "​Number of changes in csv: $changescount " -ForegroundColor green
 +
 +Read-Host -Prompt 'You are about to add group memberships to AD. If you are sure, press ENTER to continue or close the script to cancel'​
 +
 +ForEach ($change in $changes){
 + $usersam = $change.UserSamAccountName
 + $groupsam = $change.GroupSamAccountName
 + #Check for valid user
 + if (Get-ADObject -Filter {objectClass -eq "​user"​ -and samAccountName -eq $usersam}){
 + #User is valid
 + #now check for valid group
 + if (Get-ADObject -Filter {objectClass -eq "​group"​ -and samAccountName -eq $groupsam}){
 + #Group is valid, we can add the user to the group
 + Add-ADGroupMember -Identity $groupsam -Members $usersam -Confirm:​$false
 + Write-Host "​Success. Added $usersam to $groupsam"​ -ForegroundColor green
 + }else{
 + # Group is not valid
 + Write-Host "​Failed. $groupsam is not a valid Group SamAccountName"​ -ForegroundColor red
 + }
 + }else{
 + #user is not valid
 + Write-Host "​Failed. $usersam is not a valid User SamAccountName"​ -ForegroundColor red
 + }
 +}
 +
 +Stop-Transcript
 +</​code>​
 +
 += Remove Users from Group =
 +
 +<code powershell>​
 +### Quick script for adding users in to groups based on a CSV input file
 +
 +Start-Transcript "​outputfileremove.log"​
 +
 +### CSV Variables
 +### CSV Format
 +### Header: UserSamAccountName,​GroupSamAccountName
 +### Datalines: samaccountname,​samaccountname
 +$csvfile = "​\\filerepository\dfs\SCRIPT_REPOSITORY$\Organizational changes\orgchangeinput-remove.csv"​
 +
 +### Get all changes from inputfile
 +$changes = Import-CSV $csvfile
 +
 +### Get number of changes ​
 +$changescount = $changes.count
 +Write-host "​Number of changes in csv: $changescount " -ForegroundColor green
 +
 +Read-Host -Prompt 'You are about to remove group memberships to AD. If you are sure, press ENTER to continue or close the script to cancel'​
 +
 +ForEach ($change in $changes){
 + $usersam = $change.UserSamAccountName
 + $groupsam = $change.GroupSamAccountName
 + #Check for valid user
 + if (Get-ADObject -Filter {objectClass -eq "​user"​ -and samAccountName -eq $usersam}){
 + #User is valid
 + #now check for valid group
 + if (Get-ADObject -Filter {objectClass -eq "​group"​ -and samAccountName -eq $groupsam}){
 + #Group is valid, we can add the user to the group
 + Remove-ADGroupMember -Identity $groupsam -Members $usersam -Confirm:​$false
 + Write-Host "​Success. Removed $usersam from $groupsam"​ -ForegroundColor green
 + }else{
 + # Group is not valid
 + Write-Host "​Failed. $groupsam is not a valid Group SamAccountName"​ -ForegroundColor red
 + }
 + }else{
 + #user is not valid
 + Write-Host "​Failed. $usersam is not a valid User SamAccountName"​ -ForegroundColor red
 + }
 +}
 +Stop-Transcript
 +</​code>​
 +
 +{{tag>​scripts ad}}
adusersadgroups.txt ยท Last modified: 2019/02/08 15:06 by sjoerd