Azure自动化错误

时间:2015-07-31 23:25:19

标签: powershell azure azure-automation

我在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.)

1 个答案:

答案 0 :(得分: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中。