This is a nice way to handle accessing different versions of PNPPowershell.
I use this when switching between onpremise environments of different version and online.
Note that env-variables can't be used until session, like ISE is restarted. Otherwise can variable be accessed immediatly using
[System.Environment]::GetEnvironmentVariable('SharePointPnPPowerShell2019','machine')
setupEnv.ps1 downloads named modules in $array to $saveFolder.
Then locates psd1-file and adds this as a enviroment variable with name in arrayobject.
To use the module access env-variable with $env:name as showed in importModule_Example.ps1
import-module $env:SharePointPnPPowerShell2019 -DisableNameChecking | |
Remove-Module -Name SharePointPnPPowerShell2019 | |
Import-Module $env:sharepointpnppowershell2016 -DisableNameChecking | |
Remove-Module -Name SharePointPnPPowerShell2016 | |
Import-Module $env:SharePointPnPPowerShellOnline -DisableNameChecking | |
Remove-Module -Name SharePointPnPPowerShellOnline |
$saveFolder = "C:\pnpPowershell" | |
$array = @() | |
$array += [pscustomobject]@{Name="sharepointpnppowershell2019";path=""} | |
$array += [pscustomobject]@{Name="SharePointPnPPowerShellOnline";path=""} | |
$array += [pscustomobject]@{Name="sharepointpnppowershell2016";path=""} | |
if ((test-path -PathType Container -Path $saveFolder) -eq $false) {New-Item -ItemType Directory -Force -Path $saveFolder |out-null} | |
foreach ($item in $array) { | |
save-module -name $($item.name) -path $saveFolder -Force | |
} | |
foreach ($item in $array) { | |
$item.path = Get-ChildItem -Path $saveFolder -Recurse |where-object {$_.Name -like "$($item.Name).psd1"} |select -ExpandProperty FullName | |
if (![string]::IsNullOrEmpty($item.path)) { | |
[System.Environment]::SetEnvironmentVariable($item.name, $item.path,[System.EnvironmentVariableTarget]::Machine) | |
} | |
} |
References:
https://www.erwinmcm.com/running-the-various-versions-of-pnp-powershell-side-by-side/
No comments:
Post a Comment