调用 Powershell 脚本和无人值守登录的 Azure 函数
Azure Function calling a Powershell script and unattended login
我正在创建一个将调用 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 和密码。然后他们可以执行该应用可以执行的任何 activity。
我这样做是正确的还是没理解什么?
您可以使用 App Settings 定义环境变量并在其中存储密码,然后在您的代码中读取它们即可:
$username = $env:username
$password = ConvertTo-SecureString $env:password -AsPlainText -Force
我正在创建一个将调用 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 和密码。然后他们可以执行该应用可以执行的任何 activity。
我这样做是正确的还是没理解什么?
您可以使用 App Settings 定义环境变量并在其中存储密码,然后在您的代码中读取它们即可:
$username = $env:username
$password = ConvertTo-SecureString $env:password -AsPlainText -Force