-
Notifications
You must be signed in to change notification settings - Fork 5k
/
profile.ps1
71 lines (64 loc) · 2.7 KB
/
profile.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Azure Functions profile.ps1
#
# This profile.ps1 will get executed every "cold start" of your Function App.
# "cold start" occurs when:
#
# * A Function App starts up for the very first time
# * A Function App starts up after being de-allocated due to inactivity
#
# You can define helper functions, run commands, or specify environment variables
# NOTE: any variables defined that are not environment variables will get reset after the first execution
# Authenticate with Azure PowerShell using MSI.
# Remove this if you are not planning on using MSI or Azure PowerShell.
# Import modules
@('CIPPCore', 'CippExtensions', 'Az.KeyVault', 'Az.Accounts') | ForEach-Object {
try {
$Module = $_
Import-Module -Name $_ -ErrorAction Stop
} catch {
Write-LogMessage -message "Failed to import module - $Module" -LogData (Get-CippException -Exception $_) -Sev 'debug'
$_.Exception.Message
}
}
if ($env:ExternalDurablePowerShellSDK -eq $true) {
try {
Import-Module AzureFunctions.PowerShell.Durable.SDK -ErrorAction Stop
Write-Information 'External Durable SDK enabled'
} catch {
Write-LogMessage -message 'Failed to import module - AzureFunctions.PowerShell.Durable.SDK' -LogData (Get-CippException -Exception $_) -Sev 'debug'
$_.Exception.Message
}
}
try {
Disable-AzContextAutosave -Scope Process | Out-Null
} catch {}
try {
if (!$ENV:SetFromProfile) {
Write-Information "We're reloading from KV"
$Auth = Get-CIPPAuthentication
}
} catch {
Write-LogMessage -message 'Could not retrieve keys from Keyvault' -LogData (Get-CippException -Exception $_) -Sev 'debug'
}
Set-Location -Path $PSScriptRoot
$CurrentVersion = (Get-Content .\version_latest.txt).trim()
$Table = Get-CippTable -tablename 'Version'
Write-Information "Function: $($env:WEBSITE_SITE_NAME) Version: $CurrentVersion"
$LastStartup = Get-CIPPAzDataTableEntity @Table -Filter "PartitionKey eq 'Version' and RowKey eq '$($env:WEBSITE_SITE_NAME)'"
if (!$LastStartup -or $CurrentVersion -ne $LastStartup.Version) {
Write-Information "Version has changed from $($LastStartup.Version ?? 'None') to $CurrentVersion"
Clear-CippDurables
if ($LastStartup) {
$LastStartup.Version = $CurrentVersion
} else {
$LastStartup = [PSCustomObject]@{
PartitionKey = 'Version'
RowKey = $env:WEBSITE_SITE_NAME
Version = $CurrentVersion
}
}
Update-AzDataTableEntity @Table -Entity $LastStartup -Force
}
# Uncomment the next line to enable legacy AzureRm alias in Azure PowerShell.
# Enable-AzureRmAlias
# You can also define functions or aliases that can be referenced in any of your PowerShell functions.