Enable PSGallery Support

Image

Adding PowerShell Modules | PSGallery Support to WinPE

Thanks to David Segura for figuring this out and creating a module to update your WinPE WIM (see the link at bottom of the page). If you’re using his process, then you won’t need any of this. 🙂

Here we’re going to cover adding this ability without modifying your boot image, and enabling the PSGallery during WinPE in a task sequence. This will cover two different methods to enable PSGallery.

Basic Requirements:

  • Add Environment Variable LOCALAPPDATA
  • Add Modules: PowerShellGet & PackageManagement

Adding LOCALAPPDATA Variable

Using this method, the variable only lasts the session in which it was created, so you’d need to run this in each script you want to call the PSGallery.

[System.Environment]::SetEnvironmentVariable('LOCALAPPDATA',"$env:SystemDriveWindowssystem32configsystemprofileAppDataLocal")

Pulling Requirements from Internet

With this process, we have a single PowerShell script that will download the required modules from the internet and install them.

I grabbed the URLs to the nupkg files of the modules I wanted from the PowerShell Gallery, then using Invoke-WebRequest, I download them, extracted and moved them into place.

For a step-through, check out this blog post.


param (
    [string]$ModuleName
)

#Setup LOCALAPPDATA Variable
[System.Environment]::SetEnvironmentVariable('LOCALAPPDATA',"$env:SystemDriveWindowssystem32configsystemprofileAppDataLocal")

$WorkingDir = $env:TEMP

#PowerShellGet from PSGallery URL
if (!(Get-Module -Name PowerShellGet)){
    $PowerShellGetURL = "https://psg-prod-eastus.azureedge.net/packages/powershellget.2.2.5.nupkg"
    Invoke-WebRequest -UseBasicParsing -Uri $PowerShellGetURL -OutFile "$WorkingDirpowershellget.2.2.5.zip"
    $Null = New-Item -Path "$WorkingDir2.2.5" -ItemType Directory -Force
    Expand-Archive -Path "$WorkingDirpowershellget.2.2.5.zip" -DestinationPath "$WorkingDir2.2.5"
    $Null = New-Item -Path "$env:ProgramFilesWindowsPowerShellModulesPowerShellGet" -ItemType Directory -ErrorAction SilentlyContinue
    Move-Item -Path "$WorkingDir2.2.5" -Destination "$env:ProgramFilesWindowsPowerShellModulesPowerShellGet2.2.5"
    }

#PackageManagement from PSGallery URL
if (!(Get-Module -Name PackageManagement)){
    $PackageManagementURL = "https://psg-prod-eastus.azureedge.net/packages/packagemanagement.1.4.7.nupkg"
    Invoke-WebRequest -UseBasicParsing -Uri $PackageManagementURL -OutFile "$WorkingDirpackagemanagement.1.4.7.zip"
    $Null = New-Item -Path "$WorkingDir1.4.7" -ItemType Directory -Force
    Expand-Archive -Path "$WorkingDirpackagemanagement.1.4.7.zip" -DestinationPath "$WorkingDir1.4.7"
    $Null = New-Item -Path "$env:ProgramFilesWindowsPowerShellModulesPackageManagement" -ItemType Directory -ErrorAction SilentlyContinue
    Move-Item -Path "$WorkingDir1.4.7" -Destination "$env:ProgramFilesWindowsPowerShellModulesPackageManagement1.4.7"
    }

#Import PowerShellGet
Import-Module PowerShellGet


#Install Module from PSGallery
Install-Module -Name $ModuleName -Force -AcceptLicense -SkipPublisherCheck
Import-Module -Name $ModuleName -Force

Now the script leverages a parameter for you to pass it the name of a module you want to install.

PSGallery

Pulling Requirements from ConfigMgr Package

In this method, you’ve already downloaded the module nupkg files and placed into a Package. It’s essentially the same thing, but pulls from your own servers and gives you full control.

This script is very similar, but is kept in the package with the source, and copies the source from the package instead of the internet.

param (
    [string]$ModuleName
)
#Setup LOCALAPPDATA Variable
[System.Environment]::SetEnvironmentVariable('LOCALAPPDATA',"$env:SystemDriveWindowssystem32configsystemprofileAppDataLocal")

$WorkingDir = $env:TEMP

#PowerShellGet

if (!(Get-Module -Name PowerShellGet)){
    Copy-Item "$PSScriptRootpowershellget.2.2.5.nupkg" -Destination "$WorkingDirpowershellget.2.2.5.zip"
    Expand-Archive -Path "$WorkingDirpowershellget.2.2.5.zip" -DestinationPath "$WorkingDir2.2.5"
    $Null = New-Item -Path "$env:ProgramFilesWindowsPowerShellModulesPowerShellGet" -ItemType Directory -ErrorAction SilentlyContinue
    Move-Item -Path "$WorkingDir2.2.5" -Destination "$env:ProgramFilesWindowsPowerShellModulesPowerShellGet2.2.5"
    }


#PackageManagement
if (!(Get-Module -Name PackageManagement)){
    Copy-Item "$PSScriptRootpackagemanagement.1.4.7.nupkg" -Destination "$WorkingDirpackagemanagement.1.4.7.zip"
    Expand-Archive -Path "$WorkingDirpackagemanagement.1.4.7.zip" -DestinationPath "$WorkingDir1.4.7"
    $Null = New-Item -Path "$env:ProgramFilesWindowsPowerShellModulesPackageManagement" -ItemType Directory -ErrorAction SilentlyContinue
    Move-Item -Path "$WorkingDir1.4.7" -Destination "$env:ProgramFilesWindowsPowerShellModulesPackageManagement1.4.7"
    }

#Import PowerShellGet
Import-Module PowerShellGet


#Install Module from PSGallery
Install-Module -Name $ModuleName -Force -AcceptLicense -SkipPublisherCheck
Import-Module -Name $ModuleName -Force

PSGallery
PSGallery

In Action – YouTube Video

To see a demo of running these scripts, check out this YouTube Video:
PSGallery

Community Links

About Recast Software
1 in 3 organizations using Microsoft Configuration Manager rely on Right Click Tools to surface vulnerabilities and remediate quicker than ever before.
Download Free Tools
Request Pricing

Share this: