You are not logged in.
Has anyone figured out how to execute a bat file from a UNC drive on Windows 7?
Take the simple example below:
@ECHO OFF
ECHO.%CD%
PUSHD "%~dp0%"
ECHO.%CD%
POPD
PAUSE
Put the above code in a bat file in a UNC location (\\Sw-dev\Users\SomeName\Test\CantRunAsAdmin.bat for my example)
You will get the following results on Vista if you run by double clicking on it or by right clicking and selecting 'Run as administrator'.
You will get the same results on Windows 7 if you run by double clicking on it.
Results:
'\\Sw-dev\Users\SomeName\Test
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported. Defaulting to Windows directory.
C:\Windows
Z:\ SomeName\Test
Press any key to continue . . .
So far so good.
Now, if you right click and select 'Run as administrator' on Windows 7 you get the CMD screen to flash briefly and disappear.
My understanding is that this is intentional on Microsoft's part for security reasons. However, there are cases where this prevents us form doing things the way we used to do them. For example I have a bat files to do workstation updates. It is not a big deal to copy the files to the local drive and execute locally when you have 1 or 2 workstations. It's another matter when you have 50 or more.
Windows Shell Scripting and InstallShield
Offline
Not saying this will work but have you tried creating a shortcut to your batch and in properties on the shortcut select the tick box run as administrator priveledge to see if this will overide the above problem?
This is how i get round UAC interference on 7 i get one question from UAC about the running of the shortcut but the files in the batch are as admin
Offline
Has anyone figured out how to execute a bat file from a UNC drive on Windows 7?
I think the only thing you can do is map a drive letter to the UNC path and execute the scripts from within that context.
cmd | *sh | ruby | chef
Offline
Thanks Chimaera & bluesxman. I tried the methods that you suggested with no luck.
Windows Shell Scripting and InstallShield
Offline
I can't speak for the way your script has been written, but I don't think there's any reason why mapping a drive letter to the UNC path shouldn't work -- it works fine for me (at least in WinXP, maybe they've done something different in Win7?)
When I run this simple script from a network drive:
@echo off
cd
pause
I get this result:
\\TestSvr\VOL1 G:\>cd users
\\TestSvr\VOL1 G:\users>cd bluesxman
\\TestSvr\VOL1 G:\users\bluesxman>a.cmd
G:\users\bluesxman
Press any key to continue . . .
By the way, I have my prompt set up thus:
PROMPT=$M$P$G$+
Last edited by bluesxman (03 Mar 2010 19:48)
cmd | *sh | ruby | chef
Offline
bluesxman,
Yes, they have changed stuff for Windows 7. Your code works on Vista (any way you run it) and on Windows 7 if you just execute it.
BUT... IT DOES NOT WORK when you right click and 'Run as administrator' on Windows 7!
The 'Run as Administrator' checkbox in the compatibilty screen is grey out too.
I think I am out of luck for now.
Last edited by RG (04 Mar 2010 00:40)
Windows Shell Scripting and InstallShield
Offline
Ack, my bad. Seems my brain entirely skipped over the paragraph in the middle where you said you couldn't run as Administrator.
Does creating a shortcut to "cmd.exe /c yourScript.cmd" work any better?
cmd | *sh | ruby | chef
Offline
Nope. Already tried that. Same result. I think Microsoft intentionally did it for security. There are other annoyances too.
You can't copy files from a UNC drive into SOME folders (like Program Files). You have to copy them to a local drive and then to Program Files (or whatever).
The Microsoft evangelists tout how great 7 it is... and it probably is better than Vista... it is just annoying to have to give up functionality that you are actively using. I went through the UAC hoops and all when I went from XP to Vista. More of the same for 7.
Last edited by RG (04 Mar 2010 03:04)
Windows Shell Scripting and InstallShield
Offline
I might be way off base here, but bear in mind that when you're running something in the context of another user, any share mappings you have created with the user you're logged in with will not be passed through for the "run as" user.
Try doing this from a CMD Windows to test if it's what's causing your problem:
runas /user:administrator cmd.exe
Give the administrator password and in the new CMD window that appears, try to do a "dir" of the network location where your scripts sit.
cmd | *sh | ruby | chef
Offline
bluesxman,
You may be on to something there. I've tried something like that, but you have given me an idea. I will try that next week as I am working from home today and can't RDP to the machine I need.
Thanks,
I'll let you know what I find next week.
Windows Shell Scripting and InstallShield
Offline
RG,
Did you ever get past this UAC and UNC runas admin issue?
The only way around I know of is to disable UAC on the users computer or use autoit instead
cmd, vbs, ps, bash
autoit, python, swift
Offline
NDog,
Nope... never did get around it. Do as you say or copy to local drive and execute from there.
Someday I may try the suggestion at the bottom of this link just for fun, but I can't use that on customer's systems.
http://support.microsoft.com/kb/937624
Last edited by RG (12 Aug 2011 21:45)
Windows Shell Scripting and InstallShield
Offline
NDog,
Good news! Maybe....
It works if you modify the registry as stated in:
http://support.microsoft.com/kb/937624
That might be ok for you own use, but I wouldn't do it on a customer's system because of the following note:
"Important This workaround may make your system unsafe. Microsoft does not support this workaround. Use this workaround at your own risk."
Windows Shell Scripting and InstallShield
Offline
Hi RG,
Did you ever get this to work without the registry changes? I am having similar issue when I try to run batch file from Windows 7.
Offline
Stew,
Nope, never did find a way. Actually have given up on it. If I was just needing it for my own in-house use I would try the registry changes. Just don't feel comfortable doing that to a customer's machine though. Please post if you find something the rest of us have overlooked
In some cases I have added the code below near the beginning of bat files that a user might try to execute from a UNC drive. This works because the current directory gets switched to %systemroot%. Of course this means that you can't execute your bat file from %systemroot%.
IF "%CD%"=="%systemroot%" (
COLOR CF
ECHO.
ECHO ERROR! This program must be executed from a local drive.
PAUSE
EXIT
)
Last edited by RG (21 Jan 2012 04:04)
Windows Shell Scripting and InstallShield
Offline
RG,
I found the following but I haven't used or try it as yet. I don't know if this will resolve the issue.
Offline
Stew,
Thanks for posting. I gave that a try and I get the same results. I tried it on 2 Windows 7 boxes.
No problem if UAC is turned off.
My other box has UAC setting at 3rd level and it fails like before.
Don't have the time to play with it now... maybe some other time.
Last edited by RG (23 Jan 2012 21:00)
Windows Shell Scripting and InstallShield
Offline
As per my earlier posting -- if you're doing "run as Administrator", wouldn't the share mapping go away when the Administrator script "session" finishes anyway?
Sorry I don't have Win7 here to check.
cmd | *sh | ruby | chef
Offline
I ediited my last post. Initial results were flawed because I tested on a Windows 7 box with UAC turned off.
Windows Shell Scripting and InstallShield
Offline