azure - PowerShell::Microsoft.Azure.Commands.Sql.Database.Model.AzureSqlDatabaseModel.DatabaseName

标签 azure powershell azure-sql-database azure-powershell azure-sql-server

我编写了一个脚本,允许我查询整个 Azure 数据库公园:

#$ErrorActionPreference = 'SilentlyContinue'
# Connect to Azure
$azureAccount = Connect-AzAccount 
# Get Azure Access Token (we will use this to query the databasees)
#$azureToken = Get-AzAccessToken -ResourceUrl https://database.windows.net
$access_token = (Get-AzAccessToken -ResourceUrl https://database.windows.net).Token
# Queries will be picked up from here
$folderPath = '.\Queries'
# Choose how to format each date ("yyyy-MM-dd") or ("yyyy-MM-dd HH:mm:ss")
$DateTime = (Get-Date).ToString("yyyy-MM-dd")
# List Azure Sunscriptions
Get-Azsubscription | ForEach-Object -Begin { $a = 1 } -Process {"$a $($_.Name)"; $a++}
$SubscriptionChoice = Read-Host -Prompt "Copy/paste the name of the Subscription that you want to investigate. If more than one separate them by a coma, Type `"All`" if you want to target all of them"
# Iterate into subscriptoins and print names
foreach ($gs in $SubscriptionChoice) {
    Select-Azsubscription -Subscription "$gs" | Out-Null
    Write-Host "Let's browse into Azure Sunscription: " -NoNewline
    Write-Host (Get-AzContext).Subscription.Name -ForegroundColor green
    # Fins all Azure SQL Server
    Get-AzSqlServer | ForEach-Object -Begin { $a = 1 } -Process {"$a $($_.ServerName)"; $a++}
    $SqlServerChoice = Read-Host -Prompt "Copy/paste the name of the SQL Server that you want to investigate. If more than one separate them by a coma, Type `"All`" if you want to target all of them"
    
    if ($SqlServerChoice = "All"){
        $SqlServerChoice = Get-AzSqlServer
        }
    
    Foreach ($server in $SqlServerChoice){
        $DatabaseChoice = Get-AzSqlDatabase -ServerName $server.ServerName -ResourceGroupName $server.ResourceGroupName | Where-Object DatabaseName -NE "master"
        Foreach ($database in $DatabaseChoice){
            (Get-ChildItem $folderPath | sort-object {if (($i = $_.BaseName -as [int])) {$i} else {$_}} ).Foreach{
            Invoke-Sqlcmd -ServerInstance $server.FullyQualifiedDomainName -Database $database.DatabaseName -AccessToken $access_token -InputFile $psitem.FullName | Export-Csv -Path ".\Results\$psitem.csv" -Append -NoTypeInformation
            write-host "Executing $psitem on $database.DatabaseName"
            }
        }
    }   
}

但是,每次对数据库执行查询时,写入主机都会返回:

Executing DTU_to_vCore.sql on Microsoft.Azure.Commands.Sql.Database.Model.AzureSqlDatabaseModel.DatabaseName

这是一张图片:

enter image description here

此写入主机来自以下行:

write-host "Executing $psitem on $database.DatabaseName"

其中您可以找到两个变量:

  • $psitem:这是包含查询的文件的名称
  • $database.DatabaseName :应该是数据库名称,但不是打印数据库名称而是打印 Microsoft.Azure.Commands.Sql.Database.Model.AzureSqlDatabaseModel.DatabaseName

为什么两个变量之一没有被解释?

最佳答案

您需要将变量属性封装在子表达式运算符$()中。

write-host "Executing $psitem on $($database.DatabaseName)"

这是因为只有简单的变量才会在可扩展字符串中扩展。

引用文献

Only simple variable references can be directly embedded in an expandable string. Variables references using array indexing or member access must be enclosed in a subexpression.

来源:about_Quoting_Rules

子表达式运算符 $( )

Returns the result of one or more statements. For a single result, returns a scalar. For multiple results, returns an array. Use this when you want to use an expression within another expression. For example, to embed the results of command in a string expression.

PS> "Today is $(Get-Date)"
Today is 12/02/2019 13:15:20

PS> "Folder list: $((dir c:\ -dir).Name -join ', ')"
Folder list: Program Files, Program Files (x86), Users, Windows

来源:about_Operators

关于azure - PowerShell::Microsoft.Azure.Commands.Sql.Database.Model.AzureSqlDatabaseModel.DatabaseName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73824211/

相关文章:

c# - PowerShell AD 查询与 C# AD 查询 - 速度

powershell - 使用 PowerShell 匹配存储在变量中的字符串

powershell - 如何在 Powershell 的完全限定路径名中使用空格?

sql-server - SQL Azure 是否支持 'FOR XML'

azure - 如何在 Azure HDInsight 上的 Spark 中设置 Parquet block 大小?

c# - 谁能给我指出一个控制台应用程序访问 azure 存储的示例

asp.net-mvc - Html.RouteLink 在部署到 Azure 后停止生成链接

azure - 请验证 azure 搜索服务是否已启动,重新启动 WebApp,然后重试

Azure 数据工厂管道 - 将单值源查询输出存储为变量,然后在复制数据事件中使用

sql-server - 使用参数时查询很慢,但使用文字时查询很快