0
0

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 5 years have passed since last update.

Powershellのダイアログでスクリプト一覧から別スクリプトを実行する

0
Posted at

反省

アドイベントカレンダーに間に合わなかった。

本題

powershellを使って実行するスクリプトをコントロールしたいということを目的に作成しました。

ソースコード

Menu.ps1
 # Add WindowsForm
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

# Menu
$MenuForm = New-Object System.Windows.Forms.Form
$MenuForm.Text = 'Select Scripts'
$MenuForm.Size = New-Object System.Drawing.Size(300,200)
$MenuForm.StartPosition = 'CenterScreen'

# Start Button
$StartBtn = New-Object System.Windows.Forms.Button
$StartBtn.Location = New-Object System.Drawing.Point(75,120)
$StartBtn.Size = New-Object System.Drawing.Size(75,23)
$StartBtn.Text = 'Start'
$StartBtn.DialogResult = [System.Windows.Forms.DialogResult]::OK

# End Button
$EndBtn = New-Object System.Windows.Forms.Button
$EndBtn.Location = New-Object System.Drawing.Point(150,120)
$EndBtn.Size = New-Object System.Drawing.Size(75,23)
$EndBtn.Text = 'End'
$EndBtn.DialogResult = [System.Windows.Forms.DialogResult]::Cancel

# Script List
$ScriptList = New-Object System.Windows.Forms.Label
$ScriptList.Location = New-Object System.Drawing.Point(10,20)
$ScriptList.Size = New-Object System.Drawing.Size(280,20)
$ScriptList.Text = 'Please select a Scripts:'
$SlectScriptBox = New-Object System.Windows.Forms.ListBox
$SlectScriptBox.Location = New-Object System.Drawing.Point(10,40)
$SlectScriptBox.Size = New-Object System.Drawing.Size(260,20)
$SlectScriptBox.Height = 80

# Add Button
$MenuForm.AcceptButton = $StartBtn
$MenuForm.Controls.Add($StartBtn)
$MenuForm.CancelButton = $EndBtn
$MenuForm.Controls.Add($EndBtn)
$MenuForm.Controls.Add($ScriptList)
$MenuForm.Topmost = $true

# Config Setting
$ResultDoScript = ""
$MenuLists = Import-Csv -Encoding UTF8 "./Config/Menu.csv" | Where-Object {$_.ActiveFlg -eq "true"}
foreach($MenuList in $MenuLists){
    [void] $SlectScriptBox.Items.Add($MenuList.MenuList)    
}


for($i = 0;$i -lt 1;$i){
    $MenuForm.Controls.Add($SlectScriptBox)
    $SelectResult = $MenuForm.ShowDialog()


    if ($SelectResult -eq [System.Windows.Forms.DialogResult]::OK){
        $ResultDoScript = $SlectScriptBox.SelectedItem
        if($ResultDoScript -eq $null){
            Write-Host "This is null."
        }
    } elseif(($SelectResult -eq [System.Windows.Forms.DialogResult]::Cancel)) {
        Exit
    }

    # select call script
    $Powershell = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
    $ScriptPath = Import-Csv -Encoding UTF8 "./Config/Menu.csv" | Where-Object {$_.MenuList -eq $ResultDoScript}
    $ScriptFile = $ScriptPath.Path
    $Argument   = "-Command $ScriptFile"

    # call script
    Start-Process -FilePath $Powershell -ArgumentList $Argument -Wait
}
./config/Menu.csv
"MenuList","Path","ActiveFlg"
"sample1",".\sample1\index.ps1","true"
"sample2",".\sample2\index.ps1","false"

参考

リスト ボックスから項目を選択する

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?