MSGraphHelper.psm1
function Get-RequiredModules { param ( [string]$moduleName ) # Check if the module is installed $moduleInstalled = Get-Module -ListAvailable -Name $moduleName if (-not $moduleInstalled) { Write-Host "The required module '$moduleName' is not installed. Trying to install it." -ForegroundColor Yellow try { Install-Module -Name $moduleName -Force -Scope CurrentUser Write-Host "Successfully installed module '$moduleName'." -ForegroundColor Green } catch { Write-Error "Could not install module '$moduleName' due to error: $_" return } } else { Write-Host "'$moduleName' is installed on this machine" -ForegroundColor Green } # Check if the module is imported $moduleImported = Get-Module -Name $moduleName if (-not $moduleImported) { Write-Host "The required module '$moduleName' is not imported. Trying to import it." -ForegroundColor Yellow try { Import-Module -Name $moduleName Write-Host "Successfully imported module '$moduleName'." } catch { Write-Error "Could not import module '$moduleName' due to error: $_" } } else { Write-Host "'$moduleName' is imported on this machine" -ForegroundColor Green } } function Get-AccessTokenMSAL-ApplicationPermission { param ( [string]$clientId, [System.Security.SecureString]$clientSecret, [string]$tenantId ) #Checking if required Modules are installed $RequiredModulesInstalled = Get-RequiredModules -moduleName "MSAL.PS" $scope = "https://graph.microsoft.com/.default" $tokenResult = Get-MsalToken -ClientId $clientId -ClientSecret $clientSecret -TenantId $tenantId -Scopes $scope return $tokenResult.AccessToken } function Get-AccessTokenMSAL-DelegatedPermissions { param ( [string]$clientId, [string]$tenantId ) $RequiredModulesInstalled = Get-RequiredModules -moduleName "MSAL.PS" $scope = "https://graph.microsoft.com/.default" $tokenResult = Get-MsalToken -ClientId $clientId -TenantId $tenantId -Scopes $scope -RedirectUri "http://localhost" return $tokenResult } function New-LocalSecret { param ( [string]$UserName ) $credential = Get-Credential -UserName $UserName -Message "Enter your client secret" $credentialsBasePath = $env:APPDATA+"\MSALCredentialStore\" if (!(Test-Path -Path $credentialsBasePath)) { New-Item -Path $env:APPDATA -Name "MSALCredentialStore" -ItemType Directory | Out-Null } $credentialsExportFileName = "MSALCredentials.clixml" $credentialsPath = $credentialsBasePath + $credentialsExportFileName $credential | Export-Clixml -Path $credentialsPath -Force return $credentialsPath } function Get-LocalSecret { param ( [string]$PathToCLIXML ) # Retrieve the credential from the stored file $credential = Import-Clixml -Path $PathToCLIXML return $credential.Password } function Read-Calendar { param ( [string]$accessToken, [string]$fromUser, # User's email [DateTime]$startDate, # Start date for the calendar view [DateTime]$endDate # End date for the calendar view ) $graphApiEndpoint = "https://graph.microsoft.com/v1.0/users/$fromUser/calendarView?startDateTime=$($startDate.ToString('yyyy-MM-ddTHH:mm:ss'))&endDateTime=$($endDate.ToString('yyyy-MM-ddTHH:mm:ss'))&`$filter=showAs eq 'Oof'&`$select=subject,start,end,showAs" $headers = @{ Authorization = "Bearer $accessToken" "Content-Type" = "application/json" } $response = Invoke-RestMethod -Uri $graphApiEndpoint -Method Get -Headers $headers return $response.value } function Send-Email { param ( [string]$accessToken, [string]$recipientEmail, [string]$subject, [string]$body, [string]$fromUserIdOrUpn ) $graphApiEndpoint = "https://graph.microsoft.com/v1.0/users/$fromUserIdOrUpn/sendMail" $headers = @{ Authorization = "Bearer $accessToken" "Content-Type" = "application/json" } $emailData = @{ message = @{ subject = $subject body = @{ contentType = "Text" content = $body } toRecipients = @( @{ emailAddress = @{ address = $recipientEmail } } ) from = @{ emailAddress = @{ address = $fromUserIdOrUpn } } } } $emailJson = $emailData | ConvertTo-Json -Depth 100 Invoke-RestMethod -Uri $graphApiEndpoint -Method Post -Headers $headers -Body $emailJson -ContentType "application/json" } |