Skip to content
This repository was archived by the owner on Dec 6, 2023. It is now read-only.

Commit 345494f

Browse files
update scripts to use Microsoft Graph PowerShell (#397)
* update scripts to use Microsoft Graph PowerShell * (MAINT) Refactor application management scripts This change refactors the application management sample scripts for: - Clarity, using more descriptive variable names and reducing duplicate code where possible. - Idiomatic practices, replacing aliases and using explicit parameters, replacing `Add-Member` calls with the creation of **PSCustomObject**. - Line length, keeping the script width readable and succinct. This change does not affect the functionality of the scripts, only their presentation. Future changes could include replacing the non-idiomatic read/write host operations with script parameters, which would also allow for validation and in-console help. --------- Co-authored-by: Michael Lombardi <mlombardi@microsoft.com>
1 parent 4d308f4 commit 345494f

5 files changed

+725
-370
lines changed
Lines changed: 125 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,144 @@
1-
#################################################################################
2-
#DISCLAIMER: This is not an official PowerShell Script. We designed it specifically for the situation you have encountered right now.
3-
#Please do not modify or change any preset parameters.
4-
#Please note that we will not be able to support the script if it is changed or altered in any way or used in a different situation for other means.
1+
<#################################################################################
2+
DISCLAIMER:
53
6-
#This code-sample is provided "AS IT IS" without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.
7-
#This sample is not supported under any Microsoft standard support program or service..
8-
#Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose.
9-
#The entire risk arising out of the use or performance of the sample and documentation remains with you.
10-
#In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the script be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample or documentation, even if Microsoft has been advised of the possibility of such damages.
11-
#################################################################################
4+
This is not an official PowerShell Script. We designed it specifically for the situation you have
5+
encountered right now.
126
13-
Connect-AzureAD
7+
Please do not modify or change any preset parameters.
8+
9+
Please note that we will not be able to support the script if it's changed or altered in any way
10+
or used in a different situation for other means.
11+
12+
This code-sample is provided "AS IS" without warranty of any kind, either expressed or implied,
13+
including but not limited to the implied warranties of merchantability and/or fitness for a
14+
particular purpose.
15+
16+
This sample is not supported under any Microsoft standard support program or service.
17+
18+
Microsoft further disclaims all implied warranties including, without limitation, any implied
19+
warranties of merchantability or of fitness for a particular purpose.
20+
21+
The entire risk arising out of the use or performance of the sample and documentation remains with
22+
you.
23+
24+
In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or
25+
delivery of the script be liable for any damages whatsoever (including, without limitation, damages
26+
for loss of business profits, business interruption, loss of business information, or other
27+
pecuniary loss) arising out of the use of or inability to use the sample or documentation, even if
28+
Microsoft has been advised of the possibility of such damages.
29+
#################################################################################>
30+
31+
Connect-MgGraph -Scopes 'Application.ReadWrite.All'
32+
33+
$Messages = @{
34+
DurationNotice = @{
35+
Info = @(
36+
'The operation is running and will take longer the more applications the tenant has...'
37+
'Please wait...'
38+
) -join ' '
39+
}
40+
Export = @{
41+
Info = 'Where should the CSV file export to?'
42+
Prompt = 'Enter the full path in the format of <C:\Users\<USER>\Desktop\Users.csv>'
43+
}
44+
}
45+
46+
Write-Host $Messages.DurationNotice.Info -ForegroundColor yellow
47+
48+
$Applications = Get-MgApplication -All
1449

15-
$Applications = Get-AzureADApplication -all $true
1650
$Logs = @()
1751

18-
foreach ($app in $Applications) {
19-
$AppName = $app.DisplayName
20-
$AppID = $app.objectid
21-
$ApplID = $app.AppId
22-
$AppCreds = Get-AzureADApplication -ObjectId $AppID | select PasswordCredentials, KeyCredentials
23-
$secret = $AppCreds.PasswordCredentials
24-
$cert = $AppCreds.KeyCredentials
52+
foreach ($App in $Applications) {
53+
$AppName = $App.DisplayName
54+
$AppID = $App.Id
55+
$ApplID = $App.AppId
56+
57+
$AppCreds = Get-MgApplication -ApplicationId $AppID |
58+
Select-Object PasswordCredentials, KeyCredentials
59+
60+
$Secrets = $AppCreds.PasswordCredentials
61+
$Certs = $AppCreds.KeyCredentials
2562

2663
############################################
27-
$Log = New-Object System.Object
28-
29-
$Log | Add-Member -MemberType NoteProperty -Name "ApplicationName" -Value $AppName
30-
$Log | Add-Member -MemberType NoteProperty -Name "ApplicationID" -Value $ApplID
31-
$Log | Add-Member -MemberType NoteProperty -Name "Secret Start Date" -Value $Null
32-
$Log | Add-Member -MemberType NoteProperty -Name "Secret End Date" -value $Null
33-
$Log | Add-Member -MemberType NoteProperty -Name "Certificate Start Date" -Value $Null
34-
$Log | Add-Member -MemberType NoteProperty -Name "Certificate End Date" -value $Null
35-
$Log | Add-Member -MemberType NoteProperty -Name "Owner" -Value $Null
36-
$Log | Add-Member -MemberType NoteProperty -Name "Owner_ObjectID" -value $Null
37-
38-
$Logs += $Log
64+
$Logs += [PSCustomObject]@{
65+
'ApplicationName' = $AppName
66+
'ApplicationID' = $ApplID
67+
'Secret Name' = $Null
68+
'Secret Start Date' = $Null
69+
'Secret End Date' = $Null
70+
'Certificate Name' = $Null
71+
'Certificate Start Date' = $Null
72+
'Certificate End Date' = $Null
73+
'Owner' = $Null
74+
'Owner_ObjectID' = $Null
75+
}
3976
############################################
40-
foreach ($s in $secret) {
41-
$StartDate = $s.StartDate
42-
$EndDate = $s.EndDate
43-
44-
#$operation = $EndDate - $now
45-
#$ODays = $operation.Days
46-
47-
$Owner = Get-AzureADApplicationOwner -ObjectId $app.ObjectId
48-
$Username = $Owner.UserPrincipalName -join ";"
49-
$OwnerID = $Owner.ObjectID -join ";"
50-
if ($owner.UserPrincipalName -eq $Null) {
51-
$Username = $Owner.DisplayName + " **<This is an Application>**"
77+
foreach ($Secret in $Secrets) {
78+
$StartDate = $Secret.StartDateTime
79+
$EndDate = $Secret.EndDateTime
80+
$SecretName = $Secret.DisplayName
81+
82+
$Owner = Get-MgApplicationOwner -ApplicationId $App.Id
83+
$Username = $Owner.AdditionalProperties.userPrincipalName -join ';'
84+
$OwnerID = $Owner.Id -join ';'
85+
86+
if ($null -eq $Owner.AdditionalProperties.userPrincipalName) {
87+
$Username = @(
88+
$Owner.AdditionalProperties.displayName
89+
'**<This is an Application>**'
90+
) -join ' '
5291
}
53-
if ($Owner.DisplayName -eq $null) {
54-
$Username = "<<No Owner>>"
92+
if ($null -eq $Owner.AdditionalProperties.displayName) {
93+
$Username = '<<No Owner>>'
5594
}
5695

57-
$Log = New-Object System.Object
96+
$Logs += [PSCustomObject]@{
97+
'ApplicationName' = $AppName
98+
'ApplicationID' = $ApplID
99+
'Secret Name' = $SecretName
100+
'Secret Start Date' = $StartDate
101+
'Secret End Date' = $EndDate
102+
'Certificate Name' = $Null
103+
'Certificate Start Date' = $Null
104+
'Certificate End Date' = $Null
105+
'Owner' = $Username
106+
'Owner_ObjectID' = $OwnerID
107+
}
108+
}
58109

59-
$Log | Add-Member -MemberType NoteProperty -Name "ApplicationName" -Value $AppName
60-
$Log | Add-Member -MemberType NoteProperty -Name "ApplicationID" -Value $ApplID
61-
$Log | Add-Member -MemberType NoteProperty -Name "Secret Start Date" -Value $StartDate
62-
$Log | Add-Member -MemberType NoteProperty -Name "Secret End Date" -value $EndDate
63-
$Log | Add-Member -MemberType NoteProperty -Name "Certificate Start Date" -Value $Null
64-
$Log | Add-Member -MemberType NoteProperty -Name "Certificate End Date" -value $Null
65-
$Log | Add-Member -MemberType NoteProperty -Name "Owner" -Value $Username
66-
$Log | Add-Member -MemberType NoteProperty -Name "Owner_ObjectID" -value $OwnerID
110+
foreach ($Cert in $Certs) {
111+
$StartDate = $Cert.StartDateTime
112+
$EndDate = $Cert.EndDateTime
113+
$CertName = $Cert.DisplayName
67114

68-
$Logs += $Log
69-
}
70-
71-
foreach ($c in $cert) {
72-
$CStartDate = $c.StartDate
73-
$CEndDate = $c.EndDate
74-
#$COperation = $CEndDate - $now
75-
#$CODays = $COperation.Days
76-
77-
$Owner = Get-AzureADApplicationOwner -ObjectId $app.ObjectId
78-
$Username = $Owner.UserPrincipalName -join ";"
79-
$OwnerID = $Owner.ObjectID -join ";"
80-
if ($owner.UserPrincipalName -eq $Null) {
81-
$Username = $Owner.DisplayName + " **<This is an Application>**"
115+
$Owner = Get-MgApplicationOwner -ApplicationId $App.Id
116+
$Username = $Owner.AdditionalProperties.userPrincipalName -join ';'
117+
$OwnerID = $Owner.Id -join ';'
118+
119+
if ($null -eq $Owner.AdditionalProperties.userPrincipalName) {
120+
$Username = @(
121+
$Owner.AdditionalProperties.displayName
122+
'**<This is an Application>**'
123+
) -join ' '
82124
}
83-
if ($Owner.DisplayName -eq $null) {
84-
$Username = "<<No Owner>>"
125+
if ($null -eq $Owner.AdditionalProperties.displayName) {
126+
$Username = '<<No Owner>>'
85127
}
86128

87-
$Log = New-Object System.Object
88-
89-
$Log | Add-Member -MemberType NoteProperty -Name "ApplicationName" -Value $AppName
90-
$Log | Add-Member -MemberType NoteProperty -Name "ApplicationID" -Value $ApplID
91-
$Log | Add-Member -MemberType NoteProperty -Name "Certificate Start Date" -Value $CStartDate
92-
$Log | Add-Member -MemberType NoteProperty -Name "Certificate End Date" -value $CEndDate
93-
$Log | Add-Member -MemberType NoteProperty -Name "Owner" -Value $Username
94-
$Log | Add-Member -MemberType NoteProperty -Name "Owner_ObjectID" -value $OwnerID
95-
96-
$Logs += $Log
129+
$Logs += [PSCustomObject]@{
130+
'ApplicationName' = $AppName
131+
'ApplicationID' = $ApplID
132+
'Secret Name' = $Null
133+
'Certificate Name' = $CertName
134+
'Certificate Start Date' = $StartDate
135+
'Certificate End Date' = $EndDate
136+
'Owner' = $Username
137+
'Owner_ObjectID' = $OwnerID
138+
}
97139
}
98140
}
99141

100-
Write-host "Add the Path you'd like us to export the CSV file to, in the format of <C:\Users\<USER>\Desktop\Users.csv>" -ForegroundColor Green
101-
$Path = Read-Host
102-
$Logs | Export-CSV $Path -NoTypeInformation -Encoding UTF8
142+
Write-Host $Messages.Export.Info -ForegroundColor Green
143+
$Path = Read-Host -Prompt $Messages.Export.Prompt
144+
$Logs | Export-Csv $Path -NoTypeInformation -Encoding UTF8

0 commit comments

Comments
 (0)