Disassembler0 / Win10-Initial-Setup-Script

PowerShell script for automation of routine tasks done after fresh installations of Windows 10 / Server 2016 / Server 2019
MIT License
4.7k stars 1.08k forks source link

Run From Inside Existing Script #196

Closed jonvickers closed 5 years ago

jonvickers commented 5 years ago

I am a faily newb to Powershell.

I can't for the life of me run this script from inside of an elevated powershell prompt using a simple command like

PS c:> .\win10.ps1 -include c:\temp\win10.psm1 -preset c:\temp\default.preset

I get no response. It works if I use the ..: Start-Process Powershell.exe... from the docs

Am I calling the function wrong somehow?

jonvickers commented 5 years ago

What am I doing wrong? I get nothing

image

Disassembler0 commented 5 years ago

Hmm. This was dragged into the script in 5ccc3767645d96994660986af565ff90e320725b.

The problem is that there is a variable called $tweaks in the main scope of the script and there is a function which is supposed to fill it with tweak names to be invoked, which refers to it as $global:tweaks. Turns out that if you run the script as you're trying to, the global scope is taken from context of your whole PowerShell process and not just the executed script, so the final line which uses again just $tweaks variable, loops through it and invokes the actual tweaks gets an empty list. If you run Start-Process Powershell.exe ..., the main scope and the global scope are the same and everything works as expected.

The obvious fix would be to replace $tweaks with $global:tweaks on lines 16 and 53, but let me think if there is any other elegant way how to fix it without polluting the global scope altogether.