Skip to content

Instantly share code, notes, and snippets.

View craig-martin's full-sized avatar

Craig Martin craig-martin

View GitHub Profile
@craig-martin
craig-martin / Invoke-WebApiWithAadToken.ps1
Created February 2, 2016 20:50
Use PowerShell to Authenticate to AAD Then Call a Web API
### Load ADAL
Add-Type -Path "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Services\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
# Set AAD client ID for the client app
$clientId = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
$resourceAppIdURI = "https://TestWebApi01.azurewebsites.net"
$authority = "https://login.windows.net/MyAadDirectory.onmicrosoft.com"
# Create Authentication Context tied to Azure AD Tenant
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority
### Warm up by looking for commands
get-command *AzureRmKeyVault*
get-command *AzureKeyVault*
### Does the Vault exist already?
Get-AzureKeyVaultKey -VaultName cmartKeyVault01 -Name TestKey
### Does the ResourceGroupName exist yet?
Get-AzureRmResourceGroup | select ResourceGroupName
### Authenticate to AAD
Login-AzureRmAccount
### There are two commands for this, from two sources
Get-Command Get-AzureADApplication #module: AzureAD 1.1.23.0
Get-Command Get-AzureRmADApplication #module: AzureRM.Resources 1.0.3
Get-AzureRmADApplication -DisplayNameStartWith fooApp
<#
@craig-martin
craig-martin / New-SelfSignedCert.ps1
Created January 29, 2016 18:39
Create a Self-Signed Cert with PowerShell
#Create the cert
New-SelfSignedCertificate -DnsName 'foo.azurewebsites.net' -CertStoreLocation cert:\CurrentUser\My -KeyAlgorithm RSA -KeyLength 2048 -KeyExportPolicy Exportable #-KeyUsage DigitalSignature,nonRepudiation,keyEncipherment
#Look for the cert
dir Cert:\CurrentUser\My
#Export the cert to a PFX file
$mypwd = ConvertTo-SecureString -String "WhoFedTheDogCorn?" -Force –AsPlainText
dir Cert:\CurrentUser\My | Where Subject -EQ 'CN=foo.azurewebsites.net' | Export-PfxCertificate -ChainOption BuildChain -Password $mypwd -FilePath E:\Scratch\fooCert.pfx