【PowerShell利用】オリジナルインプットボックス(機能追加版)
お世話になっております。
既出のコラム(【サンプル】 PowerShell利用オリジナルインプットボックス)を参考に、
「テキストボックス」「コンボボックス」「リストボックス」「チェックボックス」「ラジオボタン」を導入したインプットボックスを作成してみました。
元となるスクリプトでは変数をそのまま改行(sCmd = "****" & _ )で連結していましたが、
こちらでは「sCmd = sCmd & "****"」といった形で文字列を連結しているので、
使用しない箇所があれば、その文頭に「'(シングルコーテーション)」をつけてコメントアウトするだけで非表示にできます。
■スクリプトのプロパティ設定がこちら(中身は後述)
■実際に表示されるインプットボックスがこちら
■上図のように入力した際、変数に取得される値がこちら
※TextBox2では、デフォルトの文字列で改行コードを入れることができなかったので、インプットボックス表示後、手動で「TextBox2のデフォルト値」と「(改行可)」の間をEnterで改行しています。
<スクリプトの記述>
Option Explicit
'***********************************************************VBScript
Const sSplit_ReadLine = "/" ' フォームの入力結果を区切る文字列
Const sSplit_CheckedBox = "," ' チェックボックスで選択された項目を区切る文字列Dim objExec
Dim strCmd
Dim strResult
Dim aryResult' コマンドを取得
strCmd = getPowerShellCommand()Set objExec = WScript.CreateObject("WScript.Shell").Exec(strCmd)
Do While objExec.Status = 0
WScript.Sleep 300
Loop' textBox2の改行対応
strResult =""
Do Until objExec.StdOut.AtEndOfStream
If strResult = "" Then
strResult = objExec.StdOut.ReadLine
Else
strResult = strResult & vbCrLf & objExec.StdOut.ReadLine
End If
LooparyResult = Split(strResult, sSplit_ReadLine)
'''msgbox strResultCall SetUMSVariable($TextBox1の値$, aryResult(0))
Call SetUMSVariable($TextBox2の値$, aryResult(1))
Call SetUMSVariable($ComboBox1の値$, aryResult(2))
Call SetUMSVariable($ComboBox2の値$, aryResult(3))
Call SetUMSVariable($ListBox1の値$, aryResult(4))
Call SetUMSVariable($ListBox2の値$, aryResult(5))
Call SetUMSVariable($CheckedBox1の値$, aryResult(6))
Call SetUMSVariable($CheckedBox2の値$, aryResult(7))
Call SetUMSVariable($RadioButtonの値$, aryResult(8))Set objExec = Nothing
'***********************************************************PowerShellCommand
Function getPowerShellCommand()Dim sCmd
sCmd = ""
sCmd = sCmd & "powershell.exe -ExecutionPolicy RemoteSigned -command " & Chr(34)sCmd = sCmd & "Add-Type -AssemblyName System.Windows.Forms;"
sCmd = sCmd & "Add-Type -AssemblyName System.Drawing;"'-----------------------------------------------------------フォント設定
sCmd = sCmd & "$Font = New-Object System.Drawing.Font('メイリオ',12);"'-----------------------------------------------------------フォーム設定
sCmd = sCmd & "$form = New-Object System.Windows.Forms.Form;"
sCmd = sCmd & "$form.Text = 'フォームタイトル';"
sCmd = sCmd & "$form.Size = New-Object System.Drawing.Size(680,800);"
sCmd = sCmd & "$form.StartPosition = 'CenterScreen';"
sCmd = sCmd & "$form.font = $Font;"'-----------------------------------------------------------TextBox1(1行)
sCmd = sCmd & "$labelText1 = New-Object System.Windows.Forms.Label;"
sCmd = sCmd & "$labelText1.Location = New-Object System.Drawing.Point(10,10);"
sCmd = sCmd & "$labelText1.Size = New-Object System.Drawing.Size(270,20);"
sCmd = sCmd & "$labelText1.Text = 'TextBox1のラベル';"
sCmd = sCmd & "$form.Controls.Add($labelText1);"sCmd = sCmd & "$textBox1 = New-Object System.Windows.Forms.TextBox;"
sCmd = sCmd & "$textBox1.Location = New-Object System.Drawing.Point(30,40);"
sCmd = sCmd & "$textBox1.Size = New-Object System.Drawing.Size(270,20);"
sCmd = sCmd & "$textBox1.Text = 'TextBox1のデフォルト値';"
sCmd = sCmd & "$form.Controls.Add($textBox1);"'-----------------------------------------------------------TextBox2(改行可)
sCmd = sCmd & "$labelText2 = New-Object System.Windows.Forms.Label;"
sCmd = sCmd & "$labelText2.Location = New-Object System.Drawing.Point(320,10);"
sCmd = sCmd & "$labelText2.Size = New-Object System.Drawing.Size(270,20);"
sCmd = sCmd & "$labelText2.Text = 'TextBox2のラベル';"
sCmd = sCmd & "$form.Controls.Add($labelText2);"sCmd = sCmd & "$textBox2 = New-Object System.Windows.Forms.TextBox;"
sCmd = sCmd & "$textBox2.Location = New-Object System.Drawing.Point(350,40);"
sCmd = sCmd & "$textBox2.Size = New-Object System.Drawing.Size(270,60);"
sCmd = sCmd & "$textBox2.Multiline = $True;"
sCmd = sCmd & "$textBox2.AcceptsReturn = $True;"
sCmd = sCmd & "$textBox2.AcceptsTab = $True;"
sCmd = sCmd & "$textBox2.WordWrap = $True;"
sCmd = sCmd & "$textBox2.ScrollBars = [System.Windows.Forms.ScrollBars]::Vertical;"
sCmd = sCmd & "$textBox2.Anchor = (([System.Windows.Forms.AnchorStyles]::Left) `"
sCmd = sCmd & " -bor ([System.Windows.Forms.AnchorStyles]::Top) `"
sCmd = sCmd & " -bor ([System.Windows.Forms.AnchorStyles]::Right) `"
sCmd = sCmd & " -bor ([System.Windows.Forms.AnchorStyles]::Bottom));"
sCmd = sCmd & "$textBox2.Text = 'TextBox2のデフォルト値(改行可)';"
sCmd = sCmd & "$form.Controls.Add($textBox2);"'-----------------------------------------------------------ComboBox1(選択肢を1つずつ追加)
sCmd = sCmd & "$labelCombo1 = New-Object System.Windows.Forms.Label;"
sCmd = sCmd & "$labelCombo1.Location = New-Object System.Drawing.Point(10,120);"
sCmd = sCmd & "$labelCombo1.Size = New-Object System.Drawing.Size(270,20);"
sCmd = sCmd & "$labelCombo1.Text = 'ComboBox1のラベル';"
sCmd = sCmd & "$form.Controls.Add($labelCombo1);"sCmd = sCmd & "$ComboBox1 = New-Object System.Windows.Forms.Combobox;"
sCmd = sCmd & "$ComboBox1.Location = New-Object System.Drawing.Point(30,150);"
sCmd = sCmd & "$ComboBox1.size = New-Object System.Drawing.Size(270,20);"
sCmd = sCmd & "$ComboBox1.DropDownStyle = 'DropDown';"
sCmd = sCmd & "$ComboBox1.FlatStyle = 'standard';"
sCmd = sCmd & "$ComboBox1.font = $Font;"
sCmd = sCmd & "$ComboBox1.BackColor = 'white';"
sCmd = sCmd & "$ComboBox1.ForeColor = 'black';"
sCmd = sCmd & "[void] $ComboBox1.Items.Add('ComboBox1の選択肢A');"
sCmd = sCmd & "[void] $ComboBox1.Items.Add('ComboBox1の選択肢B');"
sCmd = sCmd & "[void] $ComboBox1.Items.Add('ComboBox1の選択肢C');"
sCmd = sCmd & "$form.Controls.Add($ComboBox1);"'-----------------------------------------------------------ComboBox2(選択肢を一括で追加)
sCmd = sCmd & "$labelCombo2 = New-Object System.Windows.Forms.Label;"
sCmd = sCmd & "$labelCombo2.Location = New-Object System.Drawing.Point(320,120);"
sCmd = sCmd & "$labelCombo2.Size = New-Object System.Drawing.Size(270,20);"
sCmd = sCmd & "$labelCombo2.Text = 'ComboBox2のラベル';"
sCmd = sCmd & "$form.Controls.Add($labelCombo2);"sCmd = sCmd & "$ComboBox2Array = ('ComboBox2の選択肢A','ComboBox2の選択肢B','ComboBox2の選択肢C');"
sCmd = sCmd & "$ComboBox2 = New-Object System.Windows.Forms.Combobox;"
sCmd = sCmd & "$ComboBox2.Location = New-Object System.Drawing.Point(350,150);"
sCmd = sCmd & "$ComboBox2.size = New-Object System.Drawing.Size(270,20);"
sCmd = sCmd & "$ComboBox2.DropDownStyle = 'DropDown';"
sCmd = sCmd & "$ComboBox2.FlatStyle = 'standard';"
sCmd = sCmd & "$ComboBox2.font = $Font;"
sCmd = sCmd & "$ComboBox2.BackColor = 'white';"
sCmd = sCmd & "$ComboBox2.ForeColor = 'black';"
sCmd = sCmd & "$ComboBox2.Items.AddRange($ComboBox2Array);"
sCmd = sCmd & "$form.Controls.Add($ComboBox2);"'-----------------------------------------------------------ListBox1(選択肢を1つずつ追加)
sCmd = sCmd & "$labelList1 = New-Object System.Windows.Forms.Label;"
sCmd = sCmd & "$labelList1.Location = New-Object System.Drawing.Point(10,190);"
sCmd = sCmd & "$labelList1.Size = New-Object System.Drawing.Size(270,20);"
sCmd = sCmd & "$labelList1.Text = 'ListBox1のラベル';"
sCmd = sCmd & "$form.Controls.Add($labelList1);"sCmd = sCmd & "$listBox1 = New-Object System.Windows.Forms.ListBox;"
sCmd = sCmd & "$listBox1.Location = New-Object System.Drawing.Point(30,220);"
sCmd = sCmd & "$listBox1.Size = New-Object System.Drawing.Size(270,120);"
sCmd = sCmd & "[void] $listBox1.Items.Add('ListBox1の選択肢A');"
sCmd = sCmd & "[void] $listBox1.Items.Add('ListBox1の選択肢B');"
sCmd = sCmd & "[void] $listBox1.Items.Add('ListBox1の選択肢C');"
sCmd = sCmd & "$form.Controls.Add($listBox1);"'-----------------------------------------------------------ListBox2(選択肢を一括で追加)
sCmd = sCmd & "$labelList2 = New-Object System.Windows.Forms.Label;"
sCmd = sCmd & "$labelList2.Location = New-Object System.Drawing.Point(320,190);"
sCmd = sCmd & "$labelList2.Size = New-Object System.Drawing.Size(270,20);"
sCmd = sCmd & "$labelList2.Text = 'ListBox2のラベル';"
sCmd = sCmd & "$form.Controls.Add($labelList2);"sCmd = sCmd & "$listBox2Array = ('listBox2の選択肢A','listBox2の選択肢B','listBox2の選択肢C');"
sCmd = sCmd & "$listBox2 = New-Object System.Windows.Forms.ListBox;"
sCmd = sCmd & "$listBox2.Location = New-Object System.Drawing.Point(350,220);"
sCmd = sCmd & "$listBox2.Size = New-Object System.Drawing.Size(270,120);"
sCmd = sCmd & "$listBox2.Items.AddRange($listBox2Array);"
sCmd = sCmd & "$form.Controls.Add($listBox2);"'-----------------------------------------------------------CheckedBox1(選択肢を1つずつ追加)
sCmd = sCmd & "$labelChecked1 = New-Object System.Windows.Forms.Label;"
sCmd = sCmd & "$labelChecked1.Location = New-Object System.Drawing.Point(10,340);"
sCmd = sCmd & "$labelChecked1.Size = New-Object System.Drawing.Size(270,20);"
sCmd = sCmd & "$labelChecked1.Text = 'CheckedBox1のラベル';"
sCmd = sCmd & "$form.Controls.Add($labelChecked1);"sCmd = sCmd & "$CheckedBox1 = New-Object System.Windows.Forms.CheckedListBox;"
sCmd = sCmd & "$CheckedBox1.Location = '30,370';"
sCmd = sCmd & "$CheckedBox1.Size = '270,150';"
sCmd = sCmd & "$CheckedBox1.Items.AddRange('CheckedBox1の選択肢A');"
sCmd = sCmd & "$CheckedBox1.Items.AddRange('CheckedBox1の選択肢B');"
sCmd = sCmd & "$CheckedBox1.Items.AddRange('CheckedBox1の選択肢C');"
sCmd = sCmd & "$CheckedBox1.ClearSelected();" ' すべての既存の選択をクリア
sCmd = sCmd & "$form.Controls.Add($CheckedBox1);"' チェックボックスでチェックされた項目を取得(カンマ区切りの文字列に変換)
sCmd = sCmd & "$CheckedBox1Items = @($CheckedBox1.CheckedItems);"
sCmd = sCmd & "$CheckedBox1ItemsStr = $CheckedBox1Items -join ',';"'-----------------------------------------------------------CheckedBox2(選択肢を一括で追加)
sCmd = sCmd & "$labelChecked2 = New-Object System.Windows.Forms.Label;"
sCmd = sCmd & "$labelChecked2.Location = New-Object System.Drawing.Point(320,340);"
sCmd = sCmd & "$labelChecked2.Size = New-Object System.Drawing.Size(270,20);"
sCmd = sCmd & "$labelChecked2.Text = 'CheckedBox2のラベル';"
sCmd = sCmd & "$form.Controls.Add($labelChecked2);"sCmd = sCmd & "$CheckedBox2Array = ('CheckedBox2の選択肢A','CheckedBox2の選択肢B','CheckedBox2の選択肢C');"
sCmd = sCmd & "$CheckedBox2 = New-Object System.Windows.Forms.CheckedListBox;"
sCmd = sCmd & "$CheckedBox2.Location = '350,370';"
sCmd = sCmd & "$CheckedBox2.Size = '270,150';"
sCmd = sCmd & "$CheckedBox2.Items.AddRange($CheckedBox2Array);"
sCmd = sCmd & "$CheckedBox2.ClearSelected();" ' すべての既存の選択をクリア
sCmd = sCmd & "$form.Controls.Add($CheckedBox2);"'-----------------------------------------------------------RadioButton
sCmd = sCmd & "$GroupBox1 = New-Object System.Windows.Forms.GroupBox;"
sCmd = sCmd & "$GroupBox1.Location = New-Object System.Drawing.Point(10,520);"
sCmd = sCmd & "$GroupBox1.size = New-Object System.Drawing.Size(420,160);"
sCmd = sCmd & "$GroupBox1.text = 'GroupBox1のタイトル';"
sCmd = sCmd & "$form.Controls.Add($GroupBox1);"sCmd = sCmd & "$G1RadioButton1 = New-Object System.Windows.Forms.RadioButton;"
sCmd = sCmd & "$G1RadioButton1.Location = New-Object System.Drawing.Point(20,30);"
sCmd = sCmd & "$G1RadioButton1.size = New-Object System.Drawing.Size(350,30);"
sCmd = sCmd & "$G1RadioButton1.Text = 'GroupBox1のRadioButton1の項目名';"
sCmd = sCmd & "$G1RadioButton1.Checked = $True;" ' デフォルトでチェックしておく
sCmd = sCmd & "$form.Controls.Add($G1RadioButton1);"sCmd = sCmd & "$G1RadioButton2 = New-Object System.Windows.Forms.RadioButton;"
sCmd = sCmd & "$G1RadioButton2.Location = New-Object System.Drawing.Point(20,70);"
sCmd = sCmd & "$G1RadioButton2.size = New-Object System.Drawing.Size(350,30);"
sCmd = sCmd & "$G1RadioButton2.Text = 'GroupBox1のRadioButton2の項目名';"
sCmd = sCmd & "$form.Controls.Add($G1RadioButton2);"sCmd = sCmd & "$G1RadioButton3 = New-Object System.Windows.Forms.RadioButton;"
sCmd = sCmd & "$G1RadioButton3.Location = New-Object System.Drawing.Point(20,110);"
sCmd = sCmd & "$G1RadioButton3.size = New-Object System.Drawing.Size(350,30);"
sCmd = sCmd & "$G1RadioButton3.Text = 'GroupBox1のRadioButton3の項目名';"
sCmd = sCmd & "$form.Controls.Add($G1RadioButton3);"' グループにRadioButtonを入れる
sCmd = sCmd & "$GroupBox1.Controls.AddRange(@($G1RadioButton1,$G1RadioButton2,$G1RadioButton3));"'-----------------------------------------------------------OKボタン
sCmd = sCmd & "$OKButton = New-Object System.Windows.Forms.Button;"
sCmd = sCmd & "$OKButton.Location = New-Object System.Drawing.Point(20,710);"
sCmd = sCmd & "$OKButton.Size = New-Object System.Drawing.Size(75,30);"
sCmd = sCmd & "$OKButton.Text = 'OK';"
sCmd = sCmd & "$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK;"
sCmd = sCmd & "$form.AcceptButton = $OKButton;"
sCmd = sCmd & "$form.Controls.Add($OKButton);"'-----------------------------------------------------------キャンセルボタン
sCmd = sCmd & "$CancelButton = New-Object System.Windows.Forms.Button;"
sCmd = sCmd & "$CancelButton.Location = New-Object System.Drawing.Point(120,710);"
sCmd = sCmd & "$CancelButton.Size = New-Object System.Drawing.Size(75,30);"
sCmd = sCmd & "$CancelButton.Text = 'Cancel';"
sCmd = sCmd & "$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel;"
sCmd = sCmd & "$form.CancelButton = $CancelButton;"
sCmd = sCmd & "$form.Controls.Add($CancelButton);"'-----------------------------------------------------------フォームを最前面に表示
sCmd = sCmd & "$form.Topmost = $True;"'-----------------------------------------------------------フォームを表示+選択結果を変数に格納
' フォーム表示
sCmd = sCmd & "$result = $form.ShowDialog();"' CheckedBox1でチェックされた項目を取得(指定語区切りの文字列に変換)
sCmd = sCmd & "$CheckedBox1Items = @($CheckedBox1.CheckedItems);"
sCmd = sCmd & "$CheckedBox1ItemsStr = $CheckedBox1Items -join '" & sSplit_CheckedBox & "';"' CheckedBox2でチェックされた項目を取得(指定語区切りの文字列に変換)
sCmd = sCmd & "$CheckedBox2Items = @($CheckedBox2.CheckedItems);"
sCmd = sCmd & "$CheckedBox2ItemsStr = $CheckedBox2Items -join '" & sSplit_CheckedBox & "';"' RadioButtonでチェックされた項目を取得
sCmd = sCmd & "if ($G1RadioButton1.Checked){` "
sCmd = sCmd & " $GroupBox1Result = $G1RadioButton1.Text` "
sCmd = sCmd & "}elseif ($G1RadioButton2.Checked){` "
sCmd = sCmd & " $GroupBox1Result = $G1RadioButton2.Text` "
sCmd = sCmd & "}elseif ($G1RadioButton3.Checked){` "
sCmd = sCmd & " $GroupBox1Result = $G1RadioButton3.Text` "
sCmd = sCmd & "};"' すべての入力結果を配列に格納
sCmd = sCmd & "$resultArray = @("
sCmd = sCmd & "$textBox1.Text,"
sCmd = sCmd & "$textBox2.Text,"
sCmd = sCmd & "$ComboBox1.Text,"
sCmd = sCmd & "$ComboBox2.Text,"
sCmd = sCmd & "$listBox1.Text,"
sCmd = sCmd & "$listBox2.Text,"
sCmd = sCmd & "$CheckedBox1ItemsStr,"
sCmd = sCmd & "$CheckedBox2ItemsStr,"
sCmd = sCmd & "$GroupBox1Result"
sCmd = sCmd & ");"' 入力結果(OKボタン以外の場合は空文字を返す)
sCmd = sCmd & "if ($result -ne 'OK')` "
sCmd = sCmd & "{` "
sCmd = sCmd & " $resultArray.Clear()` "
sCmd = sCmd & "};"' 指定語句切りの文字列に変換
sCmd = sCmd & "$resultStr = $resultArray -join '" & sSplit_ReadLine & "';"
sCmd = sCmd & "Write-Host $resultStr;"' コマンドを返す
sCmd = sCmd & Chr(34)
getPowerShellCommand = sCmdEnd Function
<参考>
【サンプル】 PowerShell利用オリジナルインプットボックス
https://winactor.com/questions/question/【サンプル】-powershell利用オリジナルインプットボック/
PowerShellでユーザーフォームを作る - 基礎編 -
https://letspowershell.blogspot.com/2015/07/powershell_9.html
PowerShellでユーザーフォームを作る - コンボボックス編 -
https://letspowershell.blogspot.com/2015/07/powershell_29.html
PowerShellでユーザーフォームを作る - リストボックス編 -
PowerShellでユーザーフォームを作る - チェックボックス編 -
https://letspowershell.blogspot.com/2015/07/powershell_15.html
PowerShellでユーザーフォームを作る - ラジオボタン編 -
https://letspowershell.blogspot.com/2015/07/powershell_18.html
【PowerShell】Windowsフォームにテキストボックスを表示して入力できるようにする
https://hosopro.blogspot.com/2017/11/powershell-windows-form-textbox.html
配列の要素の値を連結して一つの文字列にする方法
【連載】PowerShell Core入門 - 基本コマンドの使い方 - 【第17回】配列
<メモ>
2021/06/15:デフォルトのポリシーで実行できない場合に対応するため、PowerShellコマンドのオプションを変更
sCmd = sCmd & "powershell.exe -command " & Chr(34)
▼
sCmd = sCmd & "powershell.exe -ExecutionPolicy RemoteSigned -command " & Chr(34)
参考: