Module Manifest – ScriptsToProcess


Here’s a nice little trick. When creating a PowerShell module you can specify a list of scripts to process when the module is loaded. You can use the scripts for validation or prerequisite checking.

Here’s a nice little example…

In the .psd1* file for the module, i.e. the manifest, add (or uncomment) a line to look like this.

*have a look at New-ModuleManifest for more information

 


# Script files (.ps1) that are run in the caller's environment prior to importing this module.
ScriptsToProcess = '.\Components\ScriptsToProcess\Check-Context.ps1'

 
I put the script(s) to be executed in a subfolder of the module itself. In this instance the path is .\Components\ScriptsToProcess\ and the script is Check-Context.ps1.

As I can’t use #requires -RunAsAdministrator with this module (it needs to support v3), I’ve put the following code into the Check-Context.ps1 script.
 


#Make sure we're running as admin 
if (!([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
     [Security.Principal.WindowsBuiltInRole] "Administrator"))
{
    
    Write-Output " "
    Write-Host -BackgroundColor Black -ForegroundColor Yellow "FUNKY PowerShell Module needs to be run as Administrator..."
    Write-Output " "

}

 
Now, look what happens when I try to import my module without running PowerShell as Administrator.
 
capture203
 

Comments (0)

Skip to main content