0

Java コンソール アプリから powershell スクリプトを実行しようとしていますが、以下のコマンドでこれを機能させることができました。

Process p = Runtime.getRuntime().exec("cmd.exe /c start cd "+dir+" & start cmd.exe /k \"Powershell C:\\runscript.ps1 args\"",null,dir);
p.waitFor();

私のpowershellスクリプトの中に、ループで呼び出される以下のスニペットがあります。

    Start-Process -FilePath $RunLocation -ArgumentList $args -wait -NoNewWindow -RedirectStandardOutput $OutputFile

$output = Get-Content $OutputFile| out-string   
    if(!($output.toLower().contains("failed"){
        Remove-Item $outLogFile
}

コマンドプロンプトを開いて、exec(...)コマンドにあるものを正確にコピーすると、うまく動作しますが、Javaアプリケーションで実行すると-wait、powershellスクリプトが無視されているように見え、次の行(ログのチェックと削除) が実行された後、powershell で数秒間スリープを追加することさえしましたがStart-Process、これは機能しますが、より良い方法があることを願っています。

私が得ているエラーはpowershellスクリプトにあります(これはJavaアプリから実行した場合にのみ発生します.-waitは、コマンドプロンプトから直接実行した場合に続行する前に、開始プロセスが終了するのを待ちます..):

Remove-Item : Cannot remove item D:\adhoc\logs\2016-06\output38844448.out: The process cannot access the file
'D:\adhoc\logs\2016-06\output38844448.out' because it is being used by another process.
At C:\runscript.ps1:91 char:3
+         Remove-Item $OutputFile
+         ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (D:\adhoc\log...4448.out:FileInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand

-waitを使用して Java アプリから実行すると、powershell スクリプトが機能しないのはなぜruntime().execですか?

4

0 に答える 0