Here I have wrote this small utility for one of my application developer friend, He required a small GUI utility to convert and show user account to SID and SID to account, viceversa, Although I can write WPF xaml gui form as well, but Here I am using simple winform and this is designed using online designer tool http://poshgui.com. (Recently Poshgui has changed its look and added more controls, made it more user friendly, the best thing is it is free)
Once this utility is launched, you have to type valid account or SID in text box. Once appropriate button is pressed it converts either from SID or account respectively to account or SID, When I say Account means it can be either UserName or GroupName including both domain and localhost. If invalid SID or Account is typed it shows users a friendly error message for either SID or Account is invalid.
In the script I have added addition control tooltip which I found on https://www.petri.com/add-popup-tips-powershell-winforms-script. I have tried to explain tooltip here. I have added new object forms control ToolTip in the script. In the the tooltip also shows nice use of $this automatic variable, In a script block $this defines a script property or script method, the $This variable refers to the object that is being extended.. Whenever mouse is hovered over a control (button or textbox), It captures the control name and use switch condition to show the appropriate message.
To capture the control name there must be a name property added shown in the controls and an event method add_MouseHover.
$Tooltip = New-Object System.Windows.Forms.ToolTip $ShowHelp = { Switch ($this.name) { 'InputBox' {$Tip = "If local checked don't provide domainname"; Break} } $Tooltip.SetToolTip($this,$Tip) }
$InputBox.Name = 'InputBox'
$InputBox.add_MouseHover($ShowHelp)
Another thing I added in the script when form is launched, it shows grayed out text info on the input textbox and once anything is typed, it will change the color of text to black, this requires another event method add_TextChanged.
$InputBoxWaterMark = 'Type valid User, Group with Domain or SID' $InputBox.ForeColor = 'LightGray' $InputBox.Text = $InputBoxWaterMark
$InputBox.add_TextChanged({$InputBox.ForeColor = 'Black'})
Download This script is available here, This script also available on Github.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
<# This form was created using POSHGUI.com a free online gui designer for PowerShell WrittenBy: http://vcloud-lab.com .NAME SID to Account Account to SID #> #Add-Type -AssemblyName System.Windows.Forms [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [System.Windows.Forms.Application]::EnableVisualStyles() function Get-SID { try { $LocalAccount = New-Object System.Security.Principal.NTAccount(($InputBox.Text).trim()) -ErrorAction Stop $LocalSID = $LocalAccount.Translate([System.Security.Principal.SecurityIdentifier]) $OutputResult.Text = $LocalSID.Value } catch { [System.Windows.Forms.MessageBox]::Show("Typed Domain\AccountName not valid", "Invalid details") $OutputResult.Text = '' } } function Get-Account { try { $SID = New-Object System.Security.Principal.SecurityIdentifier(($InputBox.Text).trim()) -ErrorAction Stop $DomainAccount = $SID.Translate([System.Security.Principal.NTAccount]) $OutputResult.Text = $DomainAccount.Value } catch { [System.Windows.Forms.MessageBox]::Show("Typed SID is not valid", "Invalid details") $OutputResult.Text = '' } } #region begin GUI{ $Tooltip = New-Object System.Windows.Forms.ToolTip $ShowHelp = { Switch ($this.name) { 'InputBox' {$Tip = "If local checked don't provide domainname"; Break} 'SidToUser' {$Tip = 'Type Valid Domain\UserName'; Break} 'UserToSid' {$Tip = 'Type Valid User SID'; Break} #'LocalCheckBox' {$Tip = "If local checked don't provide domainname"; Break} } $Tooltip.SetToolTip($this,$Tip) } $vcloudlabform = New-Object system.Windows.Forms.Form $vcloudlabform.ClientSize = '396,172' $vcloudlabform.text = "SID to Account and Vice Versa" $vcloudlabform.TopMost = $false $InputBox = New-Object system.Windows.Forms.TextBox $InputBox.Name = 'InputBox' $InputBox.multiline = $false $InputBox.width = 366 $InputBox.height = 20 $InputBox.location = New-Object System.Drawing.Point(8,19) $InputBox.Font = 'Microsoft Sans Serif,10' $InputBoxWaterMark = 'Type valid User, Group with Domain or SID' $InputBox.ForeColor = 'LightGray' $InputBox.Text = $InputBoxWaterMark $InputBox.add_MouseHover($ShowHelp) $InputBox.add_TextChanged({$InputBox.ForeColor = 'Black'}) $SidToAccount = New-Object system.Windows.Forms.Button $SidToAccount.Name = 'SidToUser' $SidToAccount.text = 'SID-To-Account' $SidToAccount.width = 115 $SidToAccount.height = 30 $SidToAccount.location = New-Object System.Drawing.Point(10,98) $SidToAccount.Font = 'Microsoft Sans Serif,10' $SidToAccount.add_MouseHover($ShowHelp) $SidToAccount.add_Click({Show-SIDToAccount}) $AccountToSid = New-Object system.Windows.Forms.Button $AccountToSid.Name = 'UserToSid' $AccountToSid.text = 'Account-To-SID' $AccountToSid.width = 115 $AccountToSid.height = 30 $AccountToSid.location = New-Object System.Drawing.Point(127,98) $AccountToSid.Font = 'Microsoft Sans Serif,10' $AccountToSid.add_MouseHover($ShowHelp) $AccountToSid.add_Click({Show-AccountToSID}) $OutputResult = New-Object system.Windows.Forms.TextBox $OutputResult.Name = 'OutputResult' $OutputResult.multiline = $false $OutputResult.width = 366 $OutputResult.height = 20 $OutputResult.location = New-Object System.Drawing.Point(8,53) $OutputResult.Font = 'Microsoft Sans Serif,10' $OutputResult.ReadOnly = $true $Site = New-Object system.Windows.Forms.LinkLabel $Site.Name = 'Site' $Site.text = "http://vcloud-lab.com" $Site.LinkColor = "#0074A2" $Site.ActiveLinkColor = "#114C7F" $Site.AutoSize = $true $Site.width = 25 $Site.height = 10 $Site.location = New-Object System.Drawing.Point(260,154) $Site.Font = 'Microsoft Sans Serif,10' $Site.add_Click({[system.Diagnostics.Process]::start('http://vcloud-lab.com')}) $vcloudlabform.controls.AddRange(@($InputBox,$OutputResult,$SidToAccount,$AccountToSid,$Site)) #region gui events { function Show-AccountToSID { if (($InputBox.Text -eq $InputBoxWaterMark) -or ($InputBox.Text -eq '')) { [System.Windows.Forms.MessageBox]::Show("Please type valid Domain\Account", "Textbox empty") } else { Get-SID } } function Show-SIDToAccount { if (($InputBox.Text -eq $InputBoxWaterMark) -or ($InputBox.Text -eq '')) { [System.Windows.Forms.MessageBox]::Show("Please type valid SID", "Textbox empty") } else { Get-Account } } #endregion events } #endregion GUI } [void]$vcloudlabform.ShowDialog() |
Other Powershell GUI blogs
COOL POWERSHELL FREE ONLINE GUI GENERATOR TOOL, POSHGUI
Generate random password GUI using powershell
Part 1: Create WPF XAML powershell GUI form with Visual studio
Part 2: Powershell and WPF: Build GUI applications tutorial