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

.bat Files not running in Folder #51

Closed voshond closed 6 years ago

voshond commented 6 years ago

Might be related due to subsystem on new setup not being there? Tried to run them but always gave me an error that the file was not present, copied them to the desktop and put in the absolut path for the preset and the win10.ps1 for the desired effect.

Disassembler0 commented 6 years ago

Weird. In the past, there was a problem with current working directory getting lost during elevation, resulting in wrongly resolved relative paths, but that has been fixed in https://github.com/Disassembler0/Win10-Initial-Setup-Script/commit/d427ae735487e6fb35dec95b1d54c9f92c987ebd.

I'm using the presets in bat scripts normally, see eg. Win10-ApplyAll.bat from test folder.

Could you give me a specific example of your bat file?

voshond commented 6 years ago

@ECHO OFF

powershell.exe -NoProfile -ExecutionPolicy Bypass -File "Win10.ps1" -preset "Win10-Personal.preset"

All Files, Win10, the preset and the bat are in the same folder. Double checked if they were there and renamed correctly.

I'm trying this stuff out:

https://stackoverflow.com/questions/22490785/powershell-run-script-from-shortcut-using-relative-path

Disassembler0 commented 6 years ago

Ah, I see. That's probably just PowerShell being PowerShell. Try

powershell.exe -NoProfile -ExecutionPolicy Bypass -File ".\Win10.ps1" -preset "Win10-Personal.preset"
voshond commented 6 years ago

do i need .\ for the argument as well? trying in a sec

voshond commented 6 years ago

Works.

oddly when i use this method to run other scripts as well, it works. As soon as i run the script as admin it doesnt, any clues? trying to run this one: https://github.com/W4RH4WK/Debloat-Windows-10/blob/master/utils/install-basic-software.ps1

voshond commented 6 years ago

you had some "require admin" magic in there, is it because of that?

Disassembler0 commented 6 years ago

As it turns out you actually don't need to put .\ if you have it in the -File argument (which you do have). Only if you'd use just powershell.exe Win10.ps1 (without -File) then you'd need to supply also .\ otherwise you get

Win10.ps1 : The term 'Win10.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program.

However I've tried just now with your line without .\ and it works for me on clean Win10 Pro 1709 (16299.125).

The other error which you might be getting is

The argument 'Win10.ps1' to the -File parameter does not exist. Provide the path to an existing '.ps1' file as an argument to the -File parameter.

Which you'll get when you try to launch the script while you are in different directory than the Win10.ps1 and the preset are, because the relative paths are resolved against the current working directory.

Which error are you getting?

you had some "require admin" magic in there, is it because of that?

RequireAdmin magic is used for the auto-elevation. Regarding the presets, it just takes the relative path to the preset, resolves it and reinject as absolute path to the elevated script to prevent the error mentioned above.