[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 /** 2 * GENERAL 3 * ======= 4 * Author : Rainer Meier <skybeam (at) users.sourceforge.net> 5 * Version: 1.0 6 * License: GPLv2 7 * 8 * DESCRIPTION 9 * =========== 10 * This script allows execution of another script (given by parameter) in 11 * elevated privileges mode on Windows Vista with UAC activated. 12 * 13 * This is required as there seems to be no possibility to run a batch/cmd 14 * script on Windows Vista with elevated rights. Even if you right-click a 15 * cmd script and select to run it as Administrator it does not get elevated 16 * rights. 17 * 18 * USAGE 19 * ===== 20 * This script here just allows you to call any script (batch, cscript ...) 21 * with elevated rights. 22 * 23 * NOTE: To run WPKG in elevated mode you can either write a batch file which 24 * is calling 'cscript \\path\to\wpkg.js ...' or run cscript directly by this 25 * script. 26 * Running batch file: 27 * <script> \\path\to\<batch-name.cmd> [optional arguments] 28 * Running cscript: 29 * <script> cscript \\path\to\wpkg.js <argument1> [argument2 [argument3 [...]]] 30 * 31 * NOTE: Running cscript directly does not allow you to set custom environment 32 * variables like the common SOFTWARE variable! Running this script here from a 33 * batch script which is setting the variables will not work as the environment 34 * of the child process which is run in elevated mode is reset to the windows 35 * defaults. 36 * So the following execution chain won't work (wpkg.js will not see the 37 * exported SOFTWARE variable): 38 * <set SOFTWARE variable> => execute-elevated.js => (elevated) wpkg.js 39 * Only the following will work: 40 * execute-elevated.js => (elevated) <batch to set SOFTWARE> => wpkg.js 41 * This also works with the exisiting wrapper: 42 * execute-elevated.js => (elevated) <batch to set SOFTWARE> => wrapper.js 43 * 44 * Here is some sample code you can use for your own batch file. I use this one 45 * to install all software which belongs to a specific profile to the current 46 * machine. No matter what its name is - I always use the same profile. This is 47 * useful for example if you need to upgrade your default bunch of software on 48 * a customer machine. 49 * 50 * @echo off 51 * echo Running WPKG 52 * set WPKG_PATH=\\software\RemInst\wpkg\wrapper.js 53 * set SOFTWARE=\\software\RemInst\software 54 * set SETTINGS=\\software\RemInst\settings 55 * set PROFILE=autosetup 56 * 57 * start /wait "wpkg" cscript "%WPKG_PATH%" /synchronize /profile:%PROFILE% 58 * 59 * 60 * ADDITIONAL NODES 61 * ================ 62 * Please note that using this script here is not required when launching 63 * WPKG from WPKG client (as a system service) as the system service does 64 * not require to elevate its privileges. This script is only required if you 65 * want to MANUALLY invoke wpkg.js/wrapper.js on a Windows Vista system with 66 * UAC enabled. 67 * 68 * Also note that the paths to the scripts specified should be UNC paths as 69 * elevated programs cannot access network drives which are eventually 70 * available when launching this script. So don't use something like 71 * Z:\wpkg\wpkg.js but use \\server\share\wpkg\wpkg.js instead. 72 */ 73 function showUsage() { 74 var scriptName = WScript.ScriptName; 75 var message = "\ 76 Usage: \n"; 77 message += scriptName + "<command> [[argument1 [argument2 ...]]]\n\n"; 78 message += "\ 79 Example:\n"; 80 message += scriptName + " \\\\path\\"+"to\\my-custom-wpkg-caller.cmd\n" 81 message += scriptName + " \\\\path\\"+"to\\my-custom-wpkg-caller.cmd /synchronize\n"; 82 message += scriptName + " cscript \\\\path\\"+"to\\wpkg.js /synchronize"; 83 WScript.Echo(message); 84 } 85 86 /** 87 * Call the main function with arguments while catching all errors and 88 * forwarding them to the error output. 89 */ 90 try { 91 main(); 92 } catch (e) { 93 var message = "Message: " + e.message + "\n" + 94 "Description: " + e.description + "\n" + 95 "Error number: " + hex(e.number) + "\n" + 96 "Stack: " + e.stack + "\n" + 97 "Line: " + e.lineNumber; 98 WScript.Echo(message); 99 WScript.Quit(2); 100 } 101 102 /** 103 * Main execution method. Actually runs the script 104 */ 105 function main() { 106 // get arguments 107 var argv = WScript.Arguments; 108 109 // check argument count 110 if ( argv.length < 1 ) { 111 showUsage(); 112 WScript.Quit(1) 113 } 114 115 // parse command and arguments 116 var command = argv(0); 117 var args = ""; 118 for (var i=1; i<argv.length; i++) { 119 args += " " + argv(i); 120 } 121 122 // evaluate working directory - command is run from that directory 123 var fso = new ActiveXObject("Scripting.FileSystemObject"); 124 var path = WScript.ScriptFullName; 125 workdir = fso.GetParentFolderName(path); 126 127 var shell = new ActiveXObject("WScript.Shell"); 128 command = shell.ExpandEnvironmentStrings(command); 129 args = shell.ExpandEnvironmentStrings(args); 130 131 // var shellExec = shell.exec(cmd); 132 var appShell = new ActiveXObject("Shell.Application"); 133 var shellExec = appShell.ShellExecute(command, args, workdir, "runas"); 134 } 135 136 /** 137 * User-defined function to format error codes. 138 * VBScript has a Hex() function but JScript does not. 139 */ 140 function hex(nmb) { 141 if (nmb > 0) { 142 return nmb.toString(16); 143 } else { 144 return (nmb + 0x100000000).toString(16); 145 } 146 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Tue Mar 17 22:47:18 2015 | Cross-referenced by PHPXref 0.7.1 |