Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: Many powershell code items failed to run if privacy-script.bat is called with "setlocal EnableDelayedExpansion" #221

Closed
Mark-Joy opened this issue Apr 23, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@Mark-Joy
Copy link

Description

If calling privacy-script.bat from another script which has setlocal EnableDelayedExpansion, many powershell code items failed to run.

OS

Reproduction steps

Create a script that call privact-script.bat as below:

@echo off & setlocal EnableDelayedExpansion
call  privact-script.bat

Scripts

Screenshots

Additional information

It can be fixed by adding "setlocal DisableDelayedExpansion" at the beginning of the privact-script.bat, (also possibly "EnableExtensions" for user convenience), so it would be:

@echo off & setlocal EnableExtensions DisableDelayedExpansion

@Mark-Joy Mark-Joy added the bug Something isn't working label Apr 23, 2023
@undergroundwires
Copy link
Owner

undergroundwires commented Sep 25, 2023

Thank you for bringing this issue, and providing detailed information on the bug and a possible solution. Good contribution and I will add this.

An alternative solution would be calling DisableDelayedExpansion on the caller side:

setlocal DisableDelayedExpansion 
   call  privact-script.bat
endlocal

However, I think it is a good practice to manage the environment in the script privacy.sexy to make it as robust and independent as possible. The script itself should handle this without requiring users to know about the internal workings and requirements. So I will add this.

I believe we should also add endlocal at the end of the script to ensure this script does not interfere with callers environment.

Your future feedback is appreciated.

Start of the script:

@echo off
:: {{ $homepage }} — v{{ $version }} — {{ $date }}
:: Initialize script environment
setlocal EnableExtensions DisableDelayedExpansion
:: Ensure admin privileges
fltmc >nul 2>&1 || (
    echo Administrator privileges are required.
    PowerShell Start -Verb RunAs '%0' 2> nul || (
        echo Right-click on the script and select "Run as administrator".
        pause & exit 1
    )
    exit 0
)

End of the script:

endlocal
pause
exit /b 0

Out-of-topic: pause and exit /b 0 would probably be intrusive as well. We could add ability to disable those lines with a flag, but this we should discuss in another github issue as I'd consider this a feature rather than a bug fix.

undergroundwires added a commit that referenced this issue Oct 24, 2023
This commit ensures the script functions as expected, even when invoked
from unexpected environments.

Using `setlocal` initializes a distinct environment for privacy.sexy.
It's strategically placed after the admin privilege check to avoid
unnecessary setup in case of a relaunch. The script concludes with
`endlocal` right before the exit, maintaining a clean environment
throughout its execution and ensuring no unintentional global
environment modifications.

Changes:

- Enhance script's environment robustness.
- Add descriptive comments for script start/end sequences.
@undergroundwires
Copy link
Owner

Fixed and released in 0.12.6 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants