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
Tal TeneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy LidbetterCommented:
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?
0
Tal TeneAuthor Commented:
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!
0
Guy LidbetterCommented:
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

0
ON-DEMAND: 10 Easy Ways to Lose a Password

Learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees in this on-demand webinar. We cover the importance of multi-factor authentication and how these solutions can better protect your business!

Tal TeneAuthor Commented:
ScreenHunter-02-Jul.-16-16.13.jpg
0
Guy LidbetterCommented:
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

0
Tal TeneAuthor Commented:
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.

ScreenHunter-04-Jul.-16-16.36.jpg
0
Guy LidbetterCommented:
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

0
Tal TeneAuthor Commented:
Executed it - counter was not set...

ScreenHunter-05-Jul.-16-18.50.jpg
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):

ScreenHunter-06-Jul.-16-18.51.jpg
Can I send you the script? Maybe the problem is there?

Thanks a lot!
0
Guy LidbetterCommented:
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
0
Tal TeneAuthor Commented:
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....
0
Tal TeneAuthor Commented:
To Guy and everyone who stumbles upon this thread...

I have managed to get to a WA solution:

The magic is calling the script the first time using -noexit flag, and I call it from a seperate thread, because the ps process runs the scripts and then wait on the PS prompt.

Also, I run it using -windowStyle hidden to hide the thread window.

Thanks!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Guy LidbetterCommented:
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!
0
Tal TeneAuthor Commented:
Because it simply works...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.