Running CMD with Admin rights in Windows 8

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
klownboy
Posts: 4477
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.8737 at 100% 2560x1440

Running CMD with Admin rights in Windows 8

Post by klownboy »

Hi,
Has anyone had any issues using run cmd /c command in Windows 8 or 8.1 x64? I have the following line in an XY script which works fine on Windows 7 x64 (on 2 computers), but it will not run in Windows 8. The reason is that the devcon command I'm running requires that it be run with Administrative privileges.

Code: Select all

run "cmd /c D:\Tools\DevCon\devcon.exe disable USB\ROOT_HUB20 & D:\Tools\DevCon\devcon.exe enable USB\ROOT_HUB20", , 2, 0;
I've used the command in an open command prompt window with admin privileges and it works fine, but doesn' work if open a prompt without admin privileges or run from a XY script. The Microsoft devcon.exe utility can restart, list, enable, and disable, any device (e.g., keyboard, mouse, sound driver, etc). In this case I'm using it to restart a flash or external hard drive that have been safely removed but not physically removed from the computer. Windows 7 must be automatically running the cmd with elevated privileges whereas Windows 8 does not, even though I'm the sole user and administrator. I've done some searching and read about the "runas" command which I haven't got to work properly. I was initially hoping CMD had a switch to run it with Admin privileges but it doesn't. You'd think it would automatically run CMD with elevated privileges when the user is admin which I guess is what Windows 7 does. Anyone have any ideas or encounter something similar?
Thanks,
Ken

highend
Posts: 15016
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Running CMD with Admin rights in Windows 8

Post by highend »

I'm just using hstart () to start programs with admin rights under Windows Server 2012 R2 (x64).
UAC (for admin) is turned off (via gpedit.msc). Don't know if it makes a difference regarding the necessary
parameters for hstart.

http://www.ntwind.com/software/hstart.html

From one of my older scripts:

Code: Select all

    $currentFile = readfile("<curitem>");
    $needsElevation = (strpos($currentFile, "Admin Check") != -1) ? "TRUE" : "";

    $hstart = (%osbitness% == 64) ? "<xypath>\@Tools\HStart\hstart64.exe" : "<xypath>\@Tools\HStart\hstart.exe";

    if ($needsElevation) {
        run """$hstart"" /UAC ""<curitem>""", , 2, 1;
    } else {
        run """$hstart"" ""<curitem>""", , 2, 1;
    }
One of my scripts helped you out? Please donate via Paypal

klownboy
Posts: 4477
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.8737 at 100% 2560x1440

Re: Running CMD with Admin rights in Windows 8

Post by klownboy »

Thanks highend, I'll give it a try. First I'll see if I can get my Windows 8 behavior the same as Windows 7 where running CMD via XYplorer is automatically done with admin rights. In both cases I'm the sole user and administrator.
Ken

highend
Posts: 15016
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Running CMD with Admin rights in Windows 8

Post by highend »

Beeing logged on as a user that is in the administrators group is not the same as having realadmin rights as long as you don't start an application with admin permissions (e.g. from the context menu).

I guess you are starting XYplorer under Win 7 with admin permissions? Otherwise it wouldn't execute e.g. a %cmdspec% with real admin rights.
I don't want to have XY real admin rights all the time that's why I use hstart :)
One of my scripts helped you out? Please donate via Paypal

klownboy
Posts: 4477
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.8737 at 100% 2560x1440

Re: Running CMD with Admin rights in Windows 8

Post by klownboy »

Yes, it looks like being an administrator doesn't necessarily mean you have elevated admin rights or privileges. I actually didn't have my XYplorer icon down in the taskbar set to Run As administrator yet I have no issues running CMD in the above script line and getting the devcon to work. Yet in Windows 8.1 it doesn't. As I said above, I know that's the issue since the command does run fine if I open a command window as administrator. Unlike Windows 7 where you right click on the cmd to get the context menu and click on run as admin, in Windows 8 there are 2 separate commands listed when you right click in the lower left corner, one for the normal "cmd" and one for "cmd with admin".

By the way, I tried setting up a link or shortcut for devcon with admin rights but that doesn't seem to run via XY's in a run script line.

There are definitely differences in the way Windows 7 and 8 handle running CMD. I have to do some more research on the Windows eight forum and some other sites.

I've got to do more looking at hstart as well, but would I need simply place the hstart command with the proper switches like /ELEVATE before "CMD". In other words, I assume I still need to use the CMD in the script line before the devcon command (since it not a GUI program) and if so, can that be done all in a single run command in XY? I see in one of their web site examples using hstart and CMD to run a batch file. I assume in your example, you are using it to run a Windows executable. In my case, I suppose devcon has to be run either through a batch file/CMD...maybe a batch with elevated admin rights. is another option.

Thanks again,
Ken

highend
Posts: 15016
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Running CMD with Admin rights in Windows 8

Post by highend »

maybe a batch with elevated admin rights
That's probably the easiest option. Quoting things with hstart can be a bit weird sometimes.

Btw, why are you using disable + enable instead of the restart as an option for devcon?
One of my scripts helped you out? Please donate via Paypal

klownboy
Posts: 4477
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.8737 at 100% 2560x1440

Re: Running CMD with Admin rights in Windows 8

Post by klownboy »

As a follow-up, to the last post, I did get the following command to work using hStart. Though I'll continue to look for a way to accomplish it (i.e., run devcon with elevated privileges) in Windows 8.1 as I can with Windows 7.

Code: Select all

   run "D:\Tools\hStart\hstart64.exe /NOCONSOLE /ELEVATE ""cmd /c D:\Tools\DevCon\devcon.exe disable USB\ROOT_HUB20 & D:\Tools\DevCon\devcon.exe enable USB\ROOT_HUB20""", , 2, 0;
Highend, I just saw your reply as I was typing. From what I read everywhere that was the way to go. The USB "HUB", not the individual device, has to be disabled and then enabled for this to work and it can be done in the same line. I tried so many combinations of parameters I can't remember if I tried restart. I've also read that "remove" and "rescan" is another method or "rescan" after "disable".

I did get a batch to run properly (w/o the use of hStart) when I ran it from the list panel in XY but only if I used the run as admin from the context menu. I wasn't able to set the batch file to be run as admin all the time/permanently so the batch would run properly in an XY script. This site has a bunch of example batch files http://www.robvanderwoude.com/devcon.php
Thanks again,
Ken

klownboy
Posts: 4477
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.8737 at 100% 2560x1440

Re: Running CMD with Admin rights in Windows 8

Post by klownboy »

Hey highend,
hStart is a nice little program, but have you noticed it leaves itself in memory taking substantially more resources than programs like USBDLM. Since I'd only have need to use it occasionally, I wish it had a switch which would unload from memory after use. Still investigating alternatives like running a batch or a scheduled tasks with elevated rights as another possible method, but not much time to do so. I'm extremely busy under a beach umbrella in Grand Cayman. :D
Ken

highend
Posts: 15016
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Running CMD with Admin rights in Windows 8

Post by highend »

Hi Ken,

happy holidays! :)

hstart remains in memory? Öhm, no, I can't confirm this...
How does your run ... line looks like? Maybe you're using a run() with a wait parameter?

A different approach would be:
Use a heredoc syntax and write something like this in the batchfile:

Code: Select all

REM ### Admin Check ########################################
SET "vbsFile=%TEMP%\~getAdmin.vbs"
REG QUERY "HKU\S-1-5-19" >NUL 2>NUL
IF %ERRORLEVEL% NEQ 0 (
	ECHO Set UAC = CreateObject^("Shell.Application"^)> "%vbsFile%"
	ECHO UAC.ShellExecute "%~s0", "", "", "runas", ^1>> "%vbsFile%"
	"%vbsFile%"
	EXIT /B
) ELSE (
	IF EXIST "%vbsFile%" (DEL "%vbsFile%" && PUSHD "%~dp0")
)

<the code that you're running in the batch>
One of my scripts helped you out? Please donate via Paypal

klownboy
Posts: 4477
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.8737 at 100% 2560x1440

Re: Running CMD with Admin rights in Windows 8

Post by klownboy »

Hi highend,
Yes, hstart does stay in memory...don't ask me why. Though I've only used hstart with Windows 8.1 since there was no need in Windows 7 where devcon ran elevated. Maybe staying in memory is a Windows 8 issue.

I used the run command in a script exactly as shown in my post above dated 18 Dec 2013 15:07 (EST). I can't confirm size now, but if I remember correctly it was around 15 meg. I suppose I could always use pskill from the Microsoft sysinternal utilities http://technet.microsoft.com/en-us/sysi ... s/bb896649 to kill hstart after using it. I'll definitely try your vbs modified batch file when I return. My additional batch line would simply be the cmd line in the first post without the run business in the front and end.
Thanks again and Happy Holidays,
Ken

klownboy
Posts: 4477
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.8737 at 100% 2560x1440

Re: Running CMD with Admin rights in Windows 8

Post by klownboy »

Anyone using USBDLM. Uwe Sieber issued a new update V4.8.0.0 around Christmas time with some nice changes.

highend, for your info, I did solve my issues discussed above running USBDLM in Windows 8.1 without having to resort to hStart or the visual basic mod batch file to obtain elevated rights. Two things solved the problem, the first was using "elevated=1" in USBDLM's ini file - it solved the issue of automatically running the XY script (which adds the Volume labeled tab for the USB external drive or flash drive) with elevated rights. It wasn't needed in Windows 7 running as an Administrator. The second was ensuring the two USBDLM files that run in memory are running as administrator and I did this by via properties for the file in Windows 8.1 Task Manager.

Happy New Year,
Ken

highend
Posts: 15016
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Running CMD with Admin rights in Windows 8

Post by highend »

Hi Ken,
Two things solved the problem, the first was using "elevated=1" in USBDLM's ini file
I use that one, too (to automatically invoke SyncBackSE to create backups).
The second was ensuring the two USBDLM files that run in memory are running as administrator and I did this by via properties for the file in Windows 8.1 Task Manager.
I use Server 2012 R2 as my primary (and only) OS and I don't need this elevation to let USBDLM run properly...

However, great you've solved it!

Happy new year, too :)
One of my scripts helped you out? Please donate via Paypal

admin
Site Admin
Posts: 67155
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Running CMD with Admin rights in Windows 8

Post by admin »

highend wrote:I use Server 2012 R2 as my primary (and only) OS ...
Does XY show the correct OS version info now?

highend
Posts: 15016
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Running CMD with Admin rights in Windows 8

Post by highend »

admin wrote:Does XY show the correct OS version info now?
Under "Various Information"?
OS: Windows Server 2012 R2 Datacenter Server, 64-bit (6.3)
One of my scripts helped you out? Please donate via Paypal

admin
Site Admin
Posts: 67155
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Running CMD with Admin rights in Windows 8

Post by admin »

Good. :D I don't think this worked yesterday. :)

Post Reply