I'm currently using this https://forums.mydigitallife.net/th...th-regular-user-and-admin.79962/#post-1536830 on a script. This first does non-admin stuff, then elevates to admin, quits non-admin session, and opens a brand new CMD session as admin user to do the rest. So I'm running 2 different CMD sessions. I set a variable myvar=foo in the first unprivileged session. It's only valid for that session obviously. Is there a way to pass that variable value to the privileged CMD session once it starts?
Simple as possible example: Code: @echo off rem your variable as example set myvar=foo rem Made by @UpGrade4 title Check for permissions >nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system" if '%errorlevel%' NEQ '0' ( echo Requesting administrative privileges... goto UACPrompt ) else ( goto gotAdmin ) :UACPrompt echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs" echo UAC.ShellExecute "%~s0", "%myvar%", "", "runas", 1 >> "%temp%\getadmin.vbs" cscript //B //Nologo "%temp%\getadmin.vbs" exit /B :gotAdmin if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" ) pushd "%CD%" CD /D "%~dp0" rem Do your stuff here as privileged user echo myvar=%1 pause In the above example you get the "myvar" as "foo" as the first parameter in the second CMD -> "%1"="foo"
Not working... At beginning of script I have something like if "%myvar%"==foo (goto admin) else (goto noadmin) After elevating and new clean CMD session as privileged user starts I still get "" = foo thus always going to NOADMIN
Please get whole my scrip, from the example, and run it from explorer window, for me it's working - checked.
Currently I have this: Code: @echo off if "%myvar%"==foo (goto ADMIN) else (goto NOADMIN) :NOADMIN REM Unprivileged stuff here [...] set myvar=foo ( echo Set UAC = CreateObject^("Shell.Application"^) > "%TEMP%\elevate.vbs" echo UAC.ShellExecute "%~f0", "%myvar%", "", "runas", 1 >> "%TEMP%\elevate.vbs" cscript //B //Nologo "%TEMP%\elevate.vbs" exit /b ) :ADMIN REM Admin stuff here ( if exist "%TEMP%\elevate.vbs" del /f "%TEMP%\elevate.vbs" 2>&1 pushd "%CD%" CD /D "%~dp0" ) echo myvar=%1 pause [...] I already know when elevating to privileged user it opens a totally new CMD session and starting the script *from very beginning*. Script clearly needs to do some stuff as unprivileged, and some other as privileged. When elevating to privileged user I need script to begin at :ADMIN label, not at the beginning. That's why I try checking the %myvar% variable at very first. But when just elevated and CMD starts in new session, I still get "if "" == foo". Thus :ADMIN label never actually enters. Some help please?
Why don't you an argument? Following are some instructions i developped for the Sledgehammer script : Code: set "params=Problem_with_elevating_UAC_for_Administrator_Privileges"&if exist "%temp%\getadmin.vbs" del "%temp%\getadmin.vbs" fsutil dirty query %systemdrive% >nul 2>&1 && goto :GotPrivileges :: The following test is to avoid infinite looping if elevating UAC for Administrator Privileges failed If "%1"=="%params%" (echo Elevating UAC for Administrator Privileges failed&echo Right click on the script and select 'Run as administrator'&echo Press any key to exit...&pause>nul 2>&1&exit) cmd /u /c echo Set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "%~0", "%params%", "", "runas", 1 > "%temp%\getadmin.vbs"&cscript //nologo "%temp%\getadmin.vbs" && exit /b || (echo Elevating UAC for Administrator Privileges failed&echo Right click on the script and select 'Run as administrator'&echo Press any key to exit...&pause>nul 2>&1&exit) :GotPrivileges ...UAC.ShellExecute "%~0", "%params%", "", "runas", 1... and you test the presence of this param with If "%1"=="%params%"
@rpo I agree, he get only a part of my script, without checking my script and analyze it. Then, it does not work for him - it don't works like that. For first the explanation. In the first check in your scrip the -> 'if "%myvar%"==foo (goto ADMIN) else (goto NOADMIN)' <- never return 'true', because -> "%myvar%" <- is not this same as -> foo <-, you missed double-quotes. For second, the variable "%myvar%" exists only in the :NOADMIN check function, but does not exists Globally in the the script. So, how it should work in your opinion? I already wrote to you that the variable %myvar% you get as the first parameter in the "brand new CMD" as %1 parameter. For first please check my script "as it's written", then analyze it and adapt the example to your script. Here is corrected example of your script: Code: @echo off if "%1"=="foo" (goto ADMIN) else (goto NOADMIN) :NOADMIN REM Unprivileged stuff here rem [...] set myvar=foo ( echo Set UAC = CreateObject^("Shell.Application"^) > "%TEMP%\elevate.vbs" echo UAC.ShellExecute "%~f0", "%myvar%", "", "runas", 1 >> "%TEMP%\elevate.vbs" cscript //B //Nologo "%TEMP%\elevate.vbs" exit /b ) :ADMIN REM Admin stuff here ( if exist "%TEMP%\elevate.vbs" del /f "%TEMP%\elevate.vbs" 2>&1 pushd "%CD%" CD /D "%~dp0" ) echo myvar=%myvar% echo %%1=%1 pause rem [...]
Yes, you were always right, I just could not understand it sooner. Apologies. I had indeed read your comment, but failed to understand that the variable was in the first parameter "%1", and not as the original name. Again, sorry for that. Just for notes, one last question. In this particular example I needed to pass a variable, %myvar%, as parameter for the second CMD. But in general, if no parameters needed to be passed, do I still need to pass the VBS script itself as parameter, "%TEMP%\elevate.vbs", or I could just leave that field empty?
OK. I don't know if I understand your question correctly. The "%TEMP%\elevate.vbs" run this script in elevated rights, for example with higher PowerUser/Admin rights. So, if you don't need to run this, or any, script as elevated, then you don't need to call this function. You can also transfer the "%1" parameter to the third script and/or the first script, for example: "Third_script.cmd %1", and in this script you can check for the "%1" parameter, if you want to know if the script was already elevated.