Public/Remove-Cloudockit.ps1
# This script needs to be run by an admin account in your Azure tenant. # This script will create an Microsoft Entra ID app in your organisation with permission # to access resources in yours or customers' tenants. Function Test-CommandExists { Param ($command) $oldPreference = $ErrorActionPreference $ErrorActionPreference = 'stop' try {if(Get-Command $command){RETURN $true}} Catch {Write-Host "$command does not exist"; RETURN $false} Finally {$ErrorActionPreference=$oldPreference} } #end function test-CommandExistsnd function test-CommandExists function Show-Details { if ($appName) { Write-Host "===============$appName =====================" } if ($tenant_id) { Write-Host "Tenant ID:" $tenant_id} if ($tenantName) { Write-Host "Tenant Name:" $tenantName} if ($servicePrinId) { Write-Host "ServicePrincipal ID:" $servicePrinId} } function Remove-Cloudockit { # Set ErrorActionPreference to "Stop" $ErrorActionPreference = "Stop" Try{ if((Test-Path AppRegistrationInfo.json -PathType Leaf)) { $AppRegJSON = Get-Content -Raw -Path AppRegistrationInfo.json | ConvertFrom-Json } try { $sessioninfo = Get-CloudDrive if ($sessioninfo) { $tenant_id = (Get-AzSubscription)[0].TenantID If (-Not(Test-CommandExists Connect-AZaccount)) { Install-Module Az } #Import-Module Az Write-Host "Connect-AZaccount..." Connect-AzAccount Write-Host "Connected to AZaccount" } } catch { [regex] $match = '^[{]?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}[}]?$' $i = 0 do { if ($i -gt 0) { Write-Host "The TenantID is in wrong Format, it shoud be a GUID." } $i++; $tenant_id = ($v = Read-Host "Please enter your Microsoft Entra ID TenantID, Default $($AppRegJSON.TenantId) ") ? $v : $AppRegJSON.TenantId }until ($tenant_id -match $match) If (-Not(Test-CommandExists Connect-AZaccount)) { Install-Module Az Import-Module Az } Connect-AzAccount -UseDeviceAuthentication Write-Host "Connected to AZaccount" } if((Test-Path AppRegistrationInfo.json -PathType Leaf)) { $AppRegJSON = Get-Content -Raw -Path AppRegistrationInfo.json | ConvertFrom-Json } $AppRegJSON.AppName $question = "Please enter the wanted name for the App Registration, Default $($AppRegJSON.AppName)" $appName = ($v = Read-Host $question) ? $v : $AppRegJSON.AppName $removeExistingAppWithSameName = $true if(($myApp = Get-AzADApplication -Filter "DisplayName eq '$($appName)'" -ErrorAction SilentlyContinue)) { $existingapp = $null $SearchString = "DisplayName:" + $appName $existingapp = Get-AzADApplication -Search $SearchString -ConsistencyLevel eventual $existingapp.id if ($existingapp -and $removeExistingAppWithSameName) { #wait for Azure to have AppRegistrationReady $servicePrinId = (Get-AzADServicePrincipal -DisplayName $appName).id $waiter = "" while($null -eq $servicePrinId) { $waiter = $waiter + "." Start-Sleep 2 $servicePrinId = (Get-AzADServicePrincipal -DisplayName $appName).id write-host "Waiting for AzADServicePrincipal ..." $waiter } Remove-Variable waiter $ErrorActionPreference = "Continue" $question = "Remove Reader Permission to this Subscription? [y/n]" $roleName = "Reader" $subscriptions = Get-AzSubscription $subscriptions | ForEach-Object { Write-Host Write-Host -f Green "Subscription: $($_.name)}" $scope = "/subscriptions/$($_.id)" $roleExist = Get-AzRoleAssignment -ObjectId $servicePrinId -RoleDefinitionName $roleName -Scope $scope if ($roleExist) { $confirmation = Read-Host $question while($confirmation -ne "y") { if ($confirmation -eq 'n') {return} $confirmation = Read-Host $question } Write-Host $scope Remove-AzRoleAssignment -ObjectId $servicePrinId -RoleDefinitionName $roleName -Scope $scope } } $ErrorActionPreference = "stop" Write-Host -f Green "Removing the App: $($existingApp.DisplayName)}" Remove-AzADApplication -ObjectId $existingApp.id -Confirm } } else { Write-Host Write-Host -f Yellow Azure AD Application $appName already exists. } Write-Host Write-Host -f Green "Finished" Write-Host -f Green "Disconnecting..." Disconnect-AZaccount | Out-null Write-Host Write-Host -f Green "Disconnected AZaccount" } Catch { Write-Host -foregroundcolor Red "An error occurred: $_" Write-Host "Disconnecting..." Disconnect-AZaccount | Out-null Write-Host Write-Host -f Green "Disconnected AZaccount" } # Reset ErrorActionPreference to default "Continue" $ErrorActionPreference = "Continue" } # This script needs to be run by an admin account in your Azure tenant. # This script will create an Microsoft Entra ID app in your organisation with permission # to access resources in yours or customers' tenants. |