C# を PowerShell で実行 : メッセージボックスの応答結果をファイルに書き込む ( バッチファイルで利用可能 )

まずは動作確認の為、VisualStudio でコードを完成させます。
01.using System;
02.using System.IO;
03.using System.Windows.Forms;
04. 
05.namespace MessageBoxConsole
06.{
07.        class Program
08.        {
09.                static void Main(string[] args)
10.                {
11.                        // テンポラリフォルダのパスを取得
12.                        string path = Environment.GetEnvironmentVariable("temp");
13. 
14.                        // 書き込むファイルのフルパスを編集する
15.                        string writePath = $@"{path}\_check_result";
16. 
17.                        // MessageBox を表示する( System.Windows.Forms の参照が必要です )
18.                        // ※ OK か Cancel のボタンが表示されます
19.                        DialogResult check = MessageBox.Show("実行しますか?", "確認", MessageBoxButtons.OKCancel);
20.                        using (StreamWriter sw = new StreamWriter(writePath, false))
21.                        {
22.                                // OK ならば、ファイルに 1 を書き込みます
23.                                if (check == DialogResult.OK)
24.                                {
25.                                        sw.Write("1");
26.                                }
27.                                // CANCEL ならば、ファイルに 0 を書き込みます
28.                                else
29.                                {
30.                                        sw.Write("0");
31.                                }
32. 
33.                                // ファイルを閉じます
34.                                sw.Close();
35. 
36.                        }
37.                }
38.        }
39.}

▼ System.Windows.Forms の参照


次に PowerShell のスクリプトを作成して C# のコードを実行させます。その為に、PowerShell でスクリプトの実行を許可する手順 で設定を済ませておきます。

msgbox.ps1
01.$code = @"
02.using System;
03.using System.IO;
04.using System.Windows.Forms;
05.public class MyClass {
06.        public static void Main() {
07. 
08.                // テンポラリフォルダのパスを取得
09.                string path = Environment.GetEnvironmentVariable("temp");
10. 
11.                // 書き込むファイルのフルパスを編集する
12.                string writePath = string.Format( @"{0}\_check_result", path );
13. 
14.                // MessageBox を表示する( System.Windows.Forms の参照が必要です )
15.                // ※ OK か Cancel のボタンが表示されます
16.                DialogResult check = MessageBox.Show("実行しますか?", "確認", MessageBoxButtons.OKCancel);
17.                using (StreamWriter sw = new StreamWriter(writePath, false))
18.                {
19.                        // OK ならば、ファイルに 1 を書き込みます
20.                        if (check == DialogResult.OK)
21.                        {
22.                                sw.Write("1");
23.                        }
24.                        // CANCEL ならば、ファイルに 0 を書き込みます
25.                        else
26.                        {
27.                                sw.Write("0");
28.                        }
29. 
30.                        // ファイルを閉じます
31.                        sw.Close();
32. 
33.                }
34. 
35.        }
36.}
37."@
38. 
39.Add-Type -Language CSharp -TypeDefinition $code -ReferencedAssemblies ("System.Windows.Forms")
40. 
41.[MyClass]::Main()

テキストファイルに結果を出力しているので、以下のようなバッチファイルで利用する事ができます
01.echo off
02. 
03.echo 処理を開始しました
04. 
05.powershell .\msgbox.ps1
06. 
07.FOR /F %%i IN (%temp%\_check_result) DO (
08.        if "%%i"=="1" goto :ok
09.        if "%%i"=="0" goto :cancel
10.)
11. 
12.:ok
13.echo OK を選択しました
14.goto :end
15. 
16.:cancel
17.echo Cancel を選択しました
18.goto :end
19. 
20. 
21.:end