0

I've got code that has been extensively tested across multiple PowerShell sessions, multiple machines, etc all doing exactly what I expect.

switch ($e.Data.Type) `
{
    {$_ -in [Policy.Reg]::SZ, [Policy.Reg]::EXPAND_SZ} `
        {
            $e.Data[$e.Data.Type] = ([string]$e.Data[$e.Data.Type]) -replace "{PC}", "$PC"
        } 
    {$_ -in [Policy.Reg]::MULTI_SZ} `
        {
            $e.Data[$e.Data.Type] = [string[]]($e.Data[$e.Data.Type] | % { $_ -replace "{PC}", "$PC" })
        }
}

[Policy.Reg] is an Enum that $e.Data.Type is a variable of that enum

Running the code from the ISE or the Powershell command line code works every time.

When I add it as a RunPowerShell task sequence step (MDT 2012/2013 and SCCM 2012 all tried) the task sequence fails with

    you must provide a value expression on the right-hand side of the '-' operator. [Policy.Reg]::SZ, [Policy.Reg]::EXPAND_SZ

I'm at a complete loss what is going on when a TaskSequence runs a Powershell script that makes it different than when I manually execute the some code.

halfer
  • 19,824
  • 17
  • 99
  • 186
TofuBug
  • 573
  • 1
  • 6
  • 22
  • It sounds as if a variable is not being expanded, you could try re-running your task but try writing a file to confirm your variables expand correctly. I've ran into a situation where I had to use `$script = [ScriptBlock]::Create("This expands as expected on $date")` then pass `$script` as a `ScriptBlock` – user4317867 Jun 04 '16 at 00:32
  • What is `[Policy.Reg]`? I see no technet documentation for that name nor is it known on my computer running WMF5. – alx9r Jun 24 '16 at 20:15
  • @alx9r [Policy.Reg] is a custom Enum that is part of custom C# library that is being used in a Binary Powershell module. – TofuBug Jun 27 '16 at 17:29
  • Have you confirmed that that custom C# library available and loaded in the task sequence? – alx9r Jun 27 '16 at 17:31

1 Answers1

0

So really quick it seems my issue was with MDT itself.

The standard wsf and vbs files that control PowerShell Execution check for the existence in the registry for the currently installed version and then runs one of two executables that sets up a unique PowerShell environment.

One of the Exes just runs the most recent version of powershell.

The other forces it to run in powershell 2.0 (for compatibility i'm guessing)

Here's the issue the LOGIC in the script file is all wrong

Instead of being something like

if (PSVersion  >= 2.0) then
    Run newest PowerShell
else
    Run PowerShell 2.0
end if

It looks like

if (PSVersion = 3.0 then
    Run newest Powershell
else
    Run Powershell 2.0
end if

So since all my images have PowerShell 4.0 installed on them, well as LOGIC dictates 4.0 is NOT equal to 3.0 so off to PowerShell 2.0 we go and toss out ALL the things we have gotten used to having since we spent a LONG time ensuring every system in our environment is at least on PowerShell 4.0

So in the end a little code change to the code Launching the PowerShell Scripts fixed the issue.

TofuBug
  • 573
  • 1
  • 6
  • 22