无法在Azure自动化运行簿中获取凭据

时间:2018-10-10 13:02:54

标签: azure azure-sql-database azure-automation

我正在尝试使用azure Runbook(下面的代码)进行基本的数据库操作。当我在Azure云上对其进行测试时,它失败了。错误-enter image description here

通过逐行执行,我发现此错误是由Get-AutomationPSCredential引起的。但是,当我从本地系统中的PowerShell ISE运行它时,整个代码运行良好。

workflow sqlrunbook()
{
    $SqlServer = "devserver001.database.windows.net"
    $Database = "devdb001"

    $SqlCredential = Get-AutomationPSCredential -Name 'SqlCredentialAsset'

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

    inlinescript
    {        
        $Conn = New-Object System.Data.SqlClient.SqlConnection("Server=tcp:$using:SqlServer;Database=$using:Database;User ID=$using:SqlUsername;Password=$using:SqlPassword;")

        $Cmd = New-Object System.Data.SqlClient.SqlCommand("insert into TestTable(ID) values(1)", $Conn)

        $Cmd.CommandTimeout=120

        $Conn.Open()        
        $Cmd.ExecuteNonQuery()
        $Conn.Close()
    }
}
sqlrunbook

如果我在创建凭据时错过了任何提示,或者我应该启用任何配置以在Azure云上测试此运行手册,是否可以让我知道?预先感谢。enter image description here

1 个答案:

答案 0 :(得分:0)

尝试:

Get-AzureAutomationCredential -AutomationAccountName "Contoso17" -Name "MyCredential"

您还可以检查此链接: https://docs.microsoft.com/en-us/azure/automation/automation-credentials