我正在尝试使用azure Runbook(下面的代码)进行基本的数据库操作。当我在Azure云上对其进行测试时,它失败了。错误-
通过逐行执行,我发现此错误是由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
答案 0 :(得分:0)
尝试:
Get-AzureAutomationCredential -AutomationAccountName "Contoso17" -Name "MyCredential"
您还可以检查此链接: https://docs.microsoft.com/en-us/azure/automation/automation-credentials