At the moment I have a working ps1 code that exports the output to a csv locally but I need it to save to a onedrive location as the code needs to be saved and run in Azure function app. These are the steps I have taken so far.
Created a Function App with a unique name, correct location and subscription.
I then created a Function inside that "Function App" called TimeTrigger
Inside "TimeTrigger" I have added my code as a new file called AzureExport.ps1 and then added a line to the bottom of the run.ps1 file as shown below;
# Input bindings are passed in via param block. param($Timer) # Get the current universal time in the default string format. $currentUTCtime = (Get-Date).ToUniversalTime() # The 'IsPastDue' property is 'true' when the current function invocation is later than scheduled. if ($Timer.IsPastDue) { Write-Host "PowerShell timer is running late!" } # Write an information log with the current time. Write-Host "PowerShell timer trigger function ran! TIME: $currentUTCtime" # Execute the AzureLastSignInDateCode.ps1 script . ./AzureExport.ps1Then I went to "App registration" section of Azure and clicked on "New Registration"
Filled in all my information and noted down the Application (client) ID, Object ID and Directory (tenant) ID.
Finally I clicked on the "Certifactes & secrets", Clicked on "New client secret" gave it a name and then noted down the value and secret ID.
I then took all the IDs that I got and put them into this code that I found to output a token ID
# Parameters $tenantId = "tenantID" $client_id = "clientID" $client_secret = "client_Secret" $scope = "https://graph.microsoft.com/.default" $tokenUrl ="https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"
# Construct the body of the token request $body = @{ client_id = $client_id scope = $scope client_secret = $client_secret grant_type = "client_credentials" } # Send the token request $response = Invoke-RestMethod -Uri $tokenUrl -Method Post -Body$body
# Extract the access token from the response $accessToken = $response.access_token # Use the access token to make authenticated requests toMicrosoft Graph or other Azure services # For example: $headers = @{ "Authorization" = "Bearer $accessToken" "Content-Type" = "application/json" }
# Example: Get user profile from Microsoft Graph $userProfile = Invoke-RestMethod -Uri"https://graph.microsoft.com/v1.0/me" -Method Get -Headers $headers $userProfile
But that is where I got stuck as it through out an error
Invoke-RestMethod : {"error":"invalid_client","error_description":"AADSTS7000215: Invalid client secret provided. Ensure the secret being sent in the request is the client secret value, not the client secret ID, for a secret added to app 'd4eef1b3-cb06-4123-b188-78dfd1bd1b4d'. Trace ID: cc34903e-4bc3-4a5b-b7e3-3555edd4ac00 Correlation ID: ea2c349b-3b10-403d-bcf6-d62a07c31e0d Timestamp: 2024-02-15 08:55:54Z","error_codes":[7000215],"timestamp":"2024-02-15 08:55: 54Z","trace_id":"cc34903e-4bc3-4a5b-b7e3-3555edd4ac00","correlation_id":"ea2c349b-3b10-403d-bcf6-d62a07c31e0d","error_uri":" https://login.microsoftonline.com/error?code=7000215"} At line:17 char:13
- $response = Invoke-RestMethod -Uri $tokenUrl -Method Post -Body $body
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~- CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExceptio n
- FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand Invoke-RestMethod : The remote server returned an error: (400) Bad Request. At line:30 char:16
- ... erProfile = Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/ ...
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~- CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExceptio n
- FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
