{"id":2453,"date":"2018-04-25T15:06:55","date_gmt":"2018-04-25T13:06:55","guid":{"rendered":"http:\/\/www.mariotti.de\/?p=2453"},"modified":"2018-04-25T15:06:55","modified_gmt":"2018-04-25T13:06:55","slug":"powershell-dialog","status":"publish","type":"post","link":"https:\/\/dev.mariotti.de\/?p=2453","title":{"rendered":"Dialoge in PowerShell"},"content":{"rendered":"<p>In diesem Artikel m\u00f6chte ich einige Beispiel zeigen wie man Dialog aus dem .Net Framework in PowerShell nutzen kann.<\/p>\n<h2>Message Box<\/h2>\n<pre><code class=\"language-\"># Show message box popup.\nAdd-Type -AssemblyName System.Windows.Forms\n$result = [System.Windows.Forms.MessageBox]::Show(\"My message\", \"Window Title\", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::None)\n <\/code><\/pre>\n<h2>Message Box with Custom Buttons<\/h2>\n<pre><code class=\"language-powershell\">$caption = \"Choose Action\";\n$message = \"What do you want to do?\";\n$restart = new-Object System.Management.Automation.Host.ChoiceDescription \"&amp;Restart\",\"Restart\";\n$shutdown = new-Object System.Management.Automation.Host.ChoiceDescription \"&amp;Shutdown\",\"Shutdown\";\n$choices = [System.Management.Automation.Host.ChoiceDescription[]]($restart,$shutdown);\n$answer = $host.ui.PromptForChoice($caption,$message,$choices,0)\nswitch ($answer){\n    0 {\"You entered restart\"; break}\n    1 {\"You entered shutdown\"; break}\n}<\/code><\/pre>\n<h2>Input Box<\/h2>\n<pre><code class=\"language-powershell\"># Show input box popup.\nAdd-Type -AssemblyName Microsoft.VisualBasic\n$inputText = [Microsoft.VisualBasic.Interaction]::InputBox(\"Enter some value:\", \"Window Title\", \"Default value\")\n <\/code><\/pre>\n<h2>Open File Dialog<\/h2>\n<pre><code class=\"language-powershell\"># Show an Open File Dialog and return the file selected by the user.\nfunction Read-OpenFileDialog([string]$InitialDirectory, [switch]$AllowMultiSelect)\n{\nAdd-Type -AssemblyName System.Windows.Forms\n$openFileDialog = New-Object System.Windows.Forms.OpenFileDialog\n$openFileDialog.initialDirectory = $InitialDirectory\n$openFileDialog.filter = \"All files (*.*)| *.*\"\nif ($AllowMultiSelect) { $openFileDialog.MultiSelect = $true }\n$openFileDialog.ShowDialog() &gt; $null\nif ($allowMultiSelect) { return $openFileDialog.Filenames } else { return $openFileDialog.Filename }\n}\nRead-OpenFileDialog C:\\Temp -AllowMultiSelect<\/code><\/pre>\n<h2>Folder Browse Dialog<\/h2>\n<pre><code class=\"language-powershell\"># Show an Open Folder Dialog and return the directory selected by the user.\nfunction Read-FolderBrowseDialog([string]$InitialDirectory)\n{\nAdd-Type -AssemblyName System.Windows.Forms\n$openFolderDialog = New-Object System.Windows.Forms.FolderBrowserDialog\n$openFolderDialog.ShowNewFolderButton = $true\n$openFolderDialog.RootFolder = $InitialDirectory\n$openFolderDialog.ShowDialog()\nreturn $openFolderDialog.SelectedPath\n}\nRead-FolderBrowseDialog Desktop<\/code><\/pre>\n<h2>List Box Dialog<\/h2>\n<pre><code class=\"language-powershell\">Add-Type -AssemblyName System.Windows.Forms\nAdd-Type -AssemblyName System.Drawing\n$form = New-Object System.Windows.Forms.Form\n$form.Text = 'My Title'\n$form.Size = New-Object System.Drawing.Size(300,200)\n$form.StartPosition = 'CenterScreen'\n$OKButton = New-Object System.Windows.Forms.Button\n$OKButton.Location = New-Object System.Drawing.Point(75,120)\n$OKButton.Size = New-Object System.Drawing.Size(75,23)\n$OKButton.Text = 'OK'\n$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK\n$form.AcceptButton = $OKButton\n$form.Controls.Add($OKButton)\n$CancelButton = New-Object System.Windows.Forms.Button\n$CancelButton.Location = New-Object System.Drawing.Point(150,120)\n$CancelButton.Size = New-Object System.Drawing.Size(75,23)\n$CancelButton.Text = 'Cancel'\n$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel\n$form.CancelButton = $CancelButton\n$form.Controls.Add($CancelButton)\n$label = New-Object System.Windows.Forms.Label\n$label.Location = New-Object System.Drawing.Point(10,20)\n$label.Size = New-Object System.Drawing.Size(280,20)\n$label.Text = 'Please select a value:'\n$form.Controls.Add($label)\n$listBox = New-Object System.Windows.Forms.ListBox\n$listBox.Location = New-Object System.Drawing.Point(10,40)\n$listBox.Size = New-Object System.Drawing.Size(260,20)\n$listBox.Height = 80\n[void] $listBox.Items.Add('Value1')\n[void] $listBox.Items.Add('Value2')\n[void] $listBox.Items.Add('Value3')\n[void] $listBox.Items.Add('Value4')\n[void] $listBox.Items.Add('Value5')\n[void] $listBox.Items.Add('Value6')\n[void] $listBox.Items.Add('Value7')\n$form.Controls.Add($listBox)\n$form.Topmost = $true\n$result = $form.ShowDialog()\nif ($result -eq [System.Windows.Forms.DialogResult]::OK)\n{\n    $x = $listBox.SelectedItem\n    $x\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In diesem Artikel m\u00f6chte ich einige Beispiel zeigen wie man Dialog aus dem .Net Framework in PowerShell nutzen kann.<\/p>\n","protected":false},"author":2,"featured_media":2425,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[],"class_list":["post-2453","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"_links":{"self":[{"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=\/wp\/v2\/posts\/2453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2453"}],"version-history":[{"count":0,"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=\/wp\/v2\/posts\/2453\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=\/wp\/v2\/media\/2425"}],"wp:attachment":[{"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}