zorvek (Kevin Jones)
asked on
Passing Quoted Parameter to NirCmd elevated Command
Trying to pass a quoted parameter (file path) to NirCmd elevate from a bat script. This works:
"C:\...\nircmdc.exe" elevate "C:\...\Script.bat" C:\Test.txt
This does not:
"C:\...\nircmdc.exe" elevate "C:\...\Script.bat" "C:\Test.txt"
How can I pass a quoted parameter to NirCmd elevate?
"C:\...\nircmdc.exe" elevate "C:\...\Script.bat" C:\Test.txt
This does not:
"C:\...\nircmdc.exe" elevate "C:\...\Script.bat" "C:\Test.txt"
How can I pass a quoted parameter to NirCmd elevate?
I created a bat file and put this into bat
it works fine...
what does your bat file look like...
C:\Utility\NIRSoft\NirSoft\nircmd.exe elevate "notepad.exe" 'C:\temp\1.txt'
then run that bat file from command lineit works fine...
what does your bat file look like...
and this also works
C:\Utility\NIRSoft\NirSoft\nircmd.exe elevate "notepad.exe" 'C:\temp\1.txt'
C:\Utility\NIRSoft\NirSoft\nircmd.exe elevate "notepad.exe" """C:\temp\2.txt"""
ASKER
That works but the parameter gets buggered before it is passed to the .bat script if the path has any spaces:
"C:\...\nircmdc.exe" elevate "C:\...\Script.bat" 'C:\Test File.txt'
"C:\...\nircmdc.exe" elevate "C:\...\Script.bat" 'C:\Test File.txt'
this worked for me
C:\Utility\NIRSoft\NirSoft\nircmd.exe elevate "notepad.exe" """C:\temp\1 - Test.txt"""
this one also worked
C:\Utility\NIRSoft\NirSoft\nircmd.exe elevate "notepad.exe" 'C:\temp\1 - Test.txt'
maybe the issue is bat file
what do you have in bat file?
Script.bat
what do you have in bat file?
Script.bat
In your Batch are you using %1 or %~1 or %~f1 ?
Zorbek Does you nircmd path contain spaces?
Can you put your nircmd somewhere without spaces or add its location to your PATH variable?
Zorbek Does you nircmd path contain spaces?
Can you put your nircmd somewhere without spaces or add its location to your PATH variable?
I guess using " in your bat file resolves the issue...
like
notepad.exe %1
>>>
notepad.exe "%1"
like
notepad.exe %1
>>>
notepad.exe "%1"
ASKER
When I use single quotes it calls the second script but the file name is split at the first space. When I use double quotes the second script isn't called.
My first script "Test Script 1.bat":
My second script "Test Script 2.bat"
I'm not sure why but your notepad example with double quotes works. But when I use double quotes on the file path parameter in my script, the second script is never called.
My first script "Test Script 1.bat":
SET FilePath=C:\Users\Kevin Jones\Desktop\Test\Test File.txt
"C:\Users\Kevin Jones\Desktop\Test\nircmdc.exe" elevate "C:\Users\Kevin Jones\Desktop\Test\Test Script 2.bat" '%FilePath%'
PAUSE
My second script "Test Script 2.bat"
ECHO %1%
PAUSE
I'm not sure why but your notepad example with double quotes works. But when I use double quotes on the file path parameter in my script, the second script is never called.
ok, I created same files and edited...
now what are you doing, how do you call and which one
and what should happen and what is not working...
now what are you doing, how do you call and which one
and what should happen and what is not working...
Drop the second %
just use %1
also double quotes are the correct way, try instead using them around your
SET "FilePath=C:\Users\Kevin Jones\Desktop\Test\Test File.txt"
there is something else I believe is happening, but need to test first
just use %1
also double quotes are the correct way, try instead using them around your
SET "FilePath=C:\Users\Kevin Jones\Desktop\Test\Test File.txt"
there is something else I believe is happening, but need to test first
ASKER
I am running Test Script 1.bat first. It runs nircmdc.exe with which I am trying to run Test Script 2.bat.
ASKER
If I drop the second % then the variable substitution doesn't happen.
ASKER
Any time I add double quotes the second script is not invoked.
ASKER
The objective is to have the first script call the second script using nircmd's elevate function and pass a file path that can include spaces. The parameter needs to be passed to the second script as the first parameter in it's entirety without being broken apart by those embedded spaces.
When a double quote is passed to nircmd after the first parameter (the script name) it fails to invoke the second script.
When single quotes are used, the second script is called but the file path is broken up by any embedded spaces and only part of the path is received as the first parameter.
When a double quote is passed to nircmd after the first parameter (the script name) it fails to invoke the second script.
When single quotes are used, the second script is called but the file path is broken up by any embedded spaces and only part of the path is received as the first parameter.
looks like nircmd elevate is not working to call bat files...
Test Script 1.bat
Test Script 1.bat
SET FilePath=C:\temp\Test File.txt
SET cmd="C:\temp\Test Script 2.bat" "%FilePath%"
echo %cmd%
"C:\Utility\NIRSoft\NirSoft\nircmd.exe" elevate %cmd%
PAUSE script 1...
Test Script 2.bat ECHO %1%
notepad %1%
PAUSE script 2...
command line"Test Script 1.bat"
no error, but no notepad, it closes immediately...
even this, some spaces in the program part, works fine
SET cmd="C:\Program Files\Windows NT\Accessories\wordpad.exe" "%FilePath%"
but bat file fails...
ASKER
Our tests work fine as long as we are not calling a .bat file. Here are the results of my tests:
My conclusion is that, for whatever reason, nircmd's elevate function has problems with .bat files that it does not have with .exe files. If the file path parameter is enclosed in double quotes, or ay double quote used after the .bat file reference, the .bat is not called at all. If the file path parameter is enclosed in single quotes or no quotes are used, the .bat file is called but the file path parameter is parsed into multiple parameters split by spaces.
One workaround is to use single quotes or no quotes and use the %* parameter reference in the invoked .bat file but that only allows for one parameter.
REM This calls the .bat script but the system breaks apart the path into multiple parameters
SET FilePath=C:\Windows\System32\Test File.txt
SET Command="C:\Windows\System32\Test Script.bat" '%FilePath%'
"C:\Windows\System32\nircmdc.exe" elevate %Command%
REM This does not call the .bat script at all
SET FilePath=C:\Windows\System32\Test File.txt
SET Command="C:\Windows\System32\Test Script.bat" "%FilePath%"
"C:\Windows\System32\nircmdc.exe" elevate %Command%
REM This runs Notepad and the file is opened
SET FilePath=C:\Windows\System32\Test File.txt
SET Command="C:\Windows\System32\Notepad.exe" '%FilePath%'
"C:\Windows\System32\nircmdc.exe" elevate %Command%
REM This runs Notepad and the file is opened
SET FilePath=C:\Windows\System32\Test File.txt
SET Command="C:\Windows\System32\Notepad.exe" "%FilePath%"
"C:\Windows\System32\nircmdc.exe" elevate %Command%
My conclusion is that, for whatever reason, nircmd's elevate function has problems with .bat files that it does not have with .exe files. If the file path parameter is enclosed in double quotes, or ay double quote used after the .bat file reference, the .bat is not called at all. If the file path parameter is enclosed in single quotes or no quotes are used, the .bat file is called but the file path parameter is parsed into multiple parameters split by spaces.
One workaround is to use single quotes or no quotes and use the %* parameter reference in the invoked .bat file but that only allows for one parameter.
I concluded the same thing before...
elevate bat file with nircmd does not work...
any other executabble works...
either we need to check `run-as` or find another utility (I guess all others will do the same thing -v will not work with bat)
or you need to use some other logic...
maybe create a tiny exe instead of bat file
elevate bat file with nircmd does not work...
any other executabble works...
either we need to check `run-as` or find another utility (I guess all others will do the same thing -v will not work with bat)
or you need to use some other logic...
maybe create a tiny exe instead of bat file
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
"C:\...\nircmdc.exe" elevate "C:\...\Script.bat" 'C:\Test.txt'