Link to home
Start Free TrialLog in
Avatar of Tal Tene
Tal Tene

asked on

Powershell script needs "warm up" to run from java code

Hi all,

 I need to run ps scripts from my java code in order to set performance monitor counters value.
 For that I use the following line:

 
final String PS_COMMAND = "Powershell.exe -executionpolicy Unrestricted -File .\\myscript.ps1";
Runtime.getRuntime().exec(PS_COMMAND);

Open in new window


 The problem is, that unless I first run the script from the ISE, it wouldn't set the counter value from the code, and from my perspective, the script simply won;t execute or is not usefull from the java code.

 How can I fix this stauts?

Tal
Avatar of Guy Lidbetter
Guy Lidbetter
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi Tal,

You may find that the execution policy is preventing you from running the script from within java.
Try setting your execution policy to unrestricted in your profile and have another go.

Set-ExecutionPolicy Unrestricted

Open in new window


Also, have you considered a batch file using logman to start a perfmon?
Avatar of Tal Tene
Tal Tene

ASKER

Hi Guy,

 I tried your proposal.

Powershell.exe Set-executionpolicy Unrestricted -File .\\perfmon_create_jcm_counters.ps1

Open in new window


 Now I get:

Set-ExecutionPolicy : A parameter cannot be found that matches parameter name 'File'

Thanks!
Hang on, did you run it in an Admin powershell locally? or did you update the PS1 script call?

That error tells me you probably updated the script to something like this:
final String PS_COMMAND = "Powershell.exe set-executionpolicy Unrestricted -File .\\myscript.ps1";

That would be incorrect as you are calling the set-executionpolicy cmdlet which has no -file argument. try this instead

final String PS_COMMAND = "Powershell.exe set-executionpolicy ByPass -File .\\myscript.ps1";

Open in new window

User generated image
Yup, That's incorrect. I just did what I told you not to do... lol - I forgot to remove the "set-"

Updated

final String PS_COMMAND = "Powershell.exe -executionpolicy ByPass -File .\\myscript.ps1";

Open in new window

Guy, thank you for your effort to assist me.

I executed the command like in the image.
The script is supposed to set a counter on Perfmon.
It didn't crash, but it also didn't set the counter, as until now.

User generated image
Alright, lets try this slightly differently.

Replace the string with this...

final String PS_COMMAND = "cmd /c powershell -ExecutionPolicy RemoteSigned -noprofile -noninteractive -File .\\myscript.ps1";

Open in new window

Executed it - counter was not set...

User generated image
Just to make sure that running the file from the ISE does the job, I did it and here are the results (on the right):

User generated image
Can I send you the script? Maybe the problem is there?

Thanks a lot!
Are you sure powershell script is being called? I would suggest you create a PS1 with
New-Item c:\Temp\new_file.txt -type file

Open in new window

Rerun the Java applet and make sure once it completes the file has been created.

You are welcome to send the script, I'll happily have a look for you.
Drop me a PM and I'll send you my email address to send the script to.

Please make sure there is no confidential info in it.

Regards

Guy
Guy,

 It is porved to be run from Java once I executed the script first from the ISE as I described on the first post.
 Once I execute one script on the ISE, the java code executes the scripts with no trouble at all....
ASKER CERTIFIED SOLUTION
Avatar of Tal Tene
Tal Tene

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Tal,

Glad you sorted this out. What you needed was to either use the -noexit flag or close the stream from within java after calling the ps script.

All the best!
Because it simply works...