Skip to content

Instantly share code, notes, and snippets.

View sjwaight's full-sized avatar
😎
Happy Days

Simon Waight sjwaight

😎
Happy Days
View GitHub Profile
@sjwaight
sjwaight / sample-runbook-service-principal-credentials.ps1
Last active February 19, 2016 03:32
Shows how we can use a Service Principal identity in an Azure Automation Runbook.
param (
[Parameter(Mandatory=$false)]
[String]$AzureCredentialAssetName = "VMPowerServicePrincipal",
[Parameter(Mandatory=$false)]
[String]$AzureSubscriptionIDAssetName = "VMShutdownTargetSubscription",
[Parameter(Mandatory=$false)]
[String]$AzureTenantIDAssetName = "VMShutdownTargetTenant"
)
$myEmailOrUpn = 'some.user@some.doman'
# can use your mailbox login
$cred = Get-Credential
$exoSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $cred -Authentication "Basic" -AllowRedirection
# Loads session and EXO Cmdlets
Import-PSSession $exoSession
Configuration Main
{
Import-DscResource –ModuleName 'PSDesiredStateConfiguration'
Node ('localhost')
{
Script DeployWindowsService
{
GetScript = {
@{
@sjwaight
sjwaight / Create-SelfSignedCert.ps1
Last active November 27, 2016 22:57
Shows how we can generate a self-signed certificate for use with an Azure AD Service Principal
# Requires PowerShell to be run as Admin-level user.
New-SelfSignedCertificate -CertStoreLocation cert:\localmachine\my -Provider "Microsoft Enhanced RSA and AES Cryptographic Provider" `
-Subject "cn=mydemokvcert" -KeyDescription "Used to access Key Vault" `
-NotBefore (Get-Date).AddDays(-1) -NotAfter (Get-Date).AddYears(2)
# PSParentPath: Microsoft.PowerShell.Security\Certificate::LocalMachine\my
#
#Thumbprint Subject
#---------- -------
##
# Create new Service Principal with Cert configured
##
Login-AzureRmAccount -SubscriptionId XXXXXXXX-XXXX-XXXX-XXXX-86b9ebca2d13
# $credValue comes from the previous script and contains the X509 cert we wish to use.
# $validFrom comes from the previous script and is the validity start date for the cert.
# $validTo comes from the previous script and is the validity end data for the cert.
{
"frameworks": {
"net46": {
"dependencies": {
"Microsoft.IdentityModel.Clients.ActiveDirectory": "3.13.1",
"Microsoft.IdentityModel.Logging": "1.0.0",
"Microsoft.Azure.Common": "2.1.0",
"Microsoft.Azure.KeyVault": "1.0.0",
}
}
#r "System.Runtime"
#r "System.Threading.Tasks"
using System;
using System.Threading.Tasks;
using System.Web.Configuration;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Azure.KeyVault;
using System.Security.Cryptography.X509Certificates;
@sjwaight
sjwaight / run.csx
Last active November 17, 2016 00:49
#load "keyvaultclient.csx"
public static void Run(TraceWriter log)
{
var secretStringClearText = GetKeyVaultSecret("remotepassword");
log.Info(secretStringClearText);
}
@sjwaight
sjwaight / project.json
Created November 27, 2016 09:17
Azure Functions project.json that loads the SendGrid nuget package.
{
"frameworks": {
"net46": {
"dependencies": {
"Sendgrid": "8.0.5"
}
}
}
}
@sjwaight
sjwaight / function.json
Created November 27, 2016 09:20
Azure Functions function.json with a SendGrid output binding.
{
"bindings": [
{
"type": "sendGrid",
"name": "message",
"direction": "out",
"from": "your.sender@your.domain",
"subject": "Functions r0ck5!"
}
],