来自另一本RunBook的Azure Automation RunBook

时间:2018-12-16 10:46:49

标签: powershell azure-sql-database azure-automation azure-runbook

我的Runbook名称为“ RB_ConnectSQL”

workflow RB_ConnectSQL
{
    [OutputType([string])]
     param
     (
        [Parameter(Mandatory=$true)] [string] $SqlServer,
        [Parameter(Mandatory=$false)] [int] $SqlServerPort = 1433,
        [Parameter(Mandatory=$true)] [string] $Database,
        [Parameter(Mandatory=$true)] [string] $Procedure,
        [Parameter(Mandatory=$true)] [string] $SqlCredentialName
     )
    $SqlCredential = Get-AutomationPSCredential -Name $SqlCredentialName
    $SqlUsername = $SqlCredential.UserName
    $SqlPass = $SqlCredential.GetNetworkCredential().Password

     inlinescript
     {
         $haveError = 0
         $DatabaseConnection = New-Object System.Data.SqlClient.SqlConnection("Server=tcp:$using:SqlServer,$using:SqlServerPort; Database=$using:Database; User ID=$using:SqlUsername;Password=$using:SqlPass; Trusted_Connection=False; Encrypt=True; Connect Timeout=7200;") 

         $outputDataTable = New-Object System.Data.DataTable
            [string[]] $ColumnNames
             try
             {
                $DatabaseConnection.Open()
                $Cmd=new-object system.Data.SqlClient.SqlCommand
                $Cmd.Connection = $DatabaseConnection
                $Cmd.CommandText = 'EXEC ' + $using:Procedure + ';'
                $Cmd.CommandTimeout = 7200
                $sqlDataAdapter = New-Object System.Data.SqlClient.SqlDataAdapter $Cmd
                $dataSet = New-Object System.Data.DataSet
                Write-Output($Cmd.CommandText)
                $sqlDataAdapter.Fill($dataSet) | out-null
                if ($dataSet.Tables[0].Rows.Count -gt 0)
                {
                    $outputDataTable = $dataSet.Tables[0]
                }
                else
                {
                    $outputDataTable = "SQL Stroc Proc Executed”
                }
             }
            catch
            {
                 #write your own error handling code here.
                 #if required send error message in email.                
            }
             finally
            {
                 if ($Cmd -ne $null)
                 {
                    $Cmd.Dispose
                 }

                 $DatabaseConnection.Close()
                 $DatabaseConnection.Dispose()
            }
        }
    }

和另一本Runnbook名称“ RB_Daily_Transaction_Summary_Record”

workflow RB_Daily_Transaction_Summary_Record
{
    $dataTable = RB_ConnectSQL -SqlServer 'blahblah.database.windows.net' -Database 'blahDev' -Procedure 'sp_Daily_Transaction_Summary_Record' -SqlCredentialName 'blahCredential'    
    Write-Output($dataTable)
}

运行手册“ RB_Daily_Transaction_Summary_Record”假设调用“ RB_ConnectSQL”并传入所需的参数,以便在Azure SQL Server中执行存储过程。 但是我得到了错误

  

在线:78 char:17    + -SqlServer'blahblah.database.windows.net'    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~找不到'-SqlServer命令。如果将此命令定义为工作流,请确保在调用它的工作流之前定义了该命令。如果该命令旨在直接在Windows PowerShell中运行(或在此系统上不可用),请将其放在InlineScript中:'InlineScript {-SqlServer}'

我可以知道我在Runbook上犯任何错误吗?

2 个答案:

答案 0 :(得分:1)

要将命令分成多行时,请在每行末尾添加white-space and backquote,最后一行除外。

对于您而言,它应使用以下格式工作:

workflow RB_Daily_Transaction_Summary_Record
{
    $dataTable = RB_ConnectSQL
                 -SqlServer 'blahblah.database.windows.net' `
                 -Database 'blahDev' `
                 -Procedure 'sp_Daily_Transaction_Summary_Record' `
                 -SqlCredentialName 'blahCredential'    
    Write-Output($dataTable)
}

答案 1 :(得分:0)

我发现问题是我将代码分开以提高可读性

workflow RB_Daily_Transaction_Summary_Record
{
    $dataTable = RB_ConnectSQL
                 -SqlServer 'blahblah.database.windows.net'
                 -Database 'blahDev'
                 -Procedure 'sp_Daily_Transaction_Summary_Record'
                 -SqlCredentialName 'blahCredential'    
    Write-Output($dataTable)
}

但是新行似乎破坏了代码。

我将其重写为:

workflow RB_Daily_Transaction_Summary_Record
{
    $dataTable = RB_ConnectSQL -SqlServer 'blahblah.database.windows.net' -Database 'blahDev' -Procedure 'sp_Daily_Transaction_Summary_Record' -SqlCredentialName 'blahCredential'    
    Write-Output($dataTable)
}

然后错误消失了,它运行良好!