question

DeHavenGraham-3555 avatar image
0 Votes"
DeHavenGraham-3555 asked SathyamoorthyVijayakumar-MSFT edited

Passthrough Runbook CSV output to my blob container

The following script outputs a CSV in the Runbook console but what exactly must I add to my script to export/redirect the CSV output to a file to export in my storage account blob container?

 $connectionName = "AzureRunAsConnection"
 try
 {
     # Get the connection "AzureRunAsConnection "
     $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName         
    
     Connect-AzAccount `
         -ServicePrincipal `
         -TenantId $servicePrincipalConnection.TenantId `
         -ApplicationId $servicePrincipalConnection.ApplicationId `
         -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 
 }
 catch {
     if (!$servicePrincipalConnection)
     {
         $ErrorMessage = "Connection $connectionName not found."
         throw $ErrorMessage
     } else{
         Write-Error -Message $_.Exception
         throw $_.Exception
     }
 }
     
  $a = "------------------------------------------------------------------------------------------------------------------"            
  $vnetObjs = Get-AzVirtualNetwork 
  foreach ($vnetobj in $vnetObjs) {
      $resultvNet = "vNet Name: " + $vnetObj.Name
      $resultvNet
      $resultPeering = "Peering: " + (Get-AzVirtualNetworkPeering -Name "*" -VirtualNetwork $vnetObj.Name -ResourceGroupName $vnetobj.ResourceGroupName | Select-Object -ExpandProperty Name)
      $resultPeering
      if ($vnetobj.DhcpOptions.DnsServers){
          $resultDNS = "DNS Server: " + $vnetobj.DhcpOptions.DnsServers
          }
      else {
          $resultDNS = "DNS Server: Azure Default" 
          }
      $resultDNS
      $resultIPAddress = $vnetobj | Get-AzVirtualNetworkSubnetConfig | Select-Object Name,AddressPrefix
      $resultIPAddress
      $a 
      }    
      foreach ($user in $users){
     write $user
     $user | export.csv $FileName -NoTypeInformation -Append
 }
 $StorageURL = "https://test.blob.core.windows.net/testtesttestet01/"
 $FileName = "export.csv"
 $SASToken = "xx=xxxxx=xxxxx-xx-20T15:22:29Z&se=2022-07-01T23:22:29Z&sv=xxxx-xx-10&sr=c&sig=SwBLIW%2FEfmjeNjMUHtPsO6Ad2frC8egXNwEPvN6h8gY%3D"
    
 $blobUploadParams = @{
     URI = "{0}/{1}?{2}" -f $StorageURL, $FileName, $SASToken
     Method = "PUT"
     Headers = @{
         'x-ms-blob-type' = "BlockBlob"
         'x-ms-blob-content-disposition' = "attachment; filename=`"{0}`"" -f $FileName
         'x-ms-meta-m1' = 'v1'
         'x-ms-meta-m2' = 'v2'
     }
     Body = $Content
     Infile = $FileToUpload
 }
windows-server-powershellazure-automation
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

SathyamoorthyVijayakumar-MSFT avatar image
0 Votes"
SathyamoorthyVijayakumar-MSFT answered SathyamoorthyVijayakumar-MSFT edited

@DeHavenGraham-3555, After reviewing through your code - You were just missing a command to upload the file. I built below command on top your $blobUploadParams so that there is minimal code change at your end.


 Invoke-WebRequest -Uri $blobUploadParams.URI -Method $blobUploadParams.Method -Headers $blobUploadParams.Headers -InFile $blobUploadParams.Infile -UseBasicParsing


The complete code will look as below :

Note : I have also moved the $filename to high in code. $filename earlier was used without the initialilzation.

 $connectionName = "AzureRunAsConnection"
  try
  {
      # Get the connection "AzureRunAsConnection "
      $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName         
        
      Connect-AzAccount `
          -ServicePrincipal `
          -TenantId $servicePrincipalConnection.TenantId `
          -ApplicationId $servicePrincipalConnection.ApplicationId `
          -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 
  }
  catch {
      if (!$servicePrincipalConnection)
      {
          $ErrorMessage = "Connection $connectionName not found."
          throw $ErrorMessage
      } else{
          Write-Error -Message $_.Exception
          throw $_.Exception
      }
  }
         
   $a = "------------------------------------------------------------------------------------------------------------------" 
  $StorageURL = "https://test.blob.core.windows.net/testtesttestet01/"
      $FileName = "export.csv"
      $SASToken = "xx=xxxxx=xxxxx-xx-20T15:22:29Z&se=2022-07-01T23:22:29Z&sv=xxxx-xx-10&sr=c&sig=SwBLIW%2FEfmjeNjMUHtPsO6Ad2frC8egXNwEPvN6h8gY%3D"
           
   $vnetObjs = Get-AzVirtualNetwork 
   foreach ($vnetobj in $vnetObjs) {
       $resultvNet = "vNet Name: " + $vnetObj.Name
       $resultvNet
       $resultPeering = "Peering: " + (Get-AzVirtualNetworkPeering -Name "*" -VirtualNetwork $vnetObj.Name -ResourceGroupName $vnetobj.ResourceGroupName | Select-Object -ExpandProperty Name)
       $resultPeering
       if ($vnetobj.DhcpOptions.DnsServers){
           $resultDNS = "DNS Server: " + $vnetobj.DhcpOptions.DnsServers
           }
       else {
           $resultDNS = "DNS Server: Azure Default" 
           }
       $resultDNS
       $resultIPAddress = $vnetobj | Get-AzVirtualNetworkSubnetConfig | Select-Object Name,AddressPrefix
       $resultIPAddress
       $a 
       }    
       foreach ($user in $users){
      write $user
      $user | export-csv $FileName -NoTypeInformation -Append
  }
    
        
  $blobUploadParams = @{
      URI = "{0}/{1}?{2}" -f $StorageURL, $FileName, $SASToken
      Method = "PUT"
      Headers = @{
          'x-ms-blob-type' = "BlockBlob"
          'x-ms-blob-content-disposition' = "attachment; filename=`"{0}`"" -f $FileName
          'x-ms-meta-m1' = 'v1'
          'x-ms-meta-m2' = 'v2'
      }
      Body = $Content
      Infile = $FileToUpload
  }
 Invoke-WebRequest -Uri $blobUploadParams.URI -Method $blobUploadParams.Method -Headers $blobUploadParams.Headers -InFile $blobUploadParams.Infile -UseBasicParsing






5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.