Skip to content

Instantly share code, notes, and snippets.

@eduardcloud
Created May 23, 2019 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eduardcloud/2c57c033da4247f2968e123a830ac749 to your computer and use it in GitHub Desktop.
Save eduardcloud/2c57c033da4247f2968e123a830ac749 to your computer and use it in GitHub Desktop.
#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