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!
][1]