1
votes

I have a runbook name "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()
            }
        }
    }

And another runnbook name "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)
}

The runbook "RB_Daily_Transaction_Summary_Record" suppose to call "RB_ConnectSQL" and pass in the required parameter so that will execute the Store Procedure in Azure SQL Server. However I get the error

At line:78 char:17 + -SqlServer 'blahblah.database.windows.net' + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Cannot find the '-SqlServer' command. If this command is defined as a workflow, ensure it is defined before the workflow that calls it. If it is a command intended to run directly within Windows PowerShell (or is not available on this system), place it in an InlineScript: 'InlineScript { -SqlServer }'

May I know is there any mistake I make on the runbook?

2

2 Answers

2
votes

When you want to separate a command into many lines, please add white-space and backquote at the end of each line, except for the last line.

In your case, it should work using the following format:

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)
}
0
votes

I found out that the issue was I separate my code for readability become

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)
}

But the new line seem like break the code.

I rewrite it as:

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)
}

Then the error gone and it work well!