Skip to content

Instantly share code, notes, and snippets.

@JustinGrote
Last active February 23, 2021 00:06
Show Gist options
  • Save JustinGrote/63983c9a53b4085d4710befea17bc913 to your computer and use it in GitHub Desktop.
Save JustinGrote/63983c9a53b4085d4710befea17bc913 to your computer and use it in GitHub Desktop.
Installs the latest Azure Functions PS7 Dev Build

Azure Functions Core Tools Powershell

Note: Functions Core Tools v3.0.2358 now have PS7 included. As of 2358 it was rc2 only and not GA yet.

These instructions will install the latest dev build of the Azure Functions Powershell Worker, allowing you to test new features such as Durable Functions. Please see the Durable Powershell Functions experimental information for details

Installing the latest dev build

iwr https://git.io/InstallFuncPS7Dev | iex

Install and Make Powershell 7 the default

iex "& {$(iwr 'https://git.io/InstallFuncPS7Dev')} -AsDefault"

Invoke-Expression "& {$(iwr $uri)} $myargs"

Reinstall the latest dev build

iex "& {$(iwr 'https://git.io/InstallFuncPS7Dev')} -Force"

Azure Functions Core Tools Durable Functions Quickstart (Windows)

If you have never used Azure Functions Core Tools or Durable Functions before, this quickstart will get you all set up.

