【问题标题】:Can't get a runbook test (in portal) to ask for input parameters无法获取 Runbook 测试(在门户中)以请求输入参数
【发布时间】: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


【解决方案1】:

我不能 100% 确定这一点,因为我实际上选择删除参数,只使用硬编码的服务器名称和对凭据的硬编码引用(它不会改变)。但是当我这样做时,我遇到了不同的问题,我在这里发布了这些问题:Runbook test pane is not showing Write-Output ... 答案是代码声明了工作流运行手册,而 Azure 中的运行手册是常规的 Powershell 运行手册。 (直到现在我才意识到有区别)。但这导致代码实际上根本没有运行。一旦我删除了工作流定义和内联脚本块(只留下代码),并删除了 $using 并修复了其他一些东西,它就起作用了。

我的猜测是,因为脚本根本没有真正运行,这就是为什么它之前没有要求参数,如果我选择保留参数,然后删除工作流定义和内联脚本块会解决这个问题。

【讨论】:

    【解决方案2】:

    它说没有输入参数,因为 AFAIK 您没有正确提供运行手册所需的输入凭据。您必须使用 Get-AutomationPSCredential cmdlet 来做到这一点。我还没有进行端到端测试,但很可能您可以点击this 链接来完成您的要求。

    希望这会有所帮助!

    【讨论】:

    • 感谢您的回复,对延误表示抱歉。以下是我添加凭据的方法:在 Azure 门户中,在自动化帐户中,我转到“共享资源”下的“凭据”。前往“添加凭据”,为其命名,如“myDatabase-automation”,并输入用户名和密码。叫什么名字重要吗?是这个问题吗?除此之外,没有太多空间可以搞砸了。
    【解决方案3】:

    您好,您需要在删除后将其添加到脚本的顶部

    workflow DB_DailyTasks 
    { 
    

    inlinescript
    {
    
    <#
    .SYNOPSIS
         ..
    
    .DESCRIPTION
        ... 
    
    .PARAMETER param1
        ...
    
    .PARAMETER param2
        ...
    
    .PARAMETER param3
        ... 
    #>
    ```
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-09
      • 1970-01-01
      • 1970-01-01
      • 2017-03-21
      • 2021-12-01
      • 1970-01-01
      • 2014-06-23
      • 2015-02-02
      相关资源
      最近更新 更多