Pass two values to Azure cmdlet

Hello,

I’ve been working on this script to retrieve database information from Azure. I had to limit some of the functionality due to setting an Azure context to a specific subscription. Other posts show what I was trying to accomplish.

With this part of the script, I pull the resource group and sql server assigned to that resource group from a .csv file. Now, I’d like to assign the values from the .csv file to the ‘Get-AzSqlDatabase’ cmdlet which requires both values to return a SQL database.

Here’s what I had that was slightly working at one point until I change my approach:

[pre]

$scrubbed = Import-CSV $path | Where-Object{$.ResourceGroupName -and $.ServerName}

$dbs =
foreach ($s in $scrubbed) {
$db = Get-AzSqlDatabase -ServerName $ServerName -ResourceGroupName $ResourceGroupName -ErrorAction SilentlyContinue
[PSCustomObject]@{
$ResourceGroupName = $s.ResourceGroupName
$ServerName = $s.ServerName
}

}

[/pre]

Here’s the error:

[pre]

Get-AzSqlDatabase : Cannot validate argument on parameter ‘ServerName’. The argument is null or empty. Provide an argument that is not null or empty, and then try the command
again.
At line:3 char:49

  • $db = Get-AzSqlDatabase -ServerName $ServerName -Resource …
  • CategoryInfo : InvalidData: (:slight_smile: [Get-AzSqlDatabase], ParameterBindingValidationException
  • FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Azure.Commands.Sql.Database.Cmdlet.GetAzureSqlDatabase

A null key is not allowed in a hash literal.
At line:5 char:17

  • $ResourceGroupName = $s.ResourceGroupName
  • CategoryInfo : InvalidOperation: (System.Collecti…deredDictionary:OrderedDictionary) , RuntimeException
  • FullyQualifiedErrorId : InvalidNullKey

[/pre]

Thanks for your help!

 

minor detail… lol

$scrubbed = Import-CSV $path | Where-Object{$_.ResourceGroupName -and $_.ServerName}

$dbs = foreach ($s in $scrubbed) {
    $db = Get-AzSqlDatabase -ServerName $S.ServerName -ResourceGroupName $S.ResourceGroupName -ErrorAction SilentlyContinue
    [PSCustomObject]@{
        $ResourceGroupName = $s.ResourceGroupName
        $ServerName        = $s.ServerName
    }
}

Ah, yes, I did miss that. thx. Lol.

It still failed, but when I left in the headers in the .csv file input it somewhat worked. Here’s the error I’m receiving:

[pre]

A null key is not allowed in a hash literal.
At line:5 char:17

  • $ResourceGroupName = $s.ResourceGroupName
  • CategoryInfo : InvalidOperation: (System.Collecti…deredDictionary:OrderedDictionary) , RuntimeException
  • FullyQualifiedErrorId : InvalidNullKey

[/pre]

Revised code, without removing headers:

[pre]

$scrubbed = Import-CSV $path | Where-Object{$.ResourceGroupName -and $.ServerName}
#$scrubbed = $scrubbed | ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1
#$scrubbed = $scrubbed -replace ‘"’,‘’

$dbs =
foreach ($s in $scrubbed) {
$db = Get-AzSqlDatabase -ServerName $s.ServerName -ResourceGroupName $s.ResourceGroupName -ErrorAction SilentlyContinue
[PSCustomObject]@{
$ResourceGroupName = $s.ResourceGroupName
$ServerName = $s.ServerName
}

}

[/pre]

So I receive the error above, but I also receive output of only one server similar to the below:

server1 database1 other1 other2
server1 database2 other1 other2
It will leave some space between the header of the return columns.

Thanks in advance for your help!