Method: RightScale::Platform::Shell#format_powershell_command4
- Defined in:
- lib/right_agent/platform/windows/platform.rb
#format_powershell_command4(powershell_exe_path, lines_before_script, lines_after_script, shell_script_file_path, *arguments) ⇒ String
Formats a powershell command using the given script path and arguments. Allows for specifying powershell from a specific installed location. This method is only implemented for Windows.
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 |
# File 'lib/right_agent/platform/windows/platform.rb', line 1329 def format_powershell_command4(powershell_exe_path, lines_before_script, lines_after_script, shell_script_file_path, *arguments) # special case for powershell scripts. escaped = [] [shell_script_file_path, arguments].flatten.each do |arg| value = arg.to_s # note that literal ampersand must be quoted on the powershell command # line because it otherwise means 'execute what follows'. escaped << ((value.index(' ') || value.index('&')) ? "'#{value.gsub("'", "''")}'" : value) end # resolve lines before & after script. defaulted_lines_after_script = lines_after_script.nil? lines_before_script ||= [] lines_after_script ||= [] # execute powershell with RemoteSigned execution policy. the issue # is that powershell by default will only run digitally-signed # scripts. # FIX: search for any attempt to alter execution policy in lines # before insertion. # FIX: support digitally signed scripts and/or signing on the fly by # checking for a signature file side-by-side with script. lines_before_script.insert(0, 'set-executionpolicy -executionPolicy RemoteSigned -Scope Process') # insert error checking only in case of defaulted "lines after script" # to be backward compatible with existing scripts. if defaulted_lines_after_script # ensure for a generic powershell script that any errors left in the # global $Error list are noted and result in script failure. the # best practice is for the script to handle errors itself (and clear # the $Error list if necessary), so this is a catch-all for any # script which does not handle errors "properly". lines_after_script << 'if ($NULL -eq $LastExitCode) { $LastExitCode = 0 }' lines_after_script << "if ((0 -eq $LastExitCode) -and ($Error.Count -gt 0)) { $RS_message = 'Script exited successfully but $Error contained '+($Error.Count)+' error(s):'; write-output $RS_message; write-output $Error; $LastExitCode = 1 }" end # ensure last exit code gets marshalled. marshall_last_exit_code_cmd = 'exit $LastExitCode' if defaulted_lines_after_script || (lines_after_script.last != marshall_last_exit_code_cmd) lines_after_script << marshall_last_exit_code_cmd end # format powershell command string. powershell_command = "&{#{lines_before_script.join('; ')}; &#{escaped.join(' ')}; #{lines_after_script.join('; ')}}" # in order to run 64-bit powershell from this 32-bit ruby process, we need to launch it using # our special RightRun utility from program files, if it is installed (it is not installed for # 32-bit instances and perhaps not for test/dev environments). executable_path = powershell_exe_path executable_arguments = ['-command', powershell_command] executable_path, executable_arguments = format_right_run_path(executable_path, executable_arguments) # combine command string with powershell executable and arguments. return format_executable_command(executable_path, executable_arguments) end |