5

I'm having some trouble with a .bat->.ps1 call in my build process after moving from Win7 to Win10 (1607 LTSB) - the call to PowerShell, that took less than 100msec on Win7 now takes 10sec+ on Win10 on certain hosts :-(

The call looks like this: powershell -ExecutionPolicy Bypass -NonInteractive %scriptfile%

Startup-performance of ISE and PowerShell.exe from start-menu is good.

I wasn't able to dig up anything helpful so far...

  • PoSh 5.1 on both, Win7 and Win10, PSProfile is clean apart from a couple of aliases
  • $env:PATH is "clean" apart from a couple of folders that have been added on purpose - removing those didn't improve the situation.
  • already did "ngen update" - problem still occurs.

I've create a little helper script that measures startup-performance of PowerShell from different starting points: https://gist.github.com/mwallner/d3c86794bb74680b0c0cf4e9a9758ab4 no luck with this either, on Win7 and most of my Win10 machines startup-time is way below 1sec.

anybody here who has already solved this riddle?

mwallner
  • 985
  • 11
  • 23
  • Maybe you get lucky with ETW: https://randomascii.wordpress.com/2015/09/24/etw-central/ – Martin Ba May 27 '18 at 18:44
  • Have you tried -noprofile? Has the account got a network based "homedrive"? Why does it matter if the process takes 10 secs vs 100ms? – Adam Parsons May 27 '18 at 22:54
  • @AdamParsons yes -noprofile doens't change a thing in this scenario + it's in my test cases (see the script I've included) / and it matters because it's not called once, but more like 50 times - and that part cannot be changed easily. – mwallner May 28 '18 at 04:49
  • Are you certain it's the PowerShell call? I've observed Task Sequence steps taking noticeably longer than their executions for no seemingly obvious reason, not limited to PowerShell. – Adam Parsons May 28 '18 at 05:17
  • And what about the WinPE boot image's components? What does it have in it compared to the old one (are you using a newer WinPE?). – Adam Parsons May 28 '18 at 05:24
  • Is there anything in the SMSTS log that speaks to the timing issues? – Adam Parsons May 28 '18 at 05:26
  • @AdamParsons - the problem occurs on Win10 LTSB 1607 - I'm pretty certain it is the call to powershell.exe - will do some further investigation today - I've got a tip that the startup of powershell.exe from a certain context (.bat / .vbs) might be related to the antimalware service executable – mwallner May 28 '18 at 06:53
  • Classic AV. I do think certain AV's have hissy fits over powershell being called from .bat - if it is an sccm task sequence you can call PS directly if the .bat files are just wrappers. – Adam Parsons May 28 '18 at 06:58
  • is there a solution to it? as I am facing the issue as well. wonder if it's powershell version which has the bug?! – Princa May 10 '19 at 02:46
  • @Princa - read the answer below, for me it definitely had to do with the UAC - see if you notice a speedup when you start the script as admin or even disable UAC completely (although not recommended) – mwallner May 11 '19 at 18:40

2 Answers2

5

Solved! - it's been UAC all along :-(

Solution: disable UAC for the user that's running the scriots or make sure the first script that calls the others is being run as administrator / elevated.

Not sure why UAC is causing these delays, and I'm certain there are cases where this is not an option - but for me this is solved by ensuring the first script is elevated + disabling UAC for dev boxes.

mwallner
  • 985
  • 11
  • 23
4

I had a similar problem - scripts taking multiple seconds to start.

The problem turned out to be a combination of two factors:

  • I had ExecutionPolicy force-set to RemoteSigned via GPO (run Get-ExecutionPolicy -List and check the value for MachinePolicy)
  • I had quite a few processes running, so that enumerating them was taking a long time

...so it was fixed by a restart. (I think overriding the GPO-set value would also work, but I didn't try it.)


What didn't work:

I noticed that simply running PowerShell was relatively fast, only script execution (via command-line parameters or the & / Call operator) was slow. For instance I could run the script by piping it to powershell's stdin, which didn't incur the slowdown:

type "script.ps1" | powershell.exe -noprofile -nologo -executionpolicy Bypass -file -

At this point I tried troubleshooting with ProcMon, which didn't show any long calls.

Next I tried opening Process Explorer to check the stack of the powershell.exe process while it was loading the script. The top of the stack was:

ntdll.dll!RtlGetNativeSystemInformation+0x14
KERNEL32.DLL!lstrcmpA+0x12d
KERNEL32.DLL!CreateToolhelp32Snapshot+0x108
[Native Frame: IL Method without Metadata]
[Managed to Unmanaged Transition]
System.Management.Automation.dll!System.Management.Automation.PsUtils.GetParentProcess+0x73
System.Management.Automation.dll!System.Management.Automation.Internal.SecuritySupport.GetExecutionPolicy+0x138

which led me to issue #2578 that explained the behavior I was seeing.

Nickolay
  • 31,095
  • 13
  • 107
  • 185