当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 用于 HTTP 触发的 Azure Functions 的首选 PowerShell 模板

作者:精品下载站 日期:2024-12-14 06:21:17 浏览:13 分类:玩电脑

用于 HTTP 触发的 Azure Functions 的首选 PowerShell 模板


PowerShell 脚本剖析

  • 自定义脚本行为 - 输入
  • 完成这些工作 - 处理
  • 告诉你发生了什么——输出
Get-Thing -Name 'thing1' -Value 'value1'
PS> Get-Thing -Name 'thing1' -Value 'value1'
thingvalue

PS> Get-Thing -Name 'thing1' -Value 'value1'
[pscustomobject]@{
	Name = 'thing1'
	Value = 'value1'
}

“之前”的 PowerShell 方式

[CmdletBinding()]
param(
	[Parameter(Mandatory)]
	[string]$ServiceName
)


## Returns the service object output from Restart-Service
Restart-Service -Name $ServiceName
[CmdletBinding()]
param(
	[Parameter(Mandatory)]
	[string]$ComputerName,
	
	[Parameter(Mandatory)]
	[string]$ServiceName
)


## Returns the service object output from Restart-Service
Invoke-Command -ComputerName $ComputerName -ScriptBlock { Restart-Service -Name $using:ServiceName }
.restartmyservice.ps1 -ComputerName 'REMOTEPC' -ServiceName 'someservice'

无服务器和本地:完全不同的环境

  • 它将在您控制的硬件上运行
  • 该脚本将有权访问内网资源
  • 它将在某种上下文下执行,例如域用户、本地用户等。
  • 只要完成工作需要它就会运行

淘汰旧参数

[CmdletBinding()]
param(
	[Parameter(Mandatory)]
	$Request,
	
	[Parameter(Mandatory)]
	$TriggerMetadata
)

? 您甚至不需要强制指定参数,因为调用脚本时,这些参数始终是传递给它的两个参数。

调用 Azure Functions 脚本

Invoke-RestMethod -Uri 'https://myapp.azurewebsites.net/restartservice?code=.....'

? 您可以通过运行以下命令使用 PowerShell 获取任何 Azure 函数的基本 URL:

PS> $functionApp = Get-AzFunctionApp -ResourceGroupName $resourceGroupName -Name $functionAppName
PS> $functionApp.HostName | ForEach-Object { "https://$_/$functionName }




I want to see what the Request parameter holds when I call this function so I’ll add a Write-Host reference like I usually do in the script and deploy the app (with the script) to Azure.



[CmdletBinding()]
param(
	[Parameter(Mandatory)]
	$Request,
	
	[Parameter(Mandatory)]
	$TriggerMetadata
)

Write-Host $Request

[玩转系统] 用于 HTTP 触发的 Azure Functions 的首选 PowerShell 模板

让我们通过 HTTP 绑定获取一些输出

[玩转系统] 用于 HTTP 触发的 Azure Functions 的首选 PowerShell 模板

[CmdletBinding()]
param(
	[Parameter(Mandatory)]
	$Request,
	
	[Parameter(Mandatory)]
	$TriggerMetadata
)


Push-OutputBinding -Name Response -Value $Request

[玩转系统] 用于 HTTP 触发的 Azure Functions 的首选 PowerShell 模板

using namespace System.Net

[CmdletBinding()] param( [Parameter(Mandatory)] $Request, [Parameter(Mandatory)] $TriggerMetadata )

$response = @{ Body = $Request }

Push-OutputBinding -Name Response -Value ([HttpResponseContext]$response)

? 不要忘记 using namespace System.Net 行。这将加载 HTTPResponseContext 类型所需的 .NET 程序集。

[玩转系统] 用于 HTTP 触发的 Azure Functions 的首选 PowerShell 模板

添加“PowerShellEsque”参数

从 HTTP 查询参数创建变量

[CmdletBinding()]
param(
	[Parameter()]
	$Name,
	
	[Parameter()]
	$Value
)

Write-Host "The Name parameter is $Name"
Write-Host "The Value parameter is $Value"
$Request.Query.GetEnumerator() | ForEach-Object {
	New-Variable -Name $_.Key -Value $_.Value
}
using namespace System.Net

[CmdletBinding()]
param(
	[Parameter(Mandatory)]
	$Request,
	
	[Parameter(Mandatory)]
	$TriggerMetadata
)

$Request.Query.GetEnumerator() | ForEach-Object {
	New-Variable -Name $_.Key -Value $_.Value
}

$response = @{
	Body = "The name passed was [$Name] with value of [$Value]
}

Push-OutputBinding -Name Response -Value ([HttpResponseContext]$response)

using namespace System.Net

[CmdletBinding()]
param(
	[Parameter(Mandatory)]
	$Request,
	
	[Parameter(Mandatory)]
	$TriggerMetadata
)

$response = @{
	Body = $null
}

## Define all parameters (mandatory or optional)
$allowedQueryParams = @('Name','Value')

## Define all of the parameters that must have a value
$requiredParams = @('Name')

## If any HTTP query params passed are not recognized, stop
[array]$notRecognizedParams = Compare-Object $allowedQueryParams @($request.Query.Keys) | Select-Object -ExpandProperty InputObject
if ($notRecognizedParams.Count -gt 0) {
	throw "The parameter(s) [$($notRecognizedParams -join ',')] were not recognized!"
}

## Ensure all mandatory parameters were passed
[array]$missingMandatoryParams = $requiredParams | Where-Object { $_ -notin @($request.Query.Keys) }
if ($missingMandatoryParams.Count -gt 0) {
	throw "Missing mandatory parameter(s) [$($missingMandatoryParams -join ',')]!"
}

## If all parametere were recognized and all mandatory parameters were used, now create variables from them
$Request.Query.GetEnumerator() | ForEach-Object {
	New-Variable -Name $_.Key -Value $_.Value
}

$response = @{
	Body = "The name passed was [$Name] with value of [$Value]"
}

Push-OutputBinding -Name Response -Value ([HttpResponseContext]$response)

[玩转系统] 用于 HTTP 触发的 Azure Functions 的首选 PowerShell 模板

最后一步:创建描述性 HTTP 错误

  • 将所有代码包装在 try/catch 块内。
  • 在响应中定义自定义 HTTP 状态代码
  • 将异常消息定义为响应正文
  • finally块中返回HTTP响应,无论是否抛出异常都返回响应

using namespace System.Net

[CmdletBinding()]
param(
	[Parameter(Mandatory)]
	$Request,
	
	[Parameter(Mandatory)]
	$TriggerMetadata
)

$response = @{}

try {

	## Define all parameters (mandatory or optional)
	$allowedQueryParams = @('Name','Value')
	
	## Define all of the parameters that must have a value
	$requiredParams = @('Name')
	
	## If any HTTP query params passed are not recognized, stop
	[array]$notRecognizedParams = Compare-Object $allowedQueryParams @($request.Query.Keys) | Select-Object -ExpandProperty InputObject
	if ($notRecognizedParams.Count -gt 0) {
		throw "The parameter(s) [$($notRecognizedParams -join ',')] were not recognized!"
	}
	
	## Ensure all mandatory parameters were passed
	[array]$missingMandatoryParams = $requiredParams | Where-Object { $_ -notin @($request.Query.Keys) }
	if ($missingMandatoryParams.Count -gt 0) {
		throw "Missing mandatory parameter(s) [$($missingMandatoryParams -join ',')]!"
	}
	
	## If all parametere were recognized and all mandatory parameters were used, now create variables from them
	$Request.Query.GetEnumerator() | ForEach-Object {
		New-Variable -Name $_.Key -Value $_.Value
	}
	
	$response.Body = "The name passed was [$Name] with value of [$Value]"

} catch {
	$response.StatusCode = [HttpStatusCode]::InternalServerError
	$response.Body = ($_.Exception | Out-String)
} finally {
	Push-OutputBinding -Name Response -Value ([HttpResponseContext]$response)
}

[玩转系统] 用于 HTTP 触发的 Azure Functions 的首选 PowerShell 模板

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