Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add script to automate account deletion #1508

Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
bc474f7
draft script
edwardchalstrey1 Jul 20, 2023
3ed34b3
use correct var
edwardchalstrey1 Jul 20, 2023
f2bdc38
correct path
edwardchalstrey1 Jul 20, 2023
c4b7688
rename file
edwardchalstrey1 Jul 21, 2023
9a6c2a4
loop users
edwardchalstrey1 Jul 21, 2023
ae00262
unassignedUsers SamAccountName list
edwardchalstrey1 Jul 21, 2023
84eb88f
negate
edwardchalstrey1 Jul 21, 2023
3e3904b
delete user
edwardchalstrey1 Jul 21, 2023
34254bc
delete the user
edwardchalstrey1 Jul 21, 2023
2aa274a
DC1 script
edwardchalstrey1 Jul 21, 2023
d051ac6
remove write-out
edwardchalstrey1 Jul 21, 2023
a7e201b
fin prev commit
edwardchalstrey1 Jul 21, 2023
e8f9cef
perform adsync after
edwardchalstrey1 Jul 21, 2023
dbe21e2
Merge branch 'develop' into automate-acct-deletion
edwardchalstrey1 Aug 22, 2023
ed1c281
copy DC1 script into remote script
edwardchalstrey1 Aug 22, 2023
df8435b
remove commented
edwardchalstrey1 Aug 22, 2023
3b9c1c1
move script
edwardchalstrey1 Aug 22, 2023
e24c15d
invoke remote script from file
edwardchalstrey1 Aug 22, 2023
65b2da9
rename
edwardchalstrey1 Aug 25, 2023
502678d
move to remote subdir
edwardchalstrey1 Aug 25, 2023
04a1912
force deletion
edwardchalstrey1 Aug 25, 2023
4e347e2
single foreach loop
edwardchalstrey1 Aug 25, 2023
8d548fd
add dryRun option
edwardchalstrey1 Aug 29, 2023
49ad7a0
dont sync aad dryrun
edwardchalstrey1 Aug 29, 2023
b76b52a
finish prev commit
edwardchalstrey1 Aug 29, 2023
b9258dd
add dryrun param to local script
edwardchalstrey1 Aug 29, 2023
e4288fe
param isnt string
edwardchalstrey1 Aug 29, 2023
fdf9314
use dryRun switch
edwardchalstrey1 Aug 29, 2023
6956753
use switch for dryrun local
edwardchalstrey1 Aug 29, 2023
663e357
pass remote script string param
edwardchalstrey1 Aug 29, 2023
f9e8a3c
change message for dry run
edwardchalstrey1 Aug 29, 2023
c596614
add documentation
edwardchalstrey1 Aug 29, 2023
d8e42fa
pass pester tests
edwardchalstrey1 Aug 29, 2023
3f04b76
Update docs/source/roles/system_manager/manage_users.md
edwardchalstrey1 Sep 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions deployment/administration/Delete_Unassigned_Users.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Extract list of users
$userOuPath = (Get-ADObject -Filter * | Where-Object { $_.Name -eq "Safe Haven Research Users" }).DistinguishedName
$users = Get-ADUser -Filter * -SearchBase "$userOuPath" -Properties *
foreach ($user in $users) {
$groupName = ($user | Select-Object -ExpandProperty MemberOf | ForEach-Object { (($_ -Split ",")[0] -Split "=")[1] }) -join "|"
$user | Add-Member -NotePropertyName GroupName -NotePropertyValue $groupName -Force
}

# Delete users not found in any group
foreach ($user in $users) {
if (!($user.GroupName)) {
$name = $user.SamAccountName
Remove-ADUser -Identity $name
}
}
edwardchalstrey1 marked this conversation as resolved.
Show resolved Hide resolved

# Force sync with AzureAD. It will still take around 5 minutes for changes to propagate
Write-Output "Synchronising locally Active Directory with Azure"
try {
Import-Module -Name "C:\Program Files\Microsoft Azure AD Sync\Bin\ADSync" -ErrorAction Stop
Start-ADSyncSyncCycle -PolicyType Delta
}
catch [System.IO.FileNotFoundException] {
Write-Output "Skipping as Azure AD Sync is not installed"
}
catch {
Write-Output "Unable to run Azure Active Directory synchronisation!"
}
edwardchalstrey1 marked this conversation as resolved.
Show resolved Hide resolved
25 changes: 25 additions & 0 deletions deployment/administration/SRE_Delete_Unassigned_Users.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
param(
[Parameter(Mandatory = $true, HelpMessage = "Enter SHM ID (e.g. use 'testa' for Turing Development Safe Haven A)")]
[string]$shmId
)

Import-Module Az.Accounts -ErrorAction Stop
Import-Module $PSScriptRoot/../common/AzureCompute -Force -ErrorAction Stop
Import-Module $PSScriptRoot/../common/Configuration -Force -ErrorAction Stop
Import-Module $PSScriptRoot/../common/Logging -Force -ErrorAction Stop

# Get config
# -------------------------------
$config = Get-ShmConfig -shmId $shmId
$originalContext = Get-AzContext

# Delete users not currently in a security group
# ----------------------------------------------
$null = Set-AzContext -SubscriptionId $config.subscriptionName -ErrorAction Stop
Add-LogMessage -Level Info "Deleting users not assigned to any security group: $($config.shm.id) from $($config.dc.vmName)..."

$script = "Delete_Unassigned_Users.ps1"
edwardchalstrey1 marked this conversation as resolved.
Show resolved Hide resolved

$result = Invoke-RemoteScript -Shell "PowerShell" -ScriptPath $script -VMName $config.dc.vmName -ResourceGroupName $config.dc.rg

$null = Set-AzContext -Context $originalContext -ErrorAction Stop
Loading