NOTE: If you are on a Mac/Linux, replace steps 1-2 with "use npm to install azure-functions core tools" and "have an azure storage account connection string" because Durable Functions requires Azure Storage Tables and Azurite will not support them. It is not possible to do "offline" Durable Functions development on anything but Windows right now :/

  1. Install SQL 2017 LocalDB from Cumulative Update 19 (Microsoft doesn't publish it outside the CU package and the standalone RTM release has a bug with Azure Storage Emulator)

    iwr https://git.io/InstallSQLLocalDB | iex
  2. Use scoop to install the Azure Storage Emulator and Azure Functions Core Tools

    iwr get.scoop.sh | iex
    scoop install https://git.io/azure-storage-emulator.json
    scoop install azure-functions-core-tools
    
  3. Install Powershell 7 Functions Dev Build

    iwr https://git.io/InstallFuncPS7Dev | iex
    
  4. Make a new function in the directory of your choice

    $MyFunctionAppLocation = '~\desktop\MyTestFunctionApp'
    mkdir $MyFunctionAppLocation
    set-location $MyFunctionAppLocation
  5. Download the sample DurableApp Powershell example and extract it to your function app

    start-process "https://minhaskamal.github.io/DownGit/#/home?url=https://github.com/Azure/azure-functions-powershell-worker/tree/dev/examples/durable/DurableApp"
  6. Set Powershell 7 as the default runtime for this project and install Durable Functions extensions

    $localsettings = gc -raw .\local.settings.json | convertfrom-json
    $localsettings.values | Add-Member -MemberType 'NoteProperty' -Name 'FUNCTIONS_WORKER_RUNTIME_VERSION' -Value '~7'
    $localsettings | ConvertTo-Json -Depth 9 | Out-File .\local.settings.json
    func extensions install --powershell
  7. Start Azure Storage Emulator and Azure Functions Powershell in your project directory (data will be saved to JSON files)

    & AzureStorageEmulator.exe start
    try {
        & func start
    } catch {throw $PSItem}
    finally { $azuriteJob | Stop-Job}
  8. Check that your app works, you should see a splash page

    start-process "http://localhost:7081"

  9. Try each of the durable function examples

{
"$schema": "https://raw.githubusercontent.com/lukesampson/scoop/master/schema.json",
"version": "5.10.0",
"url": "https://download.visualstudio.microsoft.com/download/pr/87453e3b-79ac-4d29-a70e-2a37d39f2b12/f0e339a0a189a0d315f75a72f0c9bd5e/microsoftazurestorageemulator.msi",
"extract_dir": "root/Microsoft SDKs/Azure/Storage Emulator",
"bin": [
"AzureStorageEmulator.exe",
"StartStorageEmulator.cmd"
],
"hash": "sha256:4ab98e9a50e9984fef497114fc5d9340aa2cef46f86ffa85fc0b5b4c4365cf53"
}
<#
.SYNOPSIS
Installs the beta version of the Powershell 7 Worker
#>
[CmdletBinding()]
param (
#Azure Functions Core Tools "func" command path. This will be autodetected by default
[String]$funcpath,
#Azure Functions Workers path. This will attempt to be autodetected by default.
[String]$funcWorkersPath,
#Force a reinstall or upgrade
[Switch]$Force,
#Set 7 as the default worker
[Switch]$AsDefault,
#Specify a branch. Defaults to 'Dev'
$Branch = 'dev'
)
$ErrorActionPreference = 'Stop'
Write-Host -fore cyan "===Azure Functions Powershell Dev Build Install Script==="
""
#Prerequisites
try {
if (-not $funcpath) {
$funcpath = 'func'
}
$funcpath = (Get-Command $funcpath).source
} catch [Management.Automation.CommandNotFoundException] {
throw "'func' not found as a command at $funcpath. Did you install the Azure Functions Core Tools first?
Hint: https://github.com/Azure/azure-functions-core-tools#installing
Hint2: If your func is not in your path, you can override it using the -funcpath parameter on this script
"
}
Write-Verbose "Azure Functions Core tools found at $funcpath"
$funcversion = [Version](func -v)
if ($funcversion -lt '3.0.0') {
throw "The Powershell Azure Functions Dev Build requires Azure Functions Core Tools 3.x or higher. We detected $funcversion at $funcpath. Please upgrade.
Hint:https://github.com/Azure/azure-functions-core-tools"
} else {
Write-Verbose "Powershell Functions 3.x or higher detected"
}
#Detect Azure Functions workers folder
#Special Scoop Shim Handling
if ($funcpath -match 'scoop') {
if ([string](scoop info azure-functions-core-tools 6>&1) -match 'Installed: (.+) ') {
$funcPath = Join-Path $matches[1].trim() 'func.exe'
Write-Verbose "Scoop install detected, changing function path to $funcpath"
}
}
if (-not $funcWorkersPath) {
$funcWorkersPath = Join-Path (Split-Path $funcpath) 'workers'
}
if (-not (Test-Path $funcWorkersPath)) {
throw "Azure Function Core Tools Worker Path $funcWorkersPath not found. You can specify it manually with the -FuncWorkersPath parameter"
}
try {
Register-PackageSource -Name AzureFunctionsCoreToolsNightlies -Location https://ci.appveyor.com/nuget/azure-functions-powershell-wor-0842fakagqy6/ -ProviderName Nuget
} catch {
if ($PSItem -notmatch 'exists') { throw $PSItem }
}
Write-Verbose 'Added Azure Functions Powershell Nuget Source'
Write-Host -nonewline -fore Cyan 'Searching for latest Microsoft.Azure.Functions.PowershellWorker.PS7 build...'
$ps7package = Find-Package 'Microsoft.Azure.Functions.PowershellWorker.PS7'
if ($ps7package) { Write-Host -fore green "OK. Version $($ps7package.Version)" }
#TODO: Better matching
#Get current package version if it exists
$PowershellWorkerPath = Join-Path $funcWorkersPath 'Powershell'
$PS7WorkerPath = Join-Path $powershellWorkerPath '7'
$PS7DepsPath = Join-Path $PS7WorkerPath 'Microsoft.Azure.Functions.PowerShellWorker.deps.json'
if (Test-Path $PS7DepsPath) {
$PS7Deps = (Get-Content -raw $PS7DepsPath | ConvertFrom-Json)
if ($PS7Deps.libraries.psobject.properties | Where-Object name -match 'Microsoft.Azure.Functions.PowershellWorker/([\d\.]+)') {
$PS7WorkerVersion = [Version]$matches[1]
} else {
throw 'Your powershell worker appears to have a corrupt dependency manifest. Try deleting the folder.'
}
if ($PS7WorkerVersion -gt [version]$ps7package.version) {
throw 'You have a newer version installed locally than is available on Appveyor. This should not happen and is probably a bug!'
}
if (-not $force -and $PS7WorkerVersion -eq [version]$ps7package.version) {
write-host -fore green "You have the latest version of the Powershell Worker Dev Build Installed ($PS7WorkerVersion)!"
write-host -fore green "If you want to reinstall anyways, specify -Force"
exit 0
}
} else {
Write-Host -fore Cyan "Powershell 7 Worker not found at $PS7WorkerPath. Installing..."
}
Write-Host ""
Write-Host -fore Cyan "===Installing Powershell 7 Worker $($PS7Package.Version)==="
Write-Host ""
$workerPackageUri = "https://ci.appveyor.com/nuget/azure-functions-powershell-wor-0842fakagqy6/api/v2/package/Microsoft.Azure.Functions.PowerShellWorker.PS7/$($PS7Package.Version)"
Write-Host -nonewline -fore Cyan "Fetching Worker Package from $workerPackageUri..."
$workerPackagePath = Join-Path ([io.path]::GetTempPath()) "Microsoft.Azure.Functions.PowerShellWorker.PS7-$($PS7Package.Version).zip"
$workerPackageExtractPath = Join-Path ([io.path]::GetTempPath()) "Microsoft.Azure.Functions.PowerShellWorker.PS7-$($PS7Package.Version)"
#BUG: Save-Package downloads a corrupt nupkg from appveyor for some reason, hence the direct fetch
([net.webclient]::new()).DownloadFile($workerPackageUri, $workerPackagePath)
Write-Host -fore Green " OK"
Expand-Archive -Path $workerPackagePath -DestinationPath $workerPackageExtractPath -Force
$ps7WorkerFiles = [io.path]::Combine($workerPackageExtractPath,'contentfiles','any','any','workers','powershell','7')
if (Test-Path (Join-Path $PowershellWorkerPath 'Microsoft.Azure.Functions.PowerShellWorker.dll')) {
$PS6WorkerPath = Join-Path $PowershellWorkerPath '6'
Write-Host -fore Yellow "PS6 Worker Detected in Default Directory, moving to $PS6WorkerPath"
try {
Get-ChildItem -Exclude $PS6WorkerPath,$PS7WorkerPath | Move-Item -Destination $PS6WorkerPath
} catch {
if ($PSItem -match 'being used by another process') {
throw 'The PS6 Worker is currently in use. Please make sure you have all instances of func.exe stopped and try again.'
} else {throw $PSItem}
}
}
Write-Host -fore Cyan "Removing Existing PS7 Worker at $PS7WorkerPath"
Remove-Item $PS7WorkerPath -Force -Recurse
Write-Host -fore Cyan "Installing New PS7 Worker to $PS7WorkerPath"
Move-Item -Path $ps7WorkerFiles -Destination $PS7WorkerPath -Force
Move-Item -Path $PS7WorkerPath/worker.config.json $PowershellWorkerPath -Force
$psWorkerConfig = Get-Content -raw "$PowershellWorkerPath/worker.config.json" | ConvertFrom-Json
if ($psworkerConfig.defaultruntimeversion -lt '7'){
if ($AsDefault) {
write-host -fore cyan 'Setting default worker version to 7'
$psWorkerConfig = Get-Content -raw "$PowershellWorkerPath/worker.config.json" | ConvertFrom-Json
$psWorkerConfig.description.defaultRuntimeVersion = 7
$psWorkerConfig | ConvertTo-Json -Depth 5 | Out-File "$PowershellWorkerPath/worker.config.json"
}
} else {
write-warning "This system still uses PS6 as the default. Set FUNCTIONS_WORKER_RUNTIME_VERSION to 7 in your project local.settings.json, or use the -AsDefault parameter to make it the default"
}
write-host -fore green "PS7 Worker Successfully Installed!"
This file has been truncated, but you can view the full file.
@JustinGrote
Copy link
Author

For the warning suggesting to install Azure Function Core tools 3 or higher (line 41), you should also include a link to the install instructions or even just the code (speaking from experience!)

npm install -g azure-functions-core-tools@3

Added in 45e52e2b40a720f2893cacb6cca43b89659d2a62

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment