Product Versions
- PowerShell Universal 3.x
- PowerShell Universal 4.0.x
- PowerShell Universal 4.1.x
Purpose
When making changes in PowerShell Universal, the configuration system will detect changes to files through the management API, admin console and on disk. In certain circumstances, it may be necessary to disable the local file system watcher. When this is done, changes made on disk will not automatically reload. This document outlines code that can be used to manually refresh a configuration file.
PowerShell Universal v4.2 and later have APIs available to do this directly.
Workaround
Within a script, API or dashboard running within the Integrated environment, you can access the internal services of PowerShell Universal to allow for reloading specific configuration files. This example reloads dashboards.
$ConfigService = $UniversalClient.GetType().GetProperty("_configService", [System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic).GetValue($UniversalClient)
$SyncAsync = $ConfigService.GetType().GetMethod("SyncAsync", [Type[]]@())
$Method = $SyncAsync.MakeGenericMethod([PowerShellUniversal.Dashboard])
$Method.Invoke($ConfigService, $null).GetAwaiter().GetResult()
To reload variables, you could use the following.
$ConfigService = $UniversalClient.GetType().GetProperty("_configService", [System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic).GetValue($UniversalClient)
$SyncAsync = $ConfigService.GetType().GetMethod("SyncAsync", [Type[]]@())
$Method = $SyncAsync.MakeGenericMethod([PowerShellUniversal.Variable])
$Method.Invoke($ConfigService, $null).GetAwaiter().GetResult()
In PowerShell Universal v4.x and later, you can take advantage of new features of PowerShell 7.3 to simplify these calls.
$ConfigService = $UniversalClient.GetType().GetProperty("_configService", [System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic).GetValue($UniversalClient)
$ConfigService.SyncAsync[PowerShellUniversal.Variable]().GetAwaiter().GetResult()