Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invocation of PS scripts from jPowerShell fails when the script path has space in it #58

Open
senarha opened this issue Mar 22, 2019 · 4 comments
Assignees

Comments

@senarha
Copy link

senarha commented Mar 22, 2019

First of all thanks for putting the awesome library in place to suffice the requirement of invoking PS from Java. It provide almost all required api method which are very much required for any application to leverage and execute the PS script. Thanks once again.

We are using jPowerShell library to execute PS scripts from Java layer. We are using tomcat application which is installed in custom drive (non- OS installed eg: M:) with path to java.io.tmp.dir containing space in it (For eg: -Djava.io.tmp.dir="M:\Program Files (x86)\Tomcat)

Thus, When we invoke the executeScript method of jPowerShell script, while creating temp scripts using File.createTempFile("psscript_" + (new Date()).getTime(), ".ps1"); in PowerShell.java, invocation of PS scripts fails saying cannot locate script path with " (x86) ..."

Please note that as we are shipping jPowerShell along with our application to the customers who already have installed tomcat in it, we cannot recommend customer to enable the short path naming convention for the non-OS drive and changing the java.io.tmp.dir in java_opts might cause failure in application.

Please note that this is not an issue with application installed in OS drive with short name flag enable because they uses the short names instead of long path.

For eg: With Shortname enabled, the path would be similar to this
Djava.io.tmpdir=C:\PROGRA2\TEST\TOMCATLIC1

Recommendation:

If the jPowershell Library handles and accept the temp directory path while creating temp file, we can pass different path based on the input fed from user and create temp file in it. Please share your thoughts.

@senarha
Copy link
Author

senarha commented Mar 22, 2019

Here is the sample code :

PowerShellResponse response = powerShell.configuration(config).executeScript("C:/temp/OSCompat.ps1", "input1");

However in PowerShell class, the executeScript method internally calls this statement :
File tmpFile = this.createWriteTempFile(srcReader); <-- This private method uses File.createTempFile method to create the temp file under the temp directory based on java.io.tmp.dir variable.

//Create temp file method
createWriteTempFile(BufferedReader srcReader) {
...
File.createTempFile("psscript_" + (new Date()).getTime(), ".ps1"); <-- If we allow to pass the directory as in which case it wont default to java.io.tmp.dir, we can add this as input so that the temp file gets created from the path that user provides. If not available, then we can default to use java.io.tmp.dir as it is today.
...
}

@profesorfalken
Copy link
Owner

Hello,

So if I have undertood well, in your case you are overriding the java.io.tmp.dir, using a path with spaces and that makes jPowershell fail when trying to create the temp file.

So what you need is just an optional parameter in executeScript to override the temp directory:

public PowerShellResponse executeScript(BufferedReader srcReader, String params ,String tmpDirectory) {
[...]
tmpFile = File.createTempFile("psscript_" + new Date().getTime(), ".ps1", tmpDirectory);

Can you confirm it?

Thanks for your feedback.

@profesorfalken profesorfalken self-assigned this Mar 31, 2019
@senarha
Copy link
Author

senarha commented Apr 3, 2019

Yes @profesorfalken . We want to customize the default temp directory location from java.io.tmp.dir to something else like ProgramData or %temp% path. So in such cases honouring the tempDirectory location in the existing framework would ease and suffice our requirement.

Thanks.

@profesorfalken
Copy link
Owner

Hi again @senarha,

You should be able to do it using the 3.1.0 version of jPowershell.

You can configure the temp path programmatically:

Map<String, String> myConfig = new HashMap<>();
myConfig.put("tempFolder", "c:\\myTempFolder");
response = powerShell.configuration(myConfig).executeCommand("Get-WmiObject Win32_BIOS");

Or just placing a configuration file called jpowershell.properties into your classpath with the content:

tempFolder=c:\\myTempFolder

Can you try it?

Best regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants