【发布时间】:2022-11-03 22:33:31
【问题描述】:
在 Azure 上,我通过 Powershell Runbook 从 100 个 Azure SQL 数据库中的容器运行多个 .sql 文件。
我希望 Powershell 阅读服务器名称和数据库名称从我的 SQL Server 表中运行脚本,如下所示:
| Servername | Databasename | Status |
|---|---|---|
| Server-01 | DB-01 | Process |
| Server-01 | DB-02 | Skip |
| Server-02 | DB-03 | Process |
在我当前版本的 Powershell 脚本中,它可以读取容器中的文件并在给定的服务器和数据库中运行它们:
# Get the blob container
$blobs = Get-AzStorageContainer -Name $containerName -Context $ctx | Get-AzStorageBlob
# Download the blob content to localhost and execute each one
foreach ($blob in $blobs)
{
$file = Get-AzStorageBlobContent -Container $containerName -Blob $blob.Name -Destination "." -Context $ctx
Write-Output ("Processing file :" + $file.Name)
$query = Get-Content -Path $file.Name
Invoke-Sqlcmd -ServerInstance "Server-01.database.windows.net" -Database "DB-01" -Query $query -AccessToken $access_token
Write-Output ("This file is executed :" + $file.Name)
}
我正在寻找一种方法来读取表中的行并将它们输入-服务器实例和-数据库中的字段调用-Sqlcmd.理想情况下,它可以过滤掉跳过行。
【问题讨论】:
-
您是否打算对 SQL 表中的每个数据库运行相同的脚本,Status = 'Process'?
-
这个服务器名称和数据库表存储在哪里?
-
@DanGuzman 是的,多个服务器中多个数据库中的相同脚本显示“进程”
-
@Larnu 该表存储在另一个 Azure SQL 数据库中
标签: sql-server powershell azure-sql-database azure-powershell azure-automation