Understanding the PowerShell Script — Part 2
Welcome to the SharePoint Archiving Solution Series:
This blog is divided into multiple sections, each focusing on a crucial aspect of the migration process. Below are links to navigate directly to each section:
- Introduction
- Pre-requisites
- Application Registration and API Permissions
- PowerShell Script Workflow
- Understanding the PowerShell Script — Part 1
- Understanding the PowerShell Script — Part 2
- Understanding the PowerShell Script — Part 3
- Get Full Script from GitHub
start_exe.ps1: This is the main script file that you will execute. It contains the core logic for transferring files from SharePoint to OneDrive. It will call functions from the AccessToken.psm1
module and use settings from the setting.json
file.
Script Breakdown
Set the global error action preference to stop on errors.
$ErrorActionPreference = "Stop"
This line sets the global error action preference to “Stop,” meaning that the script will stop executing when an error occurs. This is useful for debugging and ensuring that issues are addressed immediately.
Import Modules and Settings
Import-Module .\Modules\AccessToken.psm1
$importSettings = Get-Content "setting.json" -Raw
- The
Import-Module
cmdlet imports theAccessToken.psm1
module, making its functions available for use. Get-Content
reads thesetting.json
file as a raw string and assigns it to$importSettings
.
$setting = $importSettings| ConvertFrom-Json
$clientId = $setting.ClientID
$clientSecret = $setting.ClientSecret
$tenantID = $setting.TenantID
$oneDriveUPN = $setting.OneDriveUPN
$siteIDs = $setting.SiteIDs
$modifiedDate = $setting.ModifiedDate
ConvertFrom-Json
converts the raw JSON string into a PowerShell object stored in$setting
.- The script then assigns specific values from
$setting
to individual variables for easy access.
Get Access Code
$accessToken = Get-AccessToken -ClientID $clientId -ClientSecret $clientSecret -TenantID $tenantID
The Get-AccessToken
function is called with the ClientID
, ClientSecret
, and TenantID
parameters, retrieving the access token and storing it in $accessToken
.
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer $accessToken")
$headers.Add('Prefer', '')
- A new dictionary object
$headers
is created to store HTTP headers. - The
Authorization
header is added with the bearer token for authentication. - An empty
Prefer
header is added, for future use.
Refresh Token Before Expire
By default, the access token will expire in 1 hour. To prevent the script from stopping due to token expiration, a timer will be triggered to obtain a new access token before the current one expires.
$timer = [System.Timers.Timer]::new(3400000)
A timer is created with an interval of 3,400,000 milliseconds (approximately 56.67 minutes). This ensures that the token is refreshed before the default expiration time of one hour.
$refreshToken = Register-ObjectEvent -InputObject $timer -EventName Elapsed -Action {
$accessToken = Get-AccessToken -ClientID $clientId -ClientSecret $clientSecret -TenantID $tenantID
$headers.Authorization = "Bearer $accessToken"
Write-Output "******* New Token Issued *********"
}
Register-ObjectEvent
registers an event handler for the timer'sElapsed
event.- The event handler refreshes the access token by calling
Get-AccessToken
again and updates theAuthorization
header with the new token. - A message is written to the output to indicate that a new token has been issued.
$refreshToken
This line registers the event handler created above.
$timer.Start()
The timer is started, ensuring that the token refresh logic will be triggered at the specified interval.
This section of the script sets up the environment by importing necessary modules, reading settings, obtaining an access token, and setting up a timer to refresh the token before it expires. The error handling and modular approach make the script robust and maintainable.