Skip to content

Commit

Permalink
Added slides and demos from 2021 Nordic Virtual Summit
Browse files Browse the repository at this point in the history
  • Loading branch information
janegilring committed Feb 10, 2021
1 parent 51ffb1a commit 0afd424
Show file tree
Hide file tree
Showing 6 changed files with 409 additions and 0 deletions.
129 changes: 129 additions & 0 deletions 2021 Nordic Virtual Summit/Demos/0 - Create a function.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<#
Prerequisites
To run and debug functions locally, you will need to:
• Install PowerShell Core/7
• Install the .NET Core SDK 2.1+
• Install Azure Functions Core Tools version 2.4.299 or later (update as often as possible)
-Per Feb 2021 3.0.3233 is the latest
-Also available via NPM: https://www.npmjs.com/package/azure-functions-core-tools
To publish and run in Azure:
• Install Azure PowerShell OR install the Azure CLI version 2.x or later.
• You need an active Azure subscription.
#>


# An introduction to Azure Functions
Start-Process https://docs.microsoft.com/en-us/azure/azure-functions/functions-overview

# Azure Functions developers guide
Start-Process https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference

# Azure Functions PowerShell developer guide
Start-Process https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-powershell

cd c:\demos

mkdir PSFunctionDemo

cd PSFunctionDemo

code --add .

func init --worker-runtime powershell

func new -l powershell -t HttpTrigger -n MyHttpTrigger

dir -Recurse

<#
• .vscode - A folder that recommends VS Code extensions to the user
• MyHttpTrigger - The folder that contains the function
• .gitignore - A file used by Git to ignore certain local build/test artifacts from source control
• host.json - Contains global configuration options that affect all functions for a function app
• local.settings.json - Contains the configuration settings for your function app that can be published to “Application Settings” in your Azure function app environment
• profile.ps1 - A PowerShell script that will be executed on every cold start of your language worker
• function.json - Contains the configuration metadata for the Function and the definition of input and output bindings
• run.ps1 - This is the script that will be executed when a Function is triggered
• sample.dat - Contains the sample data that will be displayed in the Azure Portal for testing purposes
#>

# Note: Remove dependency on Az-module before start, and add " - from PowerShell $($PSVersionTable.PSVersion.ToString())"
func start

Invoke-RestMethod http://localhost:7071/api/MyHttpTrigger?Name=Jan

# Notice the PowerShell version is 6.2.x, but what if we want 7?

# Stop function (Ctrl + C), then add the following to local.settings.json: "FUNCTIONS_WORKER_RUNTIME_VERSION" : "~7",
func start

Invoke-RestMethod http://localhost:7071/api/MyHttpTrigger?Name=Jan

Connect-AzAccount

Set-AzContext -Subscription 380d994a-e9b5-4648-ab8b-815e2ef18a2b

Get-AzContext

Install-Module -Name Az.Functions

Get-Command -Module Az.Functions

New-AzResourceGroup -Name nvs-rg -Location norwayeast -Tag @{expireson = '2021-01-11';description="Nordic Virtual Summit - demos"}

$AppPlan = @{
Name = 'nvs-asp'
ResourceGroupName = 'nvs-rg'
Location = 'Norway East'
Sku = 'EP1' #Elastic Premium
WorkerType = 'Windows'
}

New-AzFunctionAppPlan @AppPlan

$StorageAccount = @{
Name = 'nvsfunction'
ResourceGroupName = 'nvs-rg'
Location = 'Norway East'
SkuName = 'Standard_LRS'
}

New-AzStorageAccount @StorageAccount

$ApplicationInsights = @{
Name = 'nvs'
ResourceGroupName = 'nvs-rg'
Location = 'Norway East'
Kind = 'other'
}

New-AzApplicationInsights @ApplicationInsights

$App = @{
Name = 'psfunctiondemo'
ResourceGroupName = 'nvs-rg'
#Location = 'Norway East' #Only used when creating Consumption plan
RunTime = 'PowerShell'
RunTimeVersion = '7.0'
FunctionsVersion = '3'
PlanName = 'nvs-asp'
StorageAccount = 'nvsfunction'
ApplicationInsightsName = 'nvs'
OSType = 'Windows'
}

New-AzFunctionApp @App

az login

az account set -s "380d994a-e9b5-4648-ab8b-815e2ef18a2b"

az account show

#func azure functionapp publish <name of function app that was created in Azure>
func azure functionapp publish psfunctiondemo --force

Invoke-RestMethod 'https://psfunctiondemo.azurewebsites.net/api/MyHttpTrigger?code=ZaYWNbYmgpoajX6uIrVBG26/wkEoBmkz/KxRy6oLjVaaVboPVYHAAw==&Name=Jan'
90 changes: 90 additions & 0 deletions 2021 Nordic Virtual Summit/Demos/1 - Hybrid Connections.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
Start-Process https://docs.microsoft.com/en-us/azure/azure-functions/functions-hybrid-powershell

# 1) Create a PowerShell function app

# F1 in VS Code, search for function and select "Azure Functions: Create Function"
# HttpTriggerHybrid1

# 2) Create a hybrid connection for the function app (Endpoint hostname must match certificate on target machine)

# 3) Download and install the hybrid connection

# 4) Create an app setting for the password of an administrator account

# 5) Configure an on-premises server for PowerShell remoting (HTTPS listener)

# For configuration of WinRM, see
# https://docs.microsoft.com/windows/win32/winrm/installation-and-configuration-for-windows-remote-management.


# Enable PowerShell remoting on on-prem server
Enable-PSRemoting -Force

# Create firewall rule for WinRM. The default HTTPS port is 5986.
New-NetFirewallRule -Name "WinRM HTTPS" `
-DisplayName "WinRM HTTPS" `
-Enabled True `
-Profile "Any" `
-Action "Allow" `
-Direction "Inbound" `
-LocalPort 5986 `
-Protocol "TCP"

# Create new self-signed-certificate to be used by WinRM.
$Thumbprint = (New-SelfSignedCertificate -DnsName $env:COMPUTERNAME -CertStoreLocation Cert:\LocalMachine\My).Thumbprint

# Create WinRM HTTPS listener.
$Cmd = "winrm create winrm/config/Listener?Address=*+Transport=HTTPS @{Hostname=""$env:COMPUTERNAME ""; CertificateThumbprint=""$Thumbprint""}"
cmd.exe /C $Cmd


# 6 Test the function

<# Insert skeleton
using namespace System.Net
# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)
# Write to the Azure Functions log stream.
Write-Host "PowerShell HTTP trigger function processed a request."
$Service = $Request.Query.Name
# Note that RBKAdminPassword is a function app setting, so I can access it as $env:ContosoUserPassword.
$UserName = "functionsdemo"
$securedPassword = ConvertTo-SecureString $Env:RBKAdminPassword -AsPlainText -Force
$Credential = [System.management.automation.pscredential]::new($UserName, $SecuredPassword)
# This is the name of the hybrid connection Endpoint.
$HybridEndpoint = "MGMT-JR-02"
$Script = {
Param(
[Parameter(Mandatory=$True)]
[String] $Service
)
Get-Service $using:Service
}
Write-Output "Running command via Invoke-Command"
$output = Invoke-Command -ComputerName $HybridEndpoint `
-Credential $Credential `
-Port 5986 `
-UseSSL `
-ScriptBlock $Script `
-SessionOption (New-PSSessionOption -SkipCACheck)
$status = [HttpStatusCode]::OK
$body = "Status for service $Service is $($output.Status)"
# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = $status
Body = $body
})
#>

# Deploy to Function App in VS Code Azure extension

Invoke-RestMethod 'https://psfunctiondemo.azurewebsites.net/api/HttpTriggerHybrid1?code=ARobJqEWGaPq1JPw9Vef1/CUMJYCYDu5fA40jPgycSTNzCSi4G6G5g==&Name=spooler'
68 changes: 68 additions & 0 deletions 2021 Nordic Virtual Summit/Demos/2 - VNet integration.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<#
The Azure Functions Premium plan (sometimes referred to as Elastic Premium plan) is a hosting option
for function apps. The Premium plan provides features like VNet connectivity, no cold start,
and premium hardware. Multiple function apps can be deployed to the same Premium plan,
and the plan allows you to configure compute instance size, base plan size, and maximum plan size.
Azure Functions deployed to a Premium plan takes advantage of new VNet integration for web apps.
#>

Start-Process https://docs.microsoft.com/en-gb/azure/azure-functions/functions-premium-plan

# 1) Enable VNet integration
Start-Process https://docs.microsoft.com/en-gb/azure/app-service/web-sites-integrate-with-vnet

# 2) Test connectivity from console (Function App->Development Tools->Console in the portal)

tcpping 172.28.0.5:5985

nameresolver.exe rbk.ad

tcpping hpv-jr-02.rbk.ad:5985

# 3) Create a PowerShell function app

# F1 in VS Code, search for function and select "Azure Functions: Create Function"
# HttpTriggerHybrid2


<# Insert skeleton
using namespace System.Net
# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)
# Write to the Azure Functions log stream.
Write-Host "PowerShell HTTP trigger function processed a request."
# Note that RBKAdminPassword is a function app setting, so I can access it as $env:ContosoUserPassword.
$UserName = "functionsdemo"
$securedPassword = ConvertTo-SecureString $Env:RBKAdminPassword -AsPlainText -Force
$Credential = [System.management.automation.pscredential]::new($UserName, $SecuredPassword)
$Output = Invoke-Command -ComputerName HPV-JR-02.rbk.ad `
-Credential $Credential `
-Port 5986 `
-UseSSL `
-ScriptBlock {Get-VM | Select-Object Name,Status} `
-SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck)
$Output
$status = [HttpStatusCode]::OK
$body = "Got $($output.Count) VMs from on-prem Hyper-V host HPV-JR-02 "
# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = $status
Body = $body
})
#>

# Deploy to Function App in VS Code Azure extension

Invoke-RestMethod 'https://psfunctiondemo.azurewebsites.net/api/HttpTriggerHybrid2?code=onavaIIKYC6cvKUlcgpNdwqBME71V7S/zSni2rLNSpeB4SmNXX6xfg=='

Loading

0 comments on commit 0afd424

Please sign in to comment.