When running a PSU script as a job. The scripts environment is powershell 7 and PS version is 7.5.1
An error occurs when running
$allGPOs = Get-GPO -All
$unlinkedGPOs = @()
$allGPOs | Foreach-Object -Parallel {$GUID = $.Id; $Report = Get-GPOReport -GUID $GUID.GUID -ReportType XML | Select-String -NotMatch “”; If ($Null -NE $Report){ $unlinkedGPOs += $}} -ThrottleLimit 5
The server does not support the requested critical extension. (Exception from HRESULT: 0x8007202C)
Does PSU not support the parallel flag and only running scripts as jobs?
@Philpsu check this doc: https://docs.powershelluniversal.com/platform/variables#foreach-object-parallel
I’ve not personally done it, just it says you have to use the $using:
keyword.
Google provided the following AI response:
$objects | ForEach-Object -Parallel {
$internalVariable = $using:externalVariable
# Perform operations on $_ in parallel
} -ThrottleLimit 16
I did try that out and looking at the documentation it appears that is based off using secrets. I got an error of “The value of the using variable ‘$using:externalVariable’ cannot be retrieved because it has not been set in the local session.”, which would make sense as no such thing exist and the variable in the code would be $_. Thank you for the suggestion but I am still stuck.
Each instance run in parallel has its own memory. We have to load custom functions on-demand using parallel foreach. We also have to load 3rd party modules on-demand. For example, if you have custom functions, save them in a script file (I.e.MyCustomFunctions.ps1) in PSU. Within the code block for the parallel foreach, load the script file (I.e. . C:\ProgramData\UniversalAutomation\Repository\MyCustomFunctions.ps1). Note there is a dot in front of the path to load the functions, not run the script file. We don’t use the PSU variables (I.e. $Repository) because they don’t exist in a foreach parallel instance.
For modules, something like this in parallel foreach code block:
Define the name of the module you want to check
$moduleName = “YourModuleName”
Check if the module is already loaded
if (-not (Get-Module -Name $moduleName -ListAvailable)) {
# If the module is not loaded, try to import it
try {
Import-Module -Name $moduleName -ErrorAction Stop
}
catch {
throw “Failed to load module $moduleName: $($Error[0].Message)”
}
}
Works great and performs well using these techniques.