Win 8 - Run exe as admin - help

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
hoangvo81
Pro Scripter
Posts: 69
Joined: Tue Feb 07, 2012 8:02 pm

Win 8 - Run exe as admin - help

Post by hoangvo81 » Wed Dec 04, 2013 2:54 am

Hi,

Can someone confirm this if possible.
script is below - read a text file contains all the reserve drives.
get a list of logical drive via WMIC into a text, regex out the name, : and just leave the letter as is in side the variable.

run through D-Z, find the next available drive
pass the avialble drive, reserve drive letter to the changeD subroutine in vbscript. which than change the reservedrive letter which if found and change it to the available drive letter.

I know what the problem is but cannot confirm if its system setup related or if its os related.

I am running windows 8, UAC is on.
There's 3 account on this PC which is hock to our workplace's network using group policy.
The account i m on is part of the Administrator group.
There's a user account call Administrator on this PC which is also administrator group.

Code: Select all

// COMPILE_OPTS|C:\temp\test.exe||CONSOLE=0|INCLUDES=1||RUNTIMES=1|BMPS=1
VBSTART
  sub ChangeD (DriveL,ReserveName)
    dim objWMIService
    dim drvVol
    set objWMIService=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
    'type 5=cdrom,6Ram,4network,2removeable,3logical, remove from DriveType in (x,x,x,x) if you want to include them .
    'this will look for all cdrom/ram,network and removeable disc only.
    set drvVol=objWMIService.execquery("select * from Win32_Volume where (DriveType=5 or DriveType=4 or DriveType=2 or DriveType=6) and Name='" & ReserveName & ":\\'")
    if drvVol.count=1 then
      'should only be 1
      for each vol in drvVol
          vol.DriveLetter="" & cstr(DriveL) & ":"
          vol.Put_
      next
      set objWMIService=nothing
      set drvVol=nothing
    else
        msgbox "Found " & drvVol.count & " will not attempt!"
    end if
  end sub
VBEND

'for multiple drive reserve potential.!! - have not completely test with network drives etc.
readfile>%script_dir%\ReserveDrives.txt,reserveDrives
trim>reserveDrives,reserveDrives
Separate>reserveDrives,crlf,drives
let>j=0
repeat>j
  add>j,1
  let>reserveDrive=drives_%j%
  if>reserveDrive<>
       gosub>ProcessDriveChange
  endif
Until>j=drives_count
goto>ENDProgram

'for Single drive reserv
//let>reserveDrive=E
//gosub>ProcessDriveChange

SRT>ProcessDriveChange
let>LOCALVAR=1
ife>%script_dir%\DriveName.txt
    deletefile>%script_dir%\Drivename.txt
endif
runprogram>cmd.exe /c wmic logicaldisk get name > %script_dir%\DriveName.txt
wait>1
readfile>%script_dir%\DriveName.txt,discName
regex>:,discname,,m,n,1,,discname
regex>Name,discname,,m,n,1,,discname
'ascii values for drive letter A-65, Z=90
'find the next drive available not used.
'start at D to Z
let>k=67
repeat>k
  add>k,1
  VBEval>Chr(%k%),charVal
  regex>%charVal%,discname,,m,n,0,
  regex>%charVal%,reserveDrives,,o,p,0,
  if>{(%n%=0) and (%p%=0)}
    vbrun>ChangeD,%charVal%,%reserveDrive%
    wait>2
    let>k=90
  endif
Until>k=90
End>ProcessDriveChange
//let>nextDrive=H:
//vbrun>changeD,%nextDrive%
Label>ENDProgram
/*ReserveDrives.txt
E
G
I
*/

The area where i m having a problem with is:

Code: Select all

runprogram>cmd.exe /c wmic logicaldisk get name > %script_dir%\drivename.txt
This code gives me a list of all the drives name currently being used.

if i run this is command promp directly (wmic logicaldisk.. etc). the output gives me all the drives

if I open command prompt as Administrator (via right click run as administrator), and run the same command, i get all drive except 1 drive (J) which is currently being mapped under my user account.
When its trying to change the letter this is where i m getting an error.

I just want to see if someone can test the above script or see if there's a problem with the above script and advise.

Any direction is appreciated.
Thanks

Hoang.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Thu Dec 05, 2013 11:28 am

This:

runprogram>cmd.exe /c wmic logicaldisk get name > %script_dir%\drivename.txt

Works fine for me, correctly reporting all drives that I have mapped.

However, if I understand you properly you want to know what drives are mapped to other profiles. I'm not sure that this is possible. Surely drives aren't mapped until profiles are actually logged in - when the session is active.

I should think you'd only be able to get mapped drives for logged on users. I'm not sure how to do that.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

hoangvo81
Pro Scripter
Posts: 69
Joined: Tue Feb 07, 2012 8:02 pm

Post by hoangvo81 » Thu Dec 05, 2013 5:55 pm

try this:

1) open up command (non administrator version)
then run the command: wmic logicaldisk get name > C:\temp\drivename.txt
take a look at the file, should show all disk in use
2) subst w: c:\
3) rerun the wmic command again
should now show the W disk as well.

4) open command prompt as Administrator
run the wmic command
open the file and do you see the W: drive?

The problem I am having is:
when i make this into an exe file, run as administrator.
The computer's local admin ran teh WMIC command as admin as well. This cause a failure when the next drive available is not detected by the WMIC command line under adminstrator.

If i run this as a user, the vbscript will not allow me to change the drive volume letter causing an access is denie.

I might have to just do everything in vbscript rather than its current state, but i thought i check to see if this is normal under windows 8, i tried on a windows 7 and it seems to be fine.

Thanks

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts