How can I run a cscript as administrator with another vbscript? - vbscript

I can use CreateObject("shell.application").ShellExecute but can I run a cscript as administrator? using that? I'm kind of a newbie vbscripter. Thanks!

Have you tried the RunAs command? I posted a solution recently that walks you through getting the proper syntax for executing the RunAs command from within VBScript.
Take a look and see if it helps.

The scriptPath is the path to your script you want to run as administrator
The Arguments is the commandline arguments you want to pass to the script. NOTE the Arguments are space separated, if you want to pass an argument that contains spaces you will need to enclose that arguments in quotes [Chr (34) is the quotes]. to do that you will need to write for example. Arguments= chr (34) & "Hello World" & chr(34)
ScriptPath="Path to your script"
Arguments="Any arguments to pass to the script"
CreateObject("Shell.Application").ShellExecute "cscript.exe",_
Chr(34) & ScriptPath & Chr(34) & _
" " & Arguments, _
"", "runas", 1

Related

WshShell.Run with spaces in command line params

From a vbscript I'm trying to get the user profile directory to expand and also run an executable with command line arguments. It is the spaces in the command line args that seem to be causing an issue. I'm close but no matter what I try I can't seem to get it.
This works:
WshShell.Run """%UserProfile%\test.exe"""
What I'm trying to do that does not work:
WshShell.Run """%UserProfile%\test.exe 8.8.8.8 8989 -e cmd.exe"""
I get an error of "The system cannot find the file specified."
As already mentioned in the comments the reason for the error in the second example is because the literal quotes are only needed to wrap a file path containing spaces, so you line should be;
WshShell.Run """%UserProfile%\test.exe"" 8.8.8.8 8989 -e cmd.exe"
It's the equivalent of writing
"%UserProfile%\test.exe" 8.8.8.8 8989 -e cmd.exe
at the command prompt in a console window (note, the position of the literal quotes).
Useful Links
Answer to: About using Double quotes in Vbscript
You can debug your variable before any execution with Wscript.Echo or MsgBox
Wscript.Echo chr(34) & CreateObject("wscript.shell").ExpandEnvironmentStrings("%UserProfile%\test.exe") & chr(34) & " 8.8.8.8 8989 -e cmd.exe"

Applescript shell script to folder with space

I am trying to write a text file to a folder that contains space in the name. eg: folder 2
The first script that writes to folder works correctly
The second script that writes to folder 2 does not work correctly.
Edit:
I tried to use single quotes around the path and it works, if I hardcode the path. Is there a way to write to the folder without losing the ~ symbol?
--this works
do shell script "echo " & "the text" & " >> ~/Desktop/folder/text.txt"
--this does not work
do shell script "echo " & "the text" & " >> ~/Desktop/folder 2/text.txt"
This will work:
do shell script "echo " & "the text" & " >> ~/Desktop/folder\\ 2/text.txt"
Note: When using ~, which is the same as $HOME, in a do shell script and there is a space in the pathname, use a double backslash \\ to escape the space and do not quote the pathname containing a ~. In bash the space would only need a single backslash \ to escape the space.
This works too:
do shell script "echo " & "the text" & " >> \"$HOME/Desktop/folder 2/text.txt\""

How to deal with white spaces in osascript [duplicate]

This question already has answers here:
Combining variables in Bash to form a command sent to AppleScript using the osascript command
(2 answers)
Closed 5 years ago.
I'm in need of your assistance on how to deal with white space in a path.
In my example below, I'm setting some variables in a Terminal bash shell and executing, "osascript" from command line, utilizing these variables to run ['do shell script'] & ['display dialog']. When I run these from command line, I'm getting different results. My ultimate goal here is to be able to execute ['do shell script'] and it execute the bash shell script based on the path. In this case, the path has spaces in it.
NOTE: The INSTALLER_PATH variable defined below is set like this because the path is generated from an Apple Script I wrote that basically takes a path [ with white spaces in it ] and combines that path with another variable. Because this path has spaces in the name, I'm using the [ to quoted form of ] setting that puts the path in quotes.
APPLE SCRIPT EXAMPLE:
set pathToApp to POSIX path of ((path to me) as text)
set dragonFrame to ("_DRAGONFRAME/")
set INSTALLER_PATH to quoted form of pathToApp & dragonFrame
display dialog INSTALLER_PATH
GENERATES THIS PATH {notice the ticks}:
'/Volumes/Free.Space/Shotgun Python Dragon Project 2017/DRAGONFRAME_SCRIPTS_MASTER_V1.02/Dragonframe_Scripts_Installer.app/'_DRAGONFRAME/
Instead of testing this through the Apple Script to find the exact syntax I would need, I figured it would be easier to test this from command line. This is exactly what I'm setting and executing from my Terminal command line. In the examples below, executing [ 'display dialog' ] works and [ 'do shell script' ] fails. I think if there's a way to enclose the entire path in double quotation marks, my problem would be solved although all attempts at getting the path enclosed in double quotation marks has failed:
%> INSTALLER_PATH='/Volumes/Free.Space/Shotgun Python Dragon Project 2017/DRAGONFRAME_SCRIPTS_MASTER_V1.02/Dragonframe_Scripts_Installer.app/'_DRAGONFRAME/
%> ADMIN_USER_PROC="_Python_PySide_QT_Installer/Scripts/AdminUserProcesses.sh"
%> osascript -e 'do shell script ("'"${INSTALLER_PATH}"'" & "'"${ADMIN_USER_PROC}"'")'
GENERATES THIS ERROR:
0:217: execution error: sh: /Volumes/Free.Space/Shotgun: No such file or directory (127)
%> osascript -e 'display dialog ("'"${INSTALLER_PATH}"'" & "'"${ADMIN_USER_PROC}"'")'
GENERATES THIS PATH:
/Volumes/Free.Space/Shotgun Python Dragon Project 2017/DRAGONFRAME_SCRIPTS_MASTER_V1.02/Dragonframe_Scripts_Installer.app/_DRAGONFRAME/_Python_PySide_QT_Installer/Scripts/AdminUserProcesses.sh
Thank you in advance for any help you can offer.
I think you're looking for something similar to this syntax:
set dragonFrame to ("/_DRAGONFRAME/")
set INSTALLER_PATH to path of (pathToApp & dragonFrame)
Result:
'/Volumes/Free.Space/Shotgun Python Dragon Project 2017/DRAGONFRAME_SCRIPTS_MASTER_V1.02/Dragonframe_Scripts_Installer.app/_DRAGONFRAME/'
If you need double-quotes around the path you can use:
("\"" & pathToApp & dragonFrame & "\"")
and get rid of the quoted form argument leaving you with:
set INSTALLER_PATH to ("\"" & pathToApp & dragonFrame & "\"")

Calling a VBScript using arguments

I am trying to get a VBScript to launch another VBScript while passing an argument from the first one to the second one. I got the part of how to receive the argument on the second script, however I have no clue how to call it within the first VBScript. Here is what I currently have in the first one:
arg1 = "MyArgument"
objShell.Run "ArgumentTest2.vbs arg1"
When I run the this script, it gives me the error message:
Script: C:\Argument Test 1.vbs
Line: 2
Char: 1
Error: Object required 'objShell'
Code: 800A01A8
Source: Microsoft VBScript runtime error
Any help would be greatly appreciated! Thanks!
You need to initialize objShell with
Set objShell = CreateObject("WScript.Shell")
before you can use its .Run method.
Also, VBScript doesn't expand variables inside strings, so you'll need to concatenate your argument to the rest of the command string:
objShell.Run "ArgumentTest2.vbs " & arg1
Note that you'll need to put tokens in double quotes if they contain spaces:
arg1 = "My Argument"
objShell.Run "ArgumentTest2.vbs """ & arg1 & """"

Passing Multiple Parameters from Applescript to Terminal Command Script

I have been trying to figure out how to pass multiple parameters from an Applescript to a Terminal Command Script. For example when running a terminal command file you are able to receive parameters programatically like so:
#!/bin/bash
var=$1
var=$2
The Applescript Code that I have been working with is below for reference:
tell application "System Events" to set app_directory to POSIX path of (container of (path to me))
set thisFile to "Dev"
set testTarget to "/Users/lab/Desktop/TestTarget/"
do shell script "/Users/lab/Desktop/TempRoot/mycommand.command " & thisFile & testTarget with administrator privileges
Where I think I have gone wrong is the input of the second parameter. When I only had one parameter it went through just fine:
do shell script "/path/to/command/mycommand.command" &var with administrative privileges
I am curious as to what the correct syntax would be for passing in this second parameter. If anybody has any suggestions please let me know! Also if you need more information I would be happy to provide it!
You just need to add a space between your arguments. Right now, there is no space being added between thisFile and testTarget. Your command looks like this:
/Users/lab/Desktop/TempRoot/mycommand.command Dev/Users/lab/Desktop/TestTarget/
Change your shell script line to:
do shell script "/Users/lab/Desktop/TempRoot/mycommand.command " & thisFile & space & testTarget with administrator privileges
Something that I find helpful when building a script is to make sure my shell commands are correct before running them. So instead of building it directly, store the command in a variable and log it. Later, replace the logging statement with the do shell script command.
set shellScript to "/Users/lab/Desktop/TempRoot/mycommand.command " & thisFile & space & testTarget with administrator privileges
log shellScript
-- do shell script shellScript

Resources