一、目的
1.powershell能簡單寫一些小腳本,不需要exe開發這么笨重。
2.在windows實現某個特定功能,做成一個exe能方便查看管理。
二、實現
1.C# code 運行結束加入返回值
Environment.ExitCode = 1; //自定義數字
2.powershell 調用并獲取
需要增加 -PassThru
$proc = Start-Process test.exe -ArgumentList "testarg1 testarg2" -Wait -PassThru
$proc.ExitCode# ArgumentList 可以使用變量
$paramList=@("testarg1", "testarg2")
注意:
1.使用 Start-Process運行,當前窗口是test.exe
2.如果不想影響窗口焦點獲取,不使用Start-Process,直接運行。代價就是不能獲取返回值,考慮exe文件內部生產txt log 然后powershell通過讀取log來判斷結果。
參考:(2條消息) Powershell Start-Process 后被調用程序的返回值_Vizer0-0的博客-CSDN博客https://blog.csdn.net/z1012178837/article/details/103716847