Why does a powershell script not run in sccm?

David 96 Reputation points
2021-08-19T16:52:10.82+00:00

If I run from an elevated command prompt
{query user | Select-String '^>(\w+)' | ForEach-Object { $_.Matches[0].Groups[1].Value }}
it returns the interactive logged on user (and not the elevated user account name).
But it fails in a powershell task sequence.
To test this I created a package (run as admin, allow user interaction) with a .bat script which runs these commands:
c:\Windows\ccmcache\3x>powershell -encodedcommand cQB1AGUAcgB5ACAAdQBzAGUAcgAgAHwAIABTAGUAbABlAGMAdAAtAFMAdAByAGkAbgBnACAAJwBeAD4AKABcAHcAKwApACcAIAB8ACAARgBvAHIARQBhAGMAaAAtAE8AYgBqAGUAYwB0ACAAewAgACQAXwAuAE0AYQB0AGMAaABlAHMAWwAwAF0ALgBHAHIAbwB1AHAAcwBbADEAXQAuAFYAYQBsAHUAZQAgAH0A
pause

but it fails with
query : The term 'query' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1

  • query user | Select-String '^>(\w+)' | ForEach-Object { $_.Matches[0] ...
  • ~~~~~
  • CategoryInfo : ObjectNotFound: (query:String) [], CommandNotFoundException
  • FullyQualifiedErrorId : CommandNotFoundException

I have also tried the same script (from ccmcache folder) as a system cmd session via PsExec64.exe -i -s cmd.exe and it works as expected
NB I want to use this in a task sequence to determine the interactive user account to create a scheduled task to runas the logged on user

Thanks
David

Microsoft Configuration Manager Application
Microsoft Configuration Manager Application
Microsoft Configuration Manager: An integrated solution for for managing large groups of personal computers and servers.Application: A computer program designed to carry out a specific task other than one relating to the operation of the computer itself, typically to be used by end users.
481 questions
0 comments No comments
{count} votes

Accepted answer
  1. David 96 Reputation points
    2021-08-20T07:48:46.85+00:00

    It turned out to be the weird 32/64 bit redirection that sccm / windows does.
    So this solution worked:
    %winDir%\Sysnative\windowsPowershell\v1.0\Powershell.exe
    https://configmonkey.wordpress.com/2015/08/04/sccm-2012-powershell-commands-not-executing-exit-code-1/


2 additional answers

Sort by: Most helpful
  1. Jason Sandys 31,286 Reputation points Microsoft Employee
    2021-08-20T02:49:38.18+00:00

    Is this an OSD task sequence or a non-OSD TS?

    Have you tested your script running as the local System account (which is what task sequence run as)?

    Also, there is no alias in PowerShell named "query" so no sure what your script is doing in the first place. Can you provide the actual full, unencoded script?


  2. Jason Sandys 31,286 Reputation points Microsoft Employee
    2021-08-20T15:54:11.333+00:00

    Ok, I see, query is not an alias at all, but an actual Windows exe -- I honestly have never used or seen that before. For clarity, I strongly recommend that you use query.exe so that it's obvious what that is.

    As for the issue, 32-bit file redirection isn't really weird at all. See https://video2.skills-academy.com/en-us/windows/win32/winprog64/file-system-redirector for lots of details.

    Because packages in ConfigMgr are run in a 32-bit context in ConfigMgr, anything in c:\Windows\system32 is not directly visible to them but instead they are silently redirected to c:\Windows\syswow64. query.exe does not exists c:\windows\syswow64 and so can't be found. That's where the sysnative alias comes in as described in the documentation I linked to above.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.