question

khouloudBelhaj-1789 avatar image
0 Votes"
khouloudBelhaj-1789 asked khouloudBelhaj-1789 commented

Azure Functions with Powershell

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-active-directorywindows-server-powershellazure-functionsazure-ad-app-registration
image.png (84.3 KiB)
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

PramodValavala-MSFT avatar image
0 Votes"
PramodValavala-MSFT answered khouloudBelhaj-1789 commented

@khouloudBelhaj-1789 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.


· 3
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.

@PramodValavala-MSFT : Thank you for your answer , well I found this way : name of the file_{datetime(date)} in the path of the blob storage and it worked perfectly for me.

0 Votes 0 ·

@PramodValavala-MSFT
Thank you for the follow up on this and I'm glad that you were able to resolve your issue!

If you have any other questions, please let me know.
Thank you for your time and patience throughout this issue.


Please remember to "Accept Answer" if any answer/reply helped, so that others in the community facing similar issues can easily find the solution.

0 Votes 0 ·

Thank you so much ! I'm newly starting with azure and for my project of end of study. I had to work with the Azure environment to migrate from a solution in SQL server to azure data factory. Well, I figured out that they are many interesting notions to learn, and I'm so happy that I'm going through this experience!

0 Votes 0 ·