-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathAdd Secret to Azure App Reg.ps1
72 lines (50 loc) · 1.89 KB
/
Add Secret to Azure App Reg.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Reference: https://docs.microsoft.com/en-us/graph/api/application-addpassword?view=graph-rest-1.0&tabs=http
#Application Permission:
#- Application.ReadWrite.OwnedBy
#- Application.ReadWrite.All
#Graph API Details
$MSGRAPHAPI_clientID = 'yourClientID'
$MSGRAPHAPI_tenantId = 'yourTenantID'
$MSGRAPHAPI_Clientsecret = 'yourSecret'
$MSGRAPHAPI_BaseURL = "https://graph.microsoft.com/v1.0"
#Enter Azure App Details
$AzureAppName = "TestApp1"
$SecretDescription="Secret1"
$SecretDurationInMonth=24
#Auth MS Graph API and Get Header
$MSGRAPHAPI_tokenBody = @{
Grant_Type = "client_credentials"
Scope = "https://graph.microsoft.com/.default"
Client_Id = $MSGRAPHAPI_clientID
Client_Secret = $MSGRAPHAPI_Clientsecret
}
$MSGRAPHAPI_tokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$MSGRAPHAPI_tenantId/oauth2/v2.0/token" -Method POST -Body $MSGRAPHAPI_tokenBody
$MSGRAPHAPI_headers = @{
"Authorization" = "Bearer $($MSGRAPHAPI_tokenResponse.access_token)"
"Content-type" = "application/json"
}
#Get Appi from App Name
$GetIDfromName_Params = @{
Method = "GET"
Uri = "$MSGRAPHAPI_BaseURL/applications?`$filter=displayName eq '$AzureAppName'"
header = $MSGRAPHAPI_headers
}
$GetIDfromName_Result = Invoke-RestMethod @GetIDfromName_Params
#Add Secret to App
$AddSecretToAppReg_Body = @"
{
"passwordCredential": {
"displayName": "$SecretDescription",
"endDateTime": "$(Get-Date -format o (Get-Date).AddMonths($SecretDurationInMonth))"
}
}
"@
$AddSecretToAppReg_Params = @{
Method = "POST"
Uri = "$MSGRAPHAPI_BaseURL/applications/$($GetIDfromName_Result.value.id)/addPassword"
header = $MSGRAPHAPI_headers
Body = $AddSecretToAppReg_Body
}
$AddSecretToAppReg_Result = Invoke-RestMethod @AddSecretToAppReg_Params
#Secret
$AddSecretToAppReg_Result.secretText