Sidebar

How can I run a Windows Powershell script from within QIE?

0 votes
25 views
asked Sep 15 by david-f-5427 (1,600 points)
I'd like to run an external Windows Powershell script from a scheduled script from within QIE. How can that be accomplished?

1 Answer

0 votes

In the body of the scheduled script, you can use Java's ProcessBuilder to execute your PowerShell script, like this:

var processBuilder = new java.lang.ProcessBuilder(
   "c:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
   "-File",
   "c:\\temp\\helloWorld.ps1"
);
processBuilder.start();

If needed, you can wait for the external process to complete

var process = processBuilder.start();
var result = process.waitFor();

//if result != 0, then there was an error executing the PS script

You can check the return value of waitFor() for more specific information on the PowerShell invocation. See https://docs.oracle.com/javase/8/docs/api/java/lang/Process.html for more info on the Java Process class.

Note: Depending on the workstation's configured policies, you _may_ need to also fire up gpedit.msc (Group Policy Editor) and navigate to either

    Computer Configuration\Administrative Templates\Windows Components\Windows PowerShell
    or 
    User Configuration\Administrative Templates\Windows Components\Windows PowerShell

and adjust "Turn On Script Execution", its "Not Configured"/"Enabled"/"Disabled" property and "Execution Policy", in accordance with your network security guidelines.

If process.waitFor() doesn't return 0, you can add the following to the top of your PS script get insights into what may be failing behind the scenes:

Start-Transcript -Append 'c:\temp\yourlogfile.txt'

‚ÄčIf the script fails, check this log file for additional debugging information. Often times, running a PS script locally from a Powershell prompt will be running under a different user than the QIE service is configured to run under. This can cause exceptions when accesing remote resources like files, folders or databases.

answered Sep 15 by david-f-5427 (1,600 points)
...