Azure函数调用Powershell脚本和无人参与的登录

问题描述 投票:1回答:1

我正在创建一个将调用PowerShell脚本的Azure函数。为了做到这一点,我需要让PS脚本进行无人值守的登录。所以我创建了一个应用程序和服务主体,如下所示:

# Create an Azure Active Directory Application that will be used for authentication in Powershell Automation scripts. 
$Password = ConvertTo-SecureString '<MyPassword>' -AsPlainText -Force
$AzureAdApplication = New-AzureRmADApplication -DisplayName "PowerShellAdminApp" -Password $Password -HomePage "https://www.phoenix.com" -IdentifierUris "https://www.phoenix.com"

# Create the Service Principal
New-AzureRmADServicePrincipal -ApplicationId $AzureAdApplication.ApplicationId

# Add permissions to the Server Principal
New-AzureRmRoleAssignment -RoleDefinitionName Contributor -ServicePrincipalName $AzureAdApplication.ApplicationId.Guid

一切正常。

然后,在我的PS脚本中,我将以无人值守的方式登录,如下所示:

$Username = "https://www.phoenix.com"
$Password = ConvertTo-SecureString "<MyPassword>" -AsPlainText -Force
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $Username, $Password
Login-AzureRmAccount -ServicePrincipal -Credential $Credential -TenantId '<MyTenantId'

这也有效。但是,我觉得我不理解某些东西或者我错过了什么。这根本不安全。如果我必须在我的所有PS脚本中都有此登录代码,我基本上让任何有权访问这些脚本的人都能看到租户ID和应用程序的密码。然后他们可以执行应用程序可以执行的任何活动。

我这样做是正确还是不理解?

powershell azure-functions azure-powershell service-principal
1个回答
0
投票

您可以使用应用程序设置来定义环境变量并在那里存储密码,并在您的代码中读取它们:

$username = $env:username
$password = ConvertTo-SecureString $env:password -AsPlainText -Force

以下是配置方法。 https://docs.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings

© www.soinside.com 2019 - 2024. All rights reserved.