从一个 Azure Automation Runbook 调用另一个时出错

Error calling one Azure Automation Runbook from another

背景

我有一个 Azure Runbook(名为 RunStoredProcedure2)定义如下:

param(
[parameter(Mandatory=$True)]
[string] $SqlServer,

[parameter(Mandatory=$False)]
[int] $SqlServerPort = 1433,

[parameter(Mandatory=$True)]
[string] $Database,

[parameter(Mandatory=$True)]
[string] $StoredProcedureName,

[parameter(Mandatory=$True)]
[PSCredential] $SqlCredential
)

# Get the username and password from the SQL Credential
$SqlUsername = $SqlCredential.UserName
$SqlPass = $SqlCredential.GetNetworkCredential().Password

$Conn = New-Object System.Data.SqlClient.SqlConnection("Server=tcp:$SqlServer,$SqlServerPort;Database=$Database;User ID=$SqlUsername;Password=$SqlPass;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;")

$Conn.Open()

$Cmd=new-object system.Data.SqlClient.SqlCommand("EXEC 
$StoredProcedureName", $Conn)
$Cmd.CommandTimeout=120


$Cmd.ExecuteNonQuery()
# Close the SQL connection
$Conn.Close()

我现在正尝试使用此命令从另一个 Azure Runbook 调用此 运行本书:

& .\RunStoredProcedure2.ps1 -Database 'adventureworksnh.database.windows.net' -SqlServer 'AdventureWorksDW' -SqlServerPort 1433 -StoredProcedureName 'TestJob1' -sqlCredential 'awadmin'

问题

当我尝试运行这个时,我得到这个错误:

C:\Temp\uzahthmc.su1\RunStoredProcedure2.ps1 : Cannot process argument transformation on parameter 'SqlCredential'. A command that prompts the user failed because the host program or the command type does not support user interaction. The host was attempting to request confirmation with the following message: Enter your credentials.

我能够 运行 RunStoredProcedure 使用这些参数成功

我试过的

您的自动化帐户中是否有名为 "awadmin" 的凭证资产?直接启动 Runbook 时(使用门户中的“开始”按钮或 Start-AzureRmAutomationRunbook),Azure 自动化允许你将凭据资产名称作为参数值传递,它会自动检索指定的凭据。但是,当您从另一个 runbook 调用一个 runbook 时,您必须遵循简单的 PowerShell 规则并传递一个 PSCredential 对象,如声明的那样:

$sqlCred = Get-AutomationPSCredential -Name 'awadmin'
& .\RunStoredProcedure2.ps1 ... -sqlCredential $sqlCred