Share via

Azure Functions with Powershell

khouloud Belhaj 91 Reputation points
2021-06-29T08:19:23.88+00:00

Hello ,
I'm looking to redo this code in an azure function :

   [CmdletBinding()]  
   param (  
       [Parameter()]  
       [uint32]  
       $daysCount,  
       [Parameter()]  
       [string]  
       $filesDirectory  
   )  

   function Use-Module ($m) {  

       #   
   }  

   #install module if not exists  
   Use-Module("MicrosoftPowerBIMgmt")  

   #configure proxy  
   $webclient=New-Object System.Net.WebClient  
   $webclient.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials  
   [Net.ServicePointManager]::SecurityProtocol = "tls12"  

   #login to power bi account  
   $password = ""  
   $username = ""  
   $credential = New-Object System.Management.Automation.PSCredential($username, $password)  
   Connect-PowerBIServiceAccount -Credential $credential  
   function createIfNotExists($directory)  
   {  
       if(![system.io.directory]::Exists($directory))   
       {  
           [system.io.directory]::CreateDirectory($directory)  
       }  
   }  
   #if filesPath empty then set the same output directory where script located  
   if(!$filesDirectory)  
   {  
   $filesDirectory = "./"  
   }  
   else {  
       createIfNotExists($filesDirectory)  
   }  
   for($i=0;$i -lt $daysCount;$i++)  
   {  

   $activitiesDate = [System.DateTime]::Now.Date.AddDays(-$i)  
   $dateTimeStart = $activitiesDate.Date.ToString("s")  
   $dateTimeEnd =  $activitiesDate.Date.AddHours(23).AddMinutes(59).AddSeconds(59).ToString("s")  

   $fileName = $filesDirectory+'ActivitiesLog_'+$activitiesDate.Date.ToString("dd-MM-yyyy")+".csv"  
   #ConvertFrom-Json |  
   $activities = Get-PowerBIActivityEvent -StartDateTime $dateTimeStart -EndDateTime: $dateTimeEnd | ConvertFrom-Json  
   $psObjectForCsv = $activities | ForEach-Object {  
       [PSCustomObject]@{  
           "id"=$_.Id  
           "RecordType" = $_.RecordType  
           "CreationTime" = $_.CreationTime  

       }  
   }  
   $psObjectForCsv | Export-Csv -path $fileName -Force  
   }  

The purpose of the code is to connect to the Power BI Service and retrieve the logs. I used a service principal to connect to Power BI Service. The content will be saved in a Blob Storage. As you can see, the name of the Blob files is random.
While I Have to get dynamic names(Blob Files) according to the date.

This is my code in the Azure function:

   using namespace System.Net  

   # Input bindings are passed in via param block.  
   param($Request, $TriggerMetadata)  

   #Initialisation des paramétres d'identification pour le service principale  
   $appId = $env:APP_ID  
   $tenantId = $env:APP_TENANT_ID  
   $secret = $env:APP_SECRET  
   $password = ConvertTo-SecureString $secret -AsPlainText -Force  
   $Cred = New-Object System.Management.Automation.PSCredential ($appId, $password)  
   #Write-Output $Cred  
   [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12  
   #configure proxy  
   $webclient=New-Object System.Net.WebClient  
   $webclient.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials  
   #Connexion au service POWER BI  
   Connect-PowerBIServiceAccount -ServicePrincipal -Tenant $tenantId -Credential $Cred  
   $activitiesDate = [System.DateTime]::Now.Date.AddDays(-4)  
   $dateTimeStart = $activitiesDate.Date.ToString("s")  
   $dateTimeEnd =  $activitiesDate.Date.AddHours(23).AddMinutes(59).AddSeconds(59).ToString("s")  
   Write-Host $activitiesDate  
   Write-Host $dateTimeStart  
   Write-Host $dateTimeEnd  
   #activities = Get-PowerBIActivityEvent -StartDateTime $dateTimeStart -EndDateTime: $dateTimeEnd | ConvertFrom-Json  
   #Récuperation des données logs  
   $activities = Get-PowerBIActivityEvent -StartDateTime $dateTimeStart -EndDateTime $dateTimeEnd | ConvertFrom-Json  
   Write-Host $activities  
   $psObjectForCsv = $activities | ForEach-Object {  
       [PSCustomObject]@{  
           "id"=$_.Id  
           "RecordType" = $_.RecordType  
           "CreationTime" = $_.CreationTime  

   }  

   Push-OutputBinding -Name OutputBlob -Value $psObjectForCsv  

And this is the container where my files are saved.

I'm a beginner with Azure Function, so any help will be appreciated!

![110139-image.png]1

Azure Functions
Azure Functions

An Azure service that provides an event-driven serverless compute platform.

Windows for business | Windows Server | User experience | PowerShell
Microsoft Security | Microsoft Entra | Microsoft Entra ID
0 comments No comments

1 answer

Sort by: Most helpful
  1. Pramod Valavala 20,661 Reputation points Microsoft Employee Moderator
    2021-06-30T15:49:23.14+00:00

    @khouloud Belhaj Unfortunately, this isn't possible. You can use binding expressions that can use metadata from the trigger payload or even put in the current time but not your exact use case.

    The workaround here would be to use the Az PowerShell module to create the blob file as required.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.