JScriptからダイアログを出す（Powershell、Windows.Forms利用）

## 例としてログインダイアログ
![login.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/252926/cbaea0f0-25fb-5280-8c3f-c186c5f10d7d.png)

```login.js
var echo = function (str) {
    WScript.Echo(str);
    // console.log(str)
};

var quit = function () {
    WScript.Quit();
};

var CredentialDialog = function () {
    var code = '\
Add-Type -AssemblyName System.Windows.Forms\r\n\
Add-Type -AssemblyName System\r\n\
\r\n\
$form = New-Object Windows.Forms.Form\r\n\
$form.Size = New-Object Drawing.Size @(400, 200)\r\n\
$form.ControlBox = $false\r\n\
$form.MinimizeBox = $false\r\n\
$form.MaximizeBox = $false\r\n\
$form.FormBorderStyle = [Windows.Forms.FormBorderStyle]::FixedDialog\r\n\
$form.Text = "ログイン"\r\n\
\r\n\
$okButton = New-Object Windows.Forms.Button\r\n\
$okButton.Location = New-Object Drawing.Size @(297, 115)\r\n\
$okButton.Size = New-Object Drawing.Size @(72, 25)\r\n\
$okButton.Font = New-Object Drawing.Font "MS UI Gothic", 12\r\n\
$okButton.TabIndex = 0\r\n\
$okButton.Text = "OK"\r\n\
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK\r\n\
$okButton.Add_Click({[System.Console]::Write("OK\t" + $userName.Text + "\t" + $password.Text)})\r\n\
$form.Controls.Add($okButton)\r\n\
$form.AcceptButton = $okButton\r\n\
\r\n\
$cancelButton = New-Object Windows.Forms.Button\r\n\
$cancelButton.Location = New-Object Drawing.Size @(192, 115)\r\n\
$cancelButton.Size = New-Object Drawing.Size @(72, 25)\r\n\
$cancelButton.Font = New-Object Drawing.Font "MS UI Gothic", 12\r\n\
$cancelButton.TabIndex = 1\r\n\
$cancelButton.Text = "Cancel"\r\n\
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel\r\n\
$form.Controls.Add($cancelButton)\r\n\
$form.CancelButton = $cancelButton\r\n\
\r\n\
$userName = New-Object Windows.Forms.TextBox\r\n\
$userName.Location = New-Object Drawing.Point @(108, 22)\r\n\
 $userName.Size = New-Object Drawing.Size @(231, 25)\r\n\
 $userName.Font = New-Object Drawing.Font "MS UI Gothic", 12\r\n\
 $userName.TabIndex = 2\r\n\
 $form.Controls.Add($userName)\r\n\
\r\n\
 $password = New-Object Windows.Forms.TextBox\r\n\
 $password.Location = New-Object Drawing.Point @(108, 69)\r\n\
 $password.Size = New-Object Drawing.Size @(231, 25)\r\n\
 $password.Font = New-Object Drawing.Font "MS UI Gothic", 12\r\n\
 $password.PasswordChar = "*"\r\n\
 $password.TabIndex = 3\r\n\
 $form.Controls.Add($password)\r\n\
\r\n\
 $label1 = New-Object System.Windows.Forms.Label\r\n\
 $label1.Location = New-Object Drawing.Point @(12, 25)\r\n\
 $label1.Size = New-Object Drawing.Point @(80, 16)\r\n\
 $label1.Font = New-Object Drawing.Font "MS UI Gothic", 12\r\n\
 $label1.Text = "ユーザーID"\r\n\
 $form.Controls.Add($label1)\r\n\
\r\n\
 $label2 = New-Object System.Windows.Forms.Label\r\n\
 $label2.Location = New-Object Drawing.Point @(16, 72)\r\n\
 $label2.Size = New-Object Drawing.Point @(80, 16)\r\n\
 $label2.Font = New-Object Drawing.Font "MS UI Gothic", 12\r\n\
 $label2.Text = "パスワード"\r\n\
 $form.Controls.Add($label2)\r\n\
\r\n\
 $result = $form.ShowDialog()\r\n\
 ';
    var shellObj = WScript.CreateObject("WScript.Shell");
    var execObj = shellObj.Exec("PowerShell -NonInteractive -WindowStyle Hidden -NoLogo -ExecutionPolicy Unrestricted -Command -");
    execObj.StdIn.Write(code);
    execObj.StdIn.Close();
    var res = execObj.StdOut.ReadAll();
    var rex = new RegExp("^(OK)\t(.*)\t(.*)$");
    var rer = rex.exec(res);
    if (rer) {
        var cred = {
            "userid": rer[2],
            "password": rer[3]
        }
        return cred;
    } else {
        echo("ログイン情報を入力してください");
        quit();
    }
}
var cred = CredentialDialog();
echo(cred.userid + cred.password);

```