来自另一个 RunBook 的 Azure 自动化 RunBook
Azure Automation RunBook from Another 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)
}
Runbook "RB_Daily_Transaction_Summary_Record" 假设调用 "RB_ConnectSQL" 并传入所需的参数,以便在 Azure SQL 服务器中执行存储过程。
但是我收到错误
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 }'
请问我在操作手册上有什么错误吗?
我发现问题是我将代码分开以提高可读性
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)
}
然后错误消失,运行良好!
当您要将一条命令分成多行时,请在每行末尾添加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)
}
我有一个 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)
}
Runbook "RB_Daily_Transaction_Summary_Record" 假设调用 "RB_ConnectSQL" 并传入所需的参数,以便在 Azure SQL 服务器中执行存储过程。 但是我收到错误
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 }'
请问我在操作手册上有什么错误吗?
我发现问题是我将代码分开以提高可读性
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)
}
然后错误消失,运行良好!
当您要将一条命令分成多行时,请在每行末尾添加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)
}