【问题标题】:How to read SQL rows as PowerShell parameters如何将 SQL 行读取为 PowerShell 参数
【发布时间】: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


【解决方案1】:

一种方法是将数据库列表加载到DataTable 并为每个查询迭代列表。根据您的身份验证方法更改下面示例代码中的$databaseListConnectionString,并根据需要设置连接AccessToken

# get database list
$databaseListConnectionString = "Data Source=YourServer;Initial Catalog=YourDatabase"
$databaseListQuery = "SELECT ServerName, DatabaseName FROM dbo.DatabaseList WHERE Status = 'Process';"
$dataAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($databaseListQuery, $databaseListConnectionString)
$dataAdapter.SelectCommand.Connection.AccessToken = $access_token
$databaseList = New-Object System.Data.DataTable
[void]$dataAdapter.Fill($databaseList)

# 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
        foreach($database in $databaseList.Rows) {
            Invoke-Sqlcmd -ServerInstance "$($database.ServerName)" -Database "$($database.DatabaseName)" -Query $query -AccessToken $access_token
            Write-Output ("This file is executed :" + $file.Name)
        }
    }
}

【讨论】:

  • 非常感谢您的回答。我一直在用我的身份验证方法尝试这种方法,但失败了。 SQL Server - Runbook 连接是通过专用端点完成的,我正在为我的“Invoke-Sqlcmd”命令使用访问令牌。有没有办法我可以在这里为“$databaseListConnectionString”遵循类似的方式?
  • @Strayda,在实例化新的 SqlDataAdapter 后尝试添加这一行:$dataAdapter.SelectCommand.Connection.AccessToken = $access_token。我认为您可以使用令牌从连接字符串中删除;Integrated Security=SSPI
  • 你是英雄!非常感谢!
  • 感谢您的确认。我将为您的用例编辑我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-25
  • 2021-03-14
相关资源
最近更新 更多