Skip to content

Commit 28af69c

Browse files
pri-kiseKilian Seizinger
and
Kilian Seizinger
authored
Improvements for Remove-AadAppsForBc (#3500)
Use SecureString for AccessToken in Remove-AadAppsForBc if necessary: I added a check to use Convert the AccessToken to a SecureString if it's required for `Connect-MgGraph` . and Fix #3472 --------- Co-authored-by: Kilian Seizinger <[email protected]>
1 parent a71489d commit 28af69c

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

AzureAD/Remove-AadAppsForBc.ps1

+19-5
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ try {
3939
Install-Package Microsoft.Graph -Force -WarningAction Ignore | Out-Null
4040
}
4141

42+
# Check the AccessToken since Microsoft Graph V2 requires a SecureString
43+
$graphAccesTokenParameter = (Get-Command Connect-MgGraph).Parameters['AccessToken']
44+
45+
if ($graphAccesTokenParameter.ParameterType -eq [securestring]){
46+
$useSecureStringForAccessToken = $true
47+
}
48+
4249
# Connect to Microsoft.Graph
4350
if (!$useCurrentMicrosoftGraphConnection) {
4451
if ($bcAuthContext) {
@@ -47,16 +54,19 @@ try {
4754
if ($jwtToken.aud -ne 'https://graph.microsoft.com') {
4855
Write-Host -ForegroundColor Yellow "The accesstoken was provided for $($jwtToken.aud), should have been for https://graph.microsoft.com"
4956
}
50-
Connect-MgGraph -AccessToken $bcAuthContext.accessToken
57+
$accessToken = $bcAuthContext.accessToken
5158
}
52-
else {
53-
if ($accessToken) {
54-
Connect-MgGraph -accessToken $accessToken
59+
if ($accessToken) {
60+
if ($useSecureStringForAccessToken){
61+
Connect-MgGraph -AccessToken (ConvertTo-SecureString -String $accessToken -AsPlainText -Force) | Out-Null
5562
}
5663
else {
57-
Connect-MgGraph
64+
Connect-MgGraph -AccessToken $accessToken | Out-Null
5865
}
5966
}
67+
else {
68+
Connect-MgGraph -Scopes 'Application.ReadWrite.All' | Out-Null
69+
}
6070
}
6171
$account = Get-MgContext
6272

@@ -91,6 +101,10 @@ try {
91101
Write-Host "Remove AAD App for EMail Service"
92102
$EMailIdentifierUri = $appIdUri.Replace('://','://email.')
93103
Get-MgApplication -All | Where-Object { $_.IdentifierUris -contains $EMailIdentifierUri } | ForEach-Object { Remove-MgApplication -ApplicationId $_.Id }
104+
105+
# Remove "old" Other Services AD Application
106+
$OtherServicesIdentifierUri = $appIdUri.Replace('://','://other.')
107+
Get-MgApplication -All | Where-Object { $_.IdentifierUris -contains $OtherServicesIdentifierUri } | ForEach-Object { Remove-MgApplication -ApplicationId $_.Id }
94108

95109
}
96110
catch {

0 commit comments

Comments
 (0)