如何在Azure Runbook中运行SQL批处理?

时间:2016-06-11 10:37:27

标签: sql sql-server azure cloud azure-automation

我需要执行一个批处理来在我的数据库中执行一些维护任务,但我看到的Azure Automation上的所有示例都在处理a single SQL command

如果创建SP不是一个选项,我该怎么办?我想我需要以某种方式将我的 script.sql 文件嵌入到Runbook脚本中或引用它(例如here)?

1 个答案:

答案 0 :(得分:1)

您可以将.sql文件存储在Azure Blob存储中,并在Runbook下载.sql文件,读取其内容,并将其传递给SqlCommand对象。

类似的东西:

try {
    # Connect to Azure using service principal auth
    $ServicePrincipalConnection = Get-AutomationConnection -Name $AzureConnectionAssetName         

    Write-Output "Logging in to Azure..."

    $Null = Add-AzureRmAccount `
        -ServicePrincipal `
        -TenantId $ServicePrincipalConnection.TenantId `
        -ApplicationId $ServicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $ServicePrincipalConnection.CertificateThumbprint 
}
catch {
    if(!$ServicePrincipalConnection) {
        throw "Connection $AzureConnectionAssetName not found."
    }
    else {
        throw $_.Exception
    }
}

$Path = "C:\abc.sql"
Set-AzureRmCurrentStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName

Get-AzureStorageBlobContent -Container $Container -Blob $Blob -Destination $Path
$Content = Get-Content $Path
$Cmd = New-Object System.Data.SqlClient.SqlCommand($Content, $Conn)