无法在 Azure 自动化 Runbook 中获取凭据
Failed to get Credential in Azure Automation Runbook
我正在尝试使用 azure 运行book(下面的代码)进行基本的数据库操作。当我在 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
如果我在创建凭据时遗漏了任何内容,或者我是否应该启用任何配置来在 Azure 云上测试这本 运行 图书,您能否告诉我?提前致谢。
试试这个:
Get-AzureAutomationCredential -AutomationAccountName "Contoso17" -Name "MyCredential"
您还可以查看此 link:
https://docs.microsoft.com/en-us/azure/automation/automation-credentials
我正在尝试使用 azure 运行book(下面的代码)进行基本的数据库操作。当我在 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
如果我在创建凭据时遗漏了任何内容,或者我是否应该启用任何配置来在 Azure 云上测试这本 运行 图书,您能否告诉我?提前致谢。
试试这个:
Get-AzureAutomationCredential -AutomationAccountName "Contoso17" -Name "MyCredential"
您还可以查看此 link: https://docs.microsoft.com/en-us/azure/automation/automation-credentials