Understanding the PowerShell Script — Part 2

Ryan Shrestha
3 min readJul 8, 2024

--

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:

  1. Introduction
  2. Pre-requisites
  3. Application Registration and API Permissions
  4. PowerShell Script Workflow
  5. Understanding the PowerShell Script — Part 1
  6. Understanding the PowerShell Script — Part 2
  7. Understanding the PowerShell Script — Part 3
  8. 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 the AccessToken.psm1 module, making its functions available for use.
  • Get-Content reads the setting.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's Elapsed event.
  • The event handler refreshes the access token by calling Get-AccessToken again and updates the Authorization 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.

< Previous 1 2 3 4 5 6 7Next >

--

--

Ryan Shrestha
0 Followers

Diving into the ocean of Communicational Technologies.