【发布时间】:2021-09-29 07:14:59
【问题描述】:
我是自动化的新手,我正在尝试获取一个运行手册来连接到 sql 数据库并运行存储过程。问题是,我正在使用的代码(改编自https://azure.microsoft.com/en-us/blog/azure-automation-your-sql-agent-in-the-cloud/)在我尝试测试它时并没有要求服务器和凭据参数。测试窗口显示“没有输入参数”。
这是我的(通用)代码:
workflow DB_DailyTasks
{
param
(
# Fully-qualified name of the Azure DB server
[parameter(Mandatory=$true)]
[string] $SqlServerName="mydb.database.windows.net",
# Credentials for $SqlServerName stored as an Azure Automation credential asset
# When using in the Azure Automation UI, please enter the name of the credential asset for the "Credential" parameter
[parameter(Mandatory=$true)]
[PSCredential] $Credential
)
inlinescript
{
# Setup credentials
$ServerName = $Using:SqlServerName
$UserId = $Using:Credential.UserName
$Password = ($Using:Credential).GetNetworkCredential().Password
# Execute the udp_Test procedure
# Create connection for each individual database
$DatabaseConnection = New-Object System.Data.SqlClient.SqlConnection
$DatabaseCommand = New-Object System.Data.SqlClient.SqlCommand
$DbName = "myDB"
# Setup connection string for $DbName
$DatabaseConnection.ConnectionString = "Server=$ServerName; Database=$DbName; User ID=$UserId; Password=$Password;"
$DatabaseConnection.Open();
# Create command for a specific database $DBName
$DatabaseCommand.Connection = $DatabaseConnection
Write-Output "Running udp_Test procedure"
$DatabaseCommand.CommandText = "EXECUTE [dbo].[udp_Test]"
$NonQueryResult = $DatabaseCommand.ExecuteNonQuery()
# Close connection to $DbName
$DatabaseConnection.Close()
}
}
我在自动化帐户中存储了一些凭据,但我无法让测试实际询问它们!当我测试时,它说“没有输入参数”。是不是我做错了什么?
【问题讨论】:
-
如果您所做的只是运行内联脚本,那么工作流程的意义何在?另外,我认为您不能为强制参数设置默认值。我也相当确定您没有以适当的方式检索凭据:docs.microsoft.com/en-us/azure/automation/shared-resources/…
-
一旦我得到它的工作,我打算把它放在一个时间表上。 Runbook 显然是 Azure 对 SQL 代理的回应。
标签: azure powershell azure-automation