6
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PowerShell から バッチファイルを実行して、ステータスや、標準出力を得る

Posted at

PowerShell からバッチファイルを実行して終了ステータスをとるという単純なことを実行するのに、意外にくっそはまったので、もうにどとはまらないように書いておく。

コマンドの直接実行が一番

PowerShell からコマンドを実行する場合は、start-processなどは介せず直接実行がよい。

最初こんなものを書いていたのだが、これが全くうまくいかない。下記のコードでは、Standard Outputなどが出力されない。.NET の Process を使う方法もあるが面倒なので直接実行がよさげだ。

function StopOnFailedExecution($exitCode) {
  if ($exitCode -Ne 0) 
  { 
    Write-Host $exitCode
    exit $exitCode 
  }
}

$result=start-process "cmd.exe" "/c .\Test.bat" -Wait -PassThru -NoNewWindow
$stdout=$result.StandardOutput
$stderr=$result.StandardError
Write-Host $stdout
Write-Host $stderr
StopOnFailedExecution($result.ExitCode)
Write-Host "success"
echo Hello!
exit 1

PowerShell ISE はBOMに注意

当初私は、Test.bat の編集を PowerShell ISE で実施していたが、これが悲劇を生んだ。PowerShell ISE は ファイルを BOM 付きで保存するのだが、cmd.exe は BOM を解釈しないようだ。下記のようなエラーになる。解決のためには、VSCode 等で

C:\Users\tsushi\Code\spike\PowerShell\TestExit.ps1

C:\Users\tsushi\Code\spike\PowerShell>echo Hello! 
cmd.exe : 'echo' is not recognized as an internal or external command,
At C:\Users\tsushi\Code\spike\PowerShell\TestExit.ps1:8 char:1
+ cmd.exe /c ".\Test.bat"
+ ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: ('echo' is no...ternal command,:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
operable program or batch file.

VScode 等で、bom なしにすると動作する
image.png

function StopOnFailedExecution {
  if ($LASTEXITCODE) 
  { 
    Write-Host $LASTEXITCode
    exit $LASTEXITCODE 
  }
}
cmd.exe /c ".\Test.bat"
StopOnFailedExecution($result.ExitCode)
Write-Host "success"

Result

> C:\Users\tsushi\Code\spike\PowerShell\TestExit.ps1

C:\Users\tsushi\Code\spike\PowerShell>echo Hello! 
Hello!

C:\Users\tsushi\Code\spike\PowerShell>exit 1 
1
6
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?