Pass two values to Azure cmdlet

Welcome Forums General PowerShell Q&A Pass two values to Azure cmdlet

Viewing 2 reply threads
  • Author
    Posts
    • #223845
      Participant
      Topics: 21
      Replies: 41
      Points: 301
      Rank: Contributor

      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:

      
      $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
      }
      
      }
      
      

      Here’s the error:

      
      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: (:) [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
      
      

      Thanks for your help!

       

      • This topic was modified 1 month ago by anelliaf38.
    • #223857
      Participant
      Topics: 12
      Replies: 523
      Points: 1,214
      Helping Hand
      Rank: Community Hero

      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
          }
      }
      
    • #224118
      Participant
      Topics: 21
      Replies: 41
      Points: 301
      Rank: Contributor

      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:

      
      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
      
      

      Revised code, without removing headers:

      
      $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
      }
      
      }
      
      

      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!

Viewing 2 reply threads
  • You must be logged in to reply to this topic.