header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Invoke Windows PowerShell from a Java program
Last Post 19 Jul 2010 07:10 AM by sp. 3 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
JLeeUser is Offline
New Member
New Member
Posts:1
Avatar

--
04 May 2009 08:35 PM
    Hi there,
     
    I am writing a Java application and would like to use PowerShell to do certain things for me.

    I have tried to use Java's "ProcessBuilder" to start the PowerShell with "C:\WINDOWS\system32\WindowsPowerShell\V1.0\powershell.exe -NoExit". When the PowerShell process started, I used Java's "Process" object to get to the stdin, stdout and stderr of the PowerShell process that was started.
     
    I was hoping that I could write to the stdin of PowerShell to tell it to run cmdlet and then I could read from stdout or stderr to get the result or error message. In other words, I was thinking to use PowerShell by simply sending commands through its stdin and getting results from its stdout/stderr.
     
    But it did not work for me. After I had sent the command, I could not get any result from either stdout or stderr. The read attempt on stdout and stderr just blocked forever. I had also tried to run a cmdlet and redirect its output to a disk file. It did not work either, the disk file was not created. It seemed to me, the command could not be sent to PowerShell through its stdin. Please advice what I have done wrong.

    I also tried C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -command "& {Get-Process | Out-File C:\test__.txt}" using "ProcessBuilder". It did not work. It seemed that the PowerShell process had been started but the command had not run. When runing C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -command "& {Get-Process | Out-File C:\test__.txt}" from start/run, it worked.

    Also, any suggestion on how to use PowerShell from a Java program?

    Thanks heaps

    J Lee
    spUser is Offline
    New Member
    New Member
    Posts:5
    Avatar

    --
    19 Jul 2010 03:55 AM
    Hi Lee,

    I was also facing the same issue. Not able to run the powershell commands from java code.

    If you get any input please share with me.

    Thanks

    SP
    PoSherLifeUser is Offline
    Basic Member
    Basic Member
    Posts:364
    Avatar

    --
    19 Jul 2010 06:23 AM
    this would be more cumbersome, but you could create a single .ps1 file with functions and use the -file file.ps1 args

    ### begin commands.ps1 ###
    param (
    [switch]$process
    )
    function process {
    Get-Process | Out-File C:\test__.txt
    }
    ### end commands.ps1 ###

    then from your java script run:
    "C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -file c:\script\commands.ps1 process"
    When at first you don't succeed Step-Into

    http://theposherlife.blogspot.com
    http://www.jandctravels.com

    spUser is Offline
    New Member
    New Member
    Posts:5
    Avatar

    --
    19 Jul 2010 07:10 AM
    Hi Cruisader03,

    This is shiva. I face the same issue what Lee facing.

    I am not suppose to create any ps1 files. All the powershell commands i have to execute from java code.

    Example:

    Process proc0 = Runtime.getRuntime().exec("powershell.exe -ExecutionPolicy Unrestricted");
    System.out.println(proc0);
    proc0.getOutputStream().close();
    Process proc1 = Runtime.getRuntime().exec("PowerShell.exe $LiveCred = New-Object System.Management.Automation.PSCredential <>, ConvertTo-SecureString <> -AsPlainText -Force");
    System.out.println(proc1);
    proc1.getOutputStream().close();

    Process proc2 = Runtime.getRuntime().exec("PowerShell.exe $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection");
    proc2.getOutputStream().close();

    Process proc3 = runtime.exec("PowerShell.exe Import-PSSession $Session");
    proc3.getOutputStream().close();

    System.out.println("Before MailBox Create statement");

    Process p = runtime.exec("PowerShell.exe New-Mailbox -Name <> -WindowsLiveID <> -Password (ConvertTo-SecureString -String <> -AsPlainText -Force) -RemotePowerShellEnabled $false -FirstName <> -LastName <>");
    p.getOutputStream().close();

    Please suggest.

    Thanks

    SP
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer