Created
May 23, 2019 08:17
-
-
Save eduardcloud/2c57c033da4247f2968e123a830ac749 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#Note that Write-Log Function is not present | |
function AzureLoginApi { | |
Param ( | |
[Parameter(Mandatory = $true)][string]$ClientID, | |
[Parameter(Mandatory = $true)][string]$ClientSecret, | |
[Parameter(Mandatory = $true)][string]$Tenant, | |
[Parameter(Mandatory = $false)][switch]$StorageLogin | |
) | |
try { | |
if ($StorageLogin) { $resource = "https://storage.azure.com/" } | |
else { $resource = "https://management.azure.com/" } | |
$body = @{grant_type = "client_credentials"; resource = "$resource"; client_id = "$clientID"; client_secret = "$clientSecret" } | |
$oauth = Invoke-RestMethod -Method Post -Uri "https://login.microsoftonline.com/$tenant/oauth2/token?api-version=2018-05-05" -Body $body | |
$global:authHeader = @{'Authorization' = "$($oauth.token_type) $($oauth.access_token)" } | |
if (!$global:authHeader) { | |
Write-Log -Mode ERROR -Message "Failed Logging to Azure tenant $tenant, 'authHeader' is empty. Exiting." | |
Exit 1 | |
} | |
} | |
catch { | |
Write-Log -Mode ERROR -Message "AzureLoginApi Failed Logging to Azure tenant $tenant. Exception: $_" | |
Exit 1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment