powershell - 验证多个 Azure Powershell 函数

标签 powershell azure azure-functions

我使用 Azure Functions 构建了一些 Powershell 函数,它运行得非常出色。

既然我已经证明了这个概念,我非常想重构我现有的功能。

首先,我想将我的函数中所需的身份验证转移到某种共享函数或其他函数中。

这是我的示例函数,它返回我的资源组中所有 Web 应用程序的列表。

# Authenticate with subscription
$subscriptionId = "<SubscriptionId>"
$resourceGroupName = "<ResourceGroupName>";
$tenantId = "<TenantId>"
$applicationId = "<ApplicationId>"
$password = "<Password>"
$userPassword = ConvertTo-SecureString -String $password -AsPlainText -Force
$userCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $applicationId, $userPassword
Add-AzureRmAccount -TenantId $tenantid -ServicePrincipal -SubscriptionId $subscriptionId -Credential $userCredential
Get-AzureRmSubscription –SubscriptionId $subscriptionId | Select-AzureRmSubscription

# Get all web apps
$Websites = Get-AzureRmWebApp -ResourceGroupName $resourceGroupName
$Websites = $Websites | select name | ConvertTo-Json -Compress

# Write output
Out-File -Encoding Ascii -FilePath $res -inputObject $Websites

我非常想将所有内容从第 1 行移动到第 10 行其他地方。是否可以?如果是,有人可以在这里指出我正确的方向吗?

<小时/>

更新

感谢 Walter 和 Pragna,我将这两种方法结合起来。

run.ps1

# Authenticate with subscription
Import-Module 'D:\home\site\wwwroot\bin\Authentication.ps1'

# Get all web apps
$Websites = Get-AzureRmWebApp -ResourceGroupName $env:ResourceGroupName
$Websites = $Websites | select name | ConvertTo-Json -Compress

# Write output
Out-File -Encoding Ascii -FilePath $res -inputObject $Websites

身份验证.ps1

$secpasswd = ConvertTo-SecureString $env:Password -AsPlainText -Force;
$userCredential = New-Object System.Management.Automation.PSCredential ($env:ApplicationId, $secpasswd)
Add-AzureRmAccount -TenantId $env:TenantId -ServicePrincipal -SubscriptionId $env:SubscriptionId -Credential $userCredential
Get-AzureRmSubscription –SubscriptionId $env:SubscriptionId | Select-AzureRmSubscription

最佳答案

在脚本中保存帐户信息对您来说是不安全的。我建议您可以将这些存储到应用程序设置。您可以找到您的函数应用-->设置-->应用程序设置-->管理应用程序设置-->应用程序设置以及设置 SP_USERNAME、SP_PASSWORD 和 TENANTID、SubscriptionId 的键值对(您也可以使用其他值或更多 key 对)。 enter image description here

修改您的脚本如下:

# Set Service Principal credentials
    # SP_PASSWORD, SP_USERNAME, TENANTID are app settings
    $secpasswd = ConvertTo-SecureString $env:SP_PASSWORD -AsPlainText -Force;
    $mycreds = New-Object System.Management.Automation.PSCredential ($env:SP_USERNAME, $secpasswd)
    Add-AzureRmAccount -ServicePrincipal -Tenant $env:TENANTID -Credential $mycreds;
    Get-AzureRmSubscription –SubscriptionId $env:subscriptionId | Select-AzureRmSubscription

当您想要修改您的帐户信息时,您不需要修改您的脚本,您只需要修改应用程序设置即可。您可以使用 Azure CLI 修改应用程序设置。

关于powershell - 验证多个 Azure Powershell 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44551270/

相关文章:

regex - 在 PowerShell 中用环境变量替换正则表达式 token

powershell - 从 VS Team Services 构建中运行的 Powershell 创建工作区

azure - 获取 Azure 数据工厂日志

azure - 如何将 Kusto 查询输出解析为自动化脚本(Powershell 或 Python)

json - Azure 逻辑应用程序返回难以使用的 json 字符串

Azure函数: make schedule cron a variable in function. json

xml - 如何使用Powershell从xml过滤以获取启动持续时间?

azure - 无法在 DevOps Server 中构建 VB6 应用程序

azure webrole Global.Application_Start 从未被调用

azure - 仅将 Azure Function 中间件用于选定的函数