-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.ps1
More file actions
109 lines (89 loc) · 3.37 KB
/
Copy pathprofile.ps1
File metadata and controls
109 lines (89 loc) · 3.37 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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.
if ($env:MSI_SECRET) {
Disable-AzContextAutosave -Scope Process | Out-Null
Connect-AzAccount -Identity
}
# Microsoft To Do Exporter Helper Functions
function Get-AccessToken {
param($Code)
Add-Type -AssemblyName System.Web
$bodyString = "grant_type=authorization_code&client_id=$([System.Web.HttpUtility]::UrlEncode($env:CLIENT_ID))&client_secret=$([System.Web.HttpUtility]::UrlEncode($env:CLIENT_SECRET))&code=$([System.Web.HttpUtility]::UrlEncode($Code))&redirect_uri=$([System.Web.HttpUtility]::UrlEncode($env:REDIRECT_URI))&scope=$([System.Web.HttpUtility]::UrlEncode('Tasks.Read'))"
Write-Host "Body string: $bodyString"
$token = Invoke-RestMethod `
-Uri "https://login.microsoftonline.com/common/oauth2/v2.0/token" `
-Method POST `
-Body $bodyString `
-ContentType "application/x-www-form-urlencoded"
return $token.access_token
}
function Invoke-GraphGet {
param([string]$Token, [string]$Url)
$headers = @{ Authorization = "Bearer $Token" }
$results = @()
$nextUrl = $Url
do {
$page = Invoke-RestMethod -Method Get -Uri $nextUrl -Headers $headers
if ($null -ne $page.value) {
$results += $page.value
}
if ($page.PSObject.Properties.Name.Contains('@odata.nextLink')) {
$nextUrl = $page.'@odata.nextLink'
}
else {
$nextUrl = $null
}
} while ($nextUrl)
Write-Output $results -NoEnumerate
}
function Get-HtmlTemplate {
param($TemplateName, $Replacements = @{})
$templatePath = Join-Path $PSScriptRoot "templates\$TemplateName.html"
if (-not (Test-Path $templatePath)) {
throw "Template not found: $templatePath"
}
$html = Get-Content $templatePath -Raw
foreach ($key in $Replacements.Keys) {
$placeholder = "{{$key}}"
$value = $Replacements[$key]
$html = $html -replace [regex]::Escape($placeholder), $value
}
return $html
}
function Render-ErrorResponse {
param(
[System.Net.HttpStatusCode]$StatusCode = [System.Net.HttpStatusCode]::InternalServerError,
$Title = "Error",
$Message = "An unexpected error occurred.",
$Details = ""
)
$detailsClass = ''
if (-not $Details) { $detailsClass = 'hidden' }
$replacements = @{
"TITLE" = $Title
"MESSAGE" = $Message
"DETAILS" = $Details
"DETAILS_CLASS" = $detailsClass
}
try {
$html = Get-HtmlTemplate -TemplateName "Error" -Replacements $replacements
}
catch {
$html = "<h1>$Title</h1><p>$Message</p><pre>$Details</pre>"
}
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = $StatusCode
ContentType = "text/html; charset=utf-8"
Body = $html
})
}