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";

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 TeneAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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?
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'

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

SD-WAN: Making It Work for You

As bandwidth requirements and Internet costs grow, businesses naturally want to manage budgets by reducing reliance on their most expensive connection types. Learn more about how to make SD-WAN work for your business in our on-demand webinar!

Tal TeneAuthor Commented:
Guy LidbetterCommented:
Yup, That's incorrect. I just did what I told you not to do... lol - I forgot to remove the "set-"


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

Open in new window

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.

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

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

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):

Can I send you the script? Maybe the problem is there?

Thanks a lot!
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.


Tal TeneAuthor Commented:

 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....
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.


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!
Tal TeneAuthor Commented:
Because it simply works...
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

From novice to tech pro — start learning today.