-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1508 from edwardchalstrey1/automate-acct-deletion
Add script to automate account deletion
- Loading branch information
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
param( | ||
[Parameter(Mandatory = $true, HelpMessage = "Enter SHM ID (e.g. use 'testa' for Turing Development Safe Haven A)")] | ||
[string]$shmId, | ||
[Parameter(Mandatory = $false, HelpMessage = "No-op mode which will not remove anything")] | ||
[Switch]$dryRun | ||
) | ||
|
||
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 | ||
$script = "remote/Delete_Unassigned_Users.ps1" | ||
|
||
# Passing a param to a remote script requires it to be a string | ||
if ($dryRun.IsPresent) { | ||
Add-LogMessage -Level Info "Listing users not assigned to any security group from $($config.dc.vmName)..." | ||
$params = @{dryRun = "yes" } | ||
} else { | ||
Add-LogMessage -Level Info "Deleting users not assigned to any security group from $($config.dc.vmName)..." | ||
$params = @{dryRun = "no" } | ||
} | ||
$result = Invoke-RemoteScript -Shell "PowerShell" -ScriptPath $script -VMName $config.dc.vmName -ResourceGroupName $config.dc.rg -Parameter $params | ||
|
||
$null = Set-AzContext -Context $originalContext -ErrorAction Stop |
33 changes: 33 additions & 0 deletions
33
deployment/administration/remote/Delete_Unassigned_Users.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
param( | ||
[Parameter(Mandatory = $true, HelpMessage = "yes/no determines whether users should actually be deleted")] | ||
[string]$dryRun | ||
) | ||
|
||
# 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 "|" | ||
if (!($groupName)) { | ||
$name = $user.SamAccountName | ||
if ($dryRun -eq "yes") { | ||
Write-Output "User $name would be deleted by this action" | ||
} else { | ||
Write-Output "Deleting $name" | ||
Remove-ADUser -Identity $name -Confirm:$false | ||
} | ||
} | ||
} | ||
|
||
# Force sync with AzureAD. It will still take around 5 minutes for changes to propagate | ||
if ($dryRun -eq "no") { | ||
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!" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters