lazyExchangeAdmin.psm1
Function New-lazyGraphAPIToken { <# .SYNOPSIS Acquire authentication token for MS Graph API .DESCRIPTION If you have a registered app in Azure AD, this function can help you get the authentication token from the MS Graph API endpoint. Each token is valid for 60 minutes. .PARAMETER ClientID This is the registered ClientID in AzureAD .PARAMETER ClientSecret This is the key of the registered app in AzureAD .PARAMETER TenantID This is your Office 365 Tenant Domain .EXAMPLE $graphToken = New-MSGraphAPIToken -ClientID <ClientID> -ClientSecret <ClientSecret> -TenantID <TenantID> The above example gets a new token using the ClientID, ClientSecret and TenantID combination .NOTES General notes #> param( [parameter(mandatory=$true)] [string]$ClientID, [parameter(mandatory=$true)] [string]$ClientSecret, [parameter(mandatory=$true)] [string]$TenantID ) $body = @{grant_type="client_credentials";scope="https://graph.microsoft.com/.default";client_id=$ClientID;client_secret=$ClientSecret} $oauth = Invoke-RestMethod -Method Post -Uri https://login.microsoftonline.com/$TenantID/oauth2/v2.0/token -Body $body $token = @{'Authorization'="$($oauth.token_type) $($oauth.access_token)"} Return $token } Function New-lazyOutlookAPIToken { <# .SYNOPSIS Acquire authentication token for Outlook REST API .DESCRIPTION If you have a registered app in Azure AD, this function can help you get the authentication token from the Outlook REST API endpoint. Each token is valid for 60 minutes. .PARAMETER ClientID This is the registered ClientID in AzureAD .PARAMETER ClientSecret This is the key of the registered app in AzureAD .PARAMETER TenantID This is your Office 365 TenantID .EXAMPLE $graphToken = New-OutlookRestAPIToken -ClientID <ClientID> -ClientSecret <ClientSecret> -TenantID <TenantID> The above example gets a new token using the ClientID, ClientSecret and TenantID combination .NOTES General notes #> param( [parameter(mandatory=$true)] [string]$ClientID, [parameter(mandatory=$true)] [string]$ClientSecret, [parameter(mandatory=$true)] [string]$TenantID ) $body = @{grant_type="client_credentials";scope="https://outlook.office.com/.default";client_id=$ClientID;client_secret=$ClientSecret} $oauth = Invoke-RestMethod -Method Post -Uri https://login.microsoftonline.com/$TenantID/oauth2/v2.0/token -Body $body $token = @{'Authorization'="$($oauth.token_type) $($oauth.access_token)"} Return $token } Function New-lazyExchangeOnlineSession { [CmdletBinding()] param( [parameter(mandatory=$true,position=0)] [PSCredential] $Credential ) Remove-PSSession -Name "ExchangeOnline" -Confirm:$false -ErrorAction SilentlyContinue $EXOSession = New-PSSession -Name "ExchangeOnline" -ConfigurationName "Microsoft.Exchange" -ConnectionUri 'https://ps.outlook.com/powershell' -Credential $Credential -Authentication Basic -AllowRedirection -WarningAction SilentlyContinue Import-PSSession $EXOSession -AllowClobber -DisableNameChecking -Prefix Ol | Out-Null } #Function to connect to Exchange OnPrem Shell Function New-lazyExchangeOnPremSession() { [CmdletBinding()] param( [parameter(mandatory=$true,position=0)] [string] $exchangeServer ) Remove-PSSession -Name "ExchangeOnPrem" -Confirm:$false -ErrorAction SilentlyContinue $EXSession = New-PSSession -Name "ExchangeOnPrem" -ConfigurationName Microsoft.Exchange -ConnectionUri "http://$($exchangeServer)/PowerShell/" -Authentication Kerberos Import-PSSession $EXSession -AllowClobber -DisableNameChecking -Prefix Op | out-null } #Function to compress file (ps 4.0) Function New-lazyZipFile { [CmdletBinding()] param ( [Parameter(Mandatory=$true,position=0)] [string]$fileToZip, [Parameter(Mandatory=$true,position=1)] [string]$destinationZip ) Add-Type -assembly System.IO.Compression Add-Type -assembly System.IO.Compression.FileSystem [System.IO.Compression.ZipArchive]$outZipFile = [System.IO.Compression.ZipFile]::Open($destinationZip, ([System.IO.Compression.ZipArchiveMode]::Create)) [System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($outZipFile, $fileToZip, (Split-Path $fileToZip -Leaf)) | out-null $outZipFile.Dispose() } #Function to delete old files based on age Function Invoke-lazyHousekeeping { [CmdletBinding()] param ( [Parameter(Mandatory=$true,position=0)] [string]$folderPath, [Parameter(Mandatory=$true,position=1)] [int]$daysToKeep, [Parameter()] [switch]$Recurse ) $datetoDelete = (Get-Date).AddDays(-$daysToKeep) if ($Recurse) { $filesToDelete = Get-ChildItem $FolderPath -Recurse | Where-Object { $_.LastWriteTime -lt $datetoDelete } } else { $filesToDelete = Get-ChildItem $FolderPath | Where-Object { $_.LastWriteTime -lt $datetoDelete } } if (($filesToDelete.Count) -gt 0) { foreach ($file in $filesToDelete) { Write-Host "$($file.FullName)" Remove-Item -Path ($file.FullName) -Force -ErrorAction SilentlyContinue } } } #Function to Stop Transaction Logging Function Stop-lazyTxnLogging { $txnLog="" Do { try { Stop-Transcript | Out-Null } catch [System.InvalidOperationException] { $txnLog="stopped" } } While ($txnLog -ne "stopped") } #Function to Start Transaction Logging Function Start-lazyTxnLogging { param ( [Parameter(Mandatory=$true,Position=0)] [string]$logDirectory ) Stop-TxnLogging Start-Transcript $logDirectory -Append } #Function to get Script Version and ProjectURI for PS vesions below 5.1 Function Get-lazyScriptInfo { param ( [Parameter(Mandatory=$true,Position=0)] [string]$Path ) $scriptFile = Get-Content $Path $props = @{ Version = "" ProjectURI = "" } $scriptInfo = New-Object PSObject -Property $props # Get Version foreach ($line in $scriptFile) { if ($line -like ".VERSION*") { $scriptInfo.Version = $line.Split(" ")[1] BREAK } } # Get ProjectURI foreach ($line in $scriptFile) { if ($line -like ".PROJECTURI*") { $scriptInfo.ProjectURI = $line.Split(" ")[1] BREAK } } Remove-Variable scriptFile Return $scriptInfo } #Get timezone information for PSv4 and below Function Get-lazyTimeZone { Return [System.TimeZoneInfo]::Local } |