Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, March 25, 2016 12:25 PM
Hi, I want to store the output of a PowerShell cmdlet(invoked from a java code) in a variable.
For example, the cmdlet
"get-vm xyz | select -ExpandProperty VMID"
give output as,
"Guid
3258c4ee-d162-4d41-ad99-d65cb7970781"
I want to store this id(i.e. 3258c4ee-d162-4d41-ad99-d65cb7970781) as a string variable in my java code.
Is this possible?
Please do help.
Thanks in advance.
All replies (4)
Friday, March 25, 2016 12:28 PM ✅Answered
Hi,
You could try something like this to get the GUID:
(Get-VM xyz | Select -ExpandProperty VMID).Guid
As for java interaction, I have no idea and can't offer any suggestions there.
Monday, March 28, 2016 9:53 AM ✅Answered
Hi Mike,
Thanks for the help, it worked!
Here's how I used it:
String command = "powershell.exe (Get-VM "+name_vm+" | Select -ExpandProperty VMID).Guid";
Process powerShellProcess = Runtime.getRuntime().exec(command);
Thanks again!
Monday, March 28, 2016 9:41 AM
Hi,
Maybe, you could directly execute PowerShell Commands in Java Program.
Here is a similar thread which may be helpful and you could take a look and use for reference:
http://stackoverflow.com/questions/29545611/executing-powershell-commands-in-java-program
Please Note that since the web sites are not hosted by Microsoft, the links may change without notice. Microsoft does not guarantee the accuracy of this information.
Besides, the issue is also about Java, I would suggest that you could post your questions in the related forum. The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.
Regards,
Wendy
Please remember to mark the replies as answers if they help and un-mark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact [email protected].
Monday, March 28, 2016 11:39 AM
Cheers, you're very welcome. Glad it worked out.