【问题标题】:Azure Automation errorAzure 自动化错误
【发布时间】:2015-08-02 04:37:41
【问题描述】:

我在 Azure 自动化中有以下 PowerShell 脚本。它所做的只是运行一个名为AddWeeks 的存储过程。

workflow GenerateWeeks
{
    $serverInstance="[my_db_server]" 
    $userName="[my_username]"
    $password="[my_password]"

    [void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.ConnectionInfo') |out-null
    $ServerConnection =new-object Microsoft.SqlServer.Management.Common.ServerConnection $serverInstance,$userName, $password
    $ServerConnection.ExecuteNonQuery("declare @date date set @date=getdate()   exec [xxxx].dbo.AddWeeks 300,@date")    

}

但是当我进行测试运行时,我收到以下错误:

Runbook definition is invalid. Could not find type Microsoft.SqlServer.Management.Common.ServerConnection. Load the type(s) and try again. 

有什么问题?

编辑

在 jisaak 的帮助下,我有了这个最终的工作版本:

workflow GenerateWeeks
{
    InlineScript 
    {
        $con = New-Object System.Data.SqlClient.SqlConnection
        $con.ConnectionString = "Server = xxx; Database=xxx; User ID = xxx; Password = xxx;"
        $con.Open();
        $sql = "declare @date date set @date=getdate()   exec [xxx].dbo.AddWeeks 300,@date"
        $cmd = New-Object System.Data.SqlClient.SqlCommand($sql,$con)
        $cmd.CommandTimeout=300
        $cmd.ExecuteNonQuery()  
    }
}

我设置了 CommandTimeout,因为当我调试时它会抛出异常:

A transport-level error has occurred when receiving results from the server. (provider: TCP Provider, error: 0 - The specified network name is no longer available.)

【问题讨论】:

    标签: powershell azure azure-automation


    【解决方案1】:

    错误指出,Microsoft.SqlServer.Management.Common.ServerConnection 不可用。请改用System.Data.SqlClient.SqlConnection

    $con = New-Object System.Data.SqlClient.SqlConnection
    $con.ConnectionString = "Server = $serverInstance; User ID = $userName; Password = $password;"
    $con.Open();
    $cmd = $con.CreateCommand("declare @date date set @date=getdate()   exec [xxxx].dbo.AddWeeks 300,@date")
    $cmd.ExecuteNonQuery()
    

    我不确定,但您可能必须将脚本包装在 inlinescript 中。

    【讨论】:

    • 非常感谢!我对您的代码进行了一些更改,并在上面发布了工作版本。我很好奇 - [System.Reflection.Assembly]::LoadWithPartialName 在 Azure 上不起作用?但它适用于本地环境。
    猜你喜欢
    • 1970-01-01
    • 2018-03-06
    • 2021-09-07
    • 1970-01-01
    • 2019-07-03
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    • 2017-07-15
    相关资源
    最近更新 更多