Skip to content

Instantly share code, notes, and snippets.

View ehrnst's full-sized avatar

Martin Ehrnst ehrnst

View GitHub Profile
@ehrnst
ehrnst / context-examples.ps1
Created May 5, 2020 18:50
Multiple Azure users/context in PowerShell
# Connect to Azure specifying a tenant
# If you want to connect to multiple tenants, you can connect multiple times.
Connect-AzAccount -tenantId customer1.onmicrosoft.com
# adding a new PowerShell Azure context
# setting a friendly name to allow for easy switching.
Set-AzContext -name "Subscription 1 in tenant 1" -SubscriptionId "31ffbc99-4cbf-43b2-8789-ba8d73171e70" -tenantid customer1.onmicrosoft.com
Set-AzContext -name "Subscription 2 in tenant 1" -SubscriptionId "b5c85827-0afd-49a0-8923-8fe35cfa8dd0" -tenantid customer1.onmicrosoft.com
@ehrnst
ehrnst / azuredeploy.json
Created March 4, 2020 17:00
Deploy function app at subscription level
{
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
"contentVersion": "1.0.0.1",
"parameters": {
"resourceGroupName": {
"type": "string",
"metadata": {
"description": "Specify the name of the resource group"
}
},
@ehrnst
ehrnst / get-all-documents
Created February 25, 2020 10:03
Azure API management cosmosDB policy
<!--
IMPORTANT:
- Policy elements can appear only within the <inbound>, <outbound>, <backend> section elements.
- To apply a policy to the incoming request (before it is forwarded to the backend service), place a corresponding policy element within the <inbound> section element.
- To apply a policy to the outgoing response (before it is sent back to the caller), place a corresponding policy element within the <outbound> section element.
- To add a policy, place the cursor at the desired insertion point and select a policy from the sidebar.
- To remove a policy, delete the corresponding policy statement from the policy document.
- Position the <base> element within a section element to inherit all policies from the corresponding section element in the enclosing scope.
- Remove the <base> element to prevent inheriting policies from the corresponding section element in the enclosing scope.
- Policies are applied in the order of their appearance, from the top down.
@ehrnst
ehrnst / acknowledge-alerts.ps1
Last active February 24, 2020 19:08
Retrieving data from Azure Monitor REST api with powershell: https://adatum.no/?p=6096
# alert handeling
# updating alert status
# get alerts
$alerts = Invoke-RestMethod -Method Get -Uri "https://management.azure.com/subscriptions/$subscriptionId/providers/Microsoft.AlertsManagement/alerts?api-version=2018-05-05" -Headers $headers
# fore every alert I have. get it's ID and acknowledge it.
# pay attention to the method is now POST (one can debate if this should be a PUT)
foreach ($alert in $alerts.value) {
@ehrnst
ehrnst / azure-pipelines.yml
Created December 13, 2019 12:37
Yaml pipeline with powershell
trigger:
- master
variables:
# Agent VM image name
vmImageName: 'windows-2019'
# service connection (azure)
azureServiceConnection: '{{ azServiceConnection }}'
@ehrnst
ehrnst / auhtenticateandquery.ps1
Created March 28, 2019 20:40
Azure AD authentication against azure functions using a custom app.
# getting a token from login.microsoft.com
# scope here is my custom app ID which has a custom application role defined.
$tenantID = "tenant.onmicrosoft.com"
$myCustomAPPID = "customAppWithID/.default"
$ClientID = 'your client id'
$ClientKey = 'your client key'
$params = @{
scope = $myCustomAPPID;
grant_type = 'client_credentials';
client_id = $ClientId;
@ehrnst
ehrnst / azuredeploy.json
Created March 8, 2019 08:10
inline nesting test Azure template
{
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
"contentVersion": "1.0.0.1",
"parameters": {
"rgName": {
"type": "string",
"defaultValue": "ehrnst-demo-function-rg"
},
"rgLocation": {
"type": "string",
@ehrnst
ehrnst / azuredeploy.json
Created February 15, 2019 12:42
101-webapp-basic-windows/azuredeploy.json
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"webAppName": {
"type": "string",
"metadata": {
"description": "Base name of the resource such as web app name and app service plan "
},
"minLength": 2
@ehrnst
ehrnst / Azure-graph-partnerCenter-examples.ps1
Last active November 9, 2023 14:11
CSP Secure app model with Powershell
# Connect to partner center via refresh token
# Considering the refresh token is stored securely. We will have to get a new access token.
$clientId = {multi tenant app id}
$secret = {multi tnant app secret}
$partnerAccessTokenUri = "https://login.windows.net/$partnerTenant/oauth2/token"
$params = @{
resource = "https://api.partnercenter.microsoft.com";
grant_type = "refresh_token";
@ehrnst
ehrnst / event-grid-function-test.ps1
Last active August 3, 2018 15:22
azure function return event grid validation event
# POST method: $req
$requestBody = Get-Content $req -Raw | ConvertFrom-Json
## validate event grid as described in https://docs.microsoft.com/en-us/azure/event-grid/security-authentication
# check event type and return a Json object with the correct validation response
if ($requestBody.eventType -eq "Microsoft.EventGrid.SubscriptionValidationEvent") {
$code = $requestBody.data.validationCode
$content = @{ validationResponse = $code }
$message = convertto-json -compress -InputObject ([ordered]@{
body = $content