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/
Why does a powershell script not run in sccm?
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
-
David 96 Reputation points
2021-08-20T07:48:46.85+00:00
2 additional answers
Sort by: Most helpful
-
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?
-
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.