{"id":944,"date":"2021-03-06T19:15:46","date_gmt":"2021-03-06T17:15:46","guid":{"rendered":"https:\/\/www.flip-design.de\/?p=944"},"modified":"2021-03-06T19:15:46","modified_gmt":"2021-03-06T17:15:46","slug":"write-azure-automation-output-to-azure-blob-storage","status":"publish","type":"post","link":"https:\/\/www.flip-design.de\/?p=944","title":{"rendered":"Write Azure Automation Output to Azure Blob Storage"},"content":{"rendered":"\n<p>In my last Blog posts, I described how you can get data from the Power BI REST API and how you can execute them via Azure Automation. When you use the last one to execute your scripts, it is also nice to save the output &#x1f60a; Now I want to describe how you can write the output to the Azure Blob Storage. The following script exports the assigned Users to a Power BI Pro License \u2013 the same story from my last post. I have created before a Storage account and a container within.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span class=\"has-inline-color has-light-gray-color\">$connectionName = \"AzureRunAsConnection\"\r\n\r\ntry {\r\n    # Get the connection \"AzureRunAsConnection \"\r\n    $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName\r\n\r\n     \"Logging in to Azure...\"\r\n    Add-AzureRmAccount `\r\n    -ServicePrincipal `\r\n    -TenantId $servicePrincipalConnection.TenantId `\r\n    -ApplicationId $servicePrincipalConnection.ApplicationId `\r\n    -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint\r\n}\r\ncatch {\r\n    if (!$servicePrincipalConnection)\r\n    {\r\n        $ErrorMessage = \"Connection $connectionName not found.\"\r\n        throw $ErrorMessage\r\n    } else{\r\n        Write-Error -Message $_.Exception\r\n        throw $_.Exception\r\n    }\r\n}\r\n\r\n# the following user writes to the blob storage and needs also access to the AD\r\n$myPassword = '&lt;Password>'\r\n$myUsername = 'philipp@plenz.onmicrosoft.com'\r\n\r\n$password = ConvertTo-SecureString $myPassword -AsPlainText -Force\r\n$credential = New-Object System.Management.Automation.PSCredential ($myUsername, $password)\r\nConnect-AzureAD -Credential $credential\r\n\r\n$date = Get-Date -Format \"ddMMyyyy\"\r\n\r\n$PBIProServicePlanID = \"70d33638-9c74-4d01-bfd3-562de28bd4ba\"\r\n$ProLicenseFile = 'proLicense' + $date  + '.csv'\r\n\r\n$ADUsers = Get-AzureADUser -All $true | Select-Object ObjectId, ObjectType, DisplayName, UserPrincipalName, UserType, @{Name=\"Date Retrieved\";Expression={$RetrieveDate}}\r\n\r\n$OrgO365Licenses = Get-AzureADSubscribedSku | Select-Object SkuID, SkuPartNumber,CapabilityStatus, ConsumedUnits -ExpandProperty PrepaidUnits | `\r\n    Select-Object SkuID,SkuPartNumber,CapabilityStatus,ConsumedUnits,Enabled,Suspended,Warning, @{Name=\"Retrieve Date\";Expression={$RetrieveDate}} \r\n   \r\n$UserLicenseDetail = ForEach ($ADUser in $ADUsers)\r\n    {\r\n        $UserObjectID = $ADUser.ObjectId\r\n        $UPN = $ADUser.UserPrincipalName\r\n        $UserName = $ADUser.DisplayName\r\n        Get-AzureADUserLicenseDetail -ObjectId $UserObjectID -ErrorAction SilentlyContinue | `\r\n        Select-Object ObjectID, @{Name=\"User Name\";Expression={$UserName}},@{Name=\"UserPrincipalName\";Expression={$UPN} `\r\n        } -ExpandProperty ServicePlans\r\n    }\r\n$ProUsers = $UserLicenseDetail | Where-Object {$_.ServicePlanId -eq $PBIProServicePlanID}\r\n\r\nSet-AzureRmCurrentStorageAccount -StorageAccountName savedfiles -ResourceGroupName PBI\r\n$MSOPwd = ConvertTo-SecureString -String $myPassword -AsPlainText -Force\r\n$MSOCred = New-Object \u2013TypeName System.Management.Automation.PSCredential \u2013ArgumentList $myUsername, $MSOPwd\r\n\r\n# append the output to a CSV format\r\nwrite-output $ProUsers | Export-Csv savedfiles -Append\r\n# write the output to the blob storage\r\nSet-AzureStorageBlobContent -Container test -File savedfiles -Blob $ProLicenseFile\r\n\r\n<\/span><\/code><\/pre>\n\n\n\n<p>After running the script, the output is available at the container and can also used at Power BI with the Blob Connector.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.flip-design.de\/wp-content\/uploads\/2021\/03\/image-2.png\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"615\" src=\"https:\/\/www.flip-design.de\/wp-content\/uploads\/2021\/03\/image-2-1024x615.png\" alt=\"\" class=\"wp-image-945\" srcset=\"https:\/\/www.flip-design.de\/wp-content\/uploads\/2021\/03\/image-2-1024x615.png 1024w, https:\/\/www.flip-design.de\/wp-content\/uploads\/2021\/03\/image-2-300x180.png 300w, https:\/\/www.flip-design.de\/wp-content\/uploads\/2021\/03\/image-2-768x462.png 768w, https:\/\/www.flip-design.de\/wp-content\/uploads\/2021\/03\/image-2-1536x923.png 1536w, https:\/\/www.flip-design.de\/wp-content\/uploads\/2021\/03\/image-2-500x300.png 500w, https:\/\/www.flip-design.de\/wp-content\/uploads\/2021\/03\/image-2.png 1920w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In my last Blog posts, I described how you can get data from the Power BI REST API and how you can execute them via Azure Automation. When you use the last one to execute your scripts, it is also &hellip; <a href=\"https:\/\/www.flip-design.de\/?p=944\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.flip-design.de\/index.php?rest_route=\/wp\/v2\/posts\/944"}],"collection":[{"href":"https:\/\/www.flip-design.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.flip-design.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.flip-design.de\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.flip-design.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=944"}],"version-history":[{"count":1,"href":"https:\/\/www.flip-design.de\/index.php?rest_route=\/wp\/v2\/posts\/944\/revisions"}],"predecessor-version":[{"id":946,"href":"https:\/\/www.flip-design.de\/index.php?rest_route=\/wp\/v2\/posts\/944\/revisions\/946"}],"wp:attachment":[{"href":"https:\/\/www.flip-design.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=944"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.flip-design.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=944"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.flip-design.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=944"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}