Quantcast
Channel: /r/powershell – How to Code .NET
Viewing all articles
Browse latest Browse all 8793

looking to add a menu bar

$
0
0

I have a powershell form that is just about complete but I am having trouble making a simple menu bar on top such as file help etc.

here is my code so far Any help would be much appreciated

function Reset-SWADPassword { [cmdletbinding(SupportsShouldProcess=$true,ConfirmImpact='Medium')] param( # Identity of user that should have their pasword reset #[Parameter(Mandatory=$true)] #[Alias('DistinguishedName')] #[String]$Identity, # Length of password that will be generated [ValidateRange(1,[int]::MaxValue)] [int]$Length = 8, [string]$Identity, # Specifies an array of strings containing charactergroups from which the password will be generated. # At least one char from each group (string) will be used. [String[]]$InputStrings = @('abcdefghijkmnopqrstuvwxyz', 'ABCEFGHJKLMNPQRSTUVWXYZ', '23456789', '!"#%&'), # Specifies that the user will not have to change their password on next logon [Switch]$NoChange ) #write-host $Result Try { $Password = New-SWRandomPassword -PasswordLength $Length -InputStrings $InputStrings $SecPaswd = ConvertTo-SecureString -String $Password -AsPlainText -Force if($PSCmdlet.ShouldProcess("`n$DistinguishedName",'Reset password')) { $ADUser = Set-ADAccountPassword -Reset -NewPassword $SecPaswd -Identity $Identity -PassThru -Confirm:$false -WhatIf:$false -ErrorAction Stop Write-Verbose -Message 'Password reset successfully' if(-Not $NoChange) #Changed here { Set-ADUser -ChangePasswordAtLogon $False -Identity $ADUser -Confirm:$false -WhatIf:$false -ErrorAction Stop Write-Verbose -Message 'Change password at logon set to True' } Unlock-ADAccount -Identity $ADUser -Confirm:$false -WhatIf:$false -ErrorAction Stop Write-Verbose -Message 'Useraccount unlocked' Get-Phonetic -Char $Password passreport -Identity $Identity } } Catch { Throw $_ } } #-------------------------------------------------------------------------------------------------------------- function New-SWRandomPassword { <# .Synopsis Generates one or more complex passwords designed to fulfill the requirements for Active Directory .DESCRIPTION Generates one or more complex passwords designed to fulfill the requirements for Active Directory .EXAMPLE New-SWRandomPassword C&3SX6Kn Will generate one password with a length between 8 and 12 chars. .EXAMPLE New-SWRandomPassword -MinPasswordLength 8 -MaxPasswordLength 12 -Count 4 7d&5cnaB !Bh776T"Fw 9"C"RxKcY %mtM7#9LQ9h Will generate four passwords, each with a length of between 8 and 12 chars. .EXAMPLE New-SWRandomPassword -InputStrings abc, ABC, 123 -PasswordLength 4 3ABa Generates a password with a length of 4 containing atleast one char from each InputString .EXAMPLE New-SWRandomPassword -InputStrings abc, ABC, 123 -PasswordLength 4 -FirstChar abcdefghijkmnpqrstuvwxyzABCEFGHJKLMNPQRSTUVWXYZ 3ABa Generates a password with a length of 4 containing atleast one char from each InputString that will start with a letter from the string specified with the parameter FirstChar .OUTPUTS [String] .NOTES Written by Simon Wåhlin, blog.simonw.se I take no responsibility for any issues caused by this script. .FUNCTIONALITY Generates random passwords .LINK http://blog.simonw.se/powershell-generating-random-password-for-active-directory/ #> [CmdletBinding(DefaultParameterSetName='FixedLength',ConfirmImpact='None')] [OutputType([String])] Param ( # Specifies minimum password length [Parameter(Mandatory=$false, ParameterSetName='RandomLength')] [ValidateScript({$_ -gt 0})] [Alias('Min')] [int]$MinPasswordLength = 8, # Specifies maximum password length [Parameter(Mandatory=$false, ParameterSetName='RandomLength')] [ValidateScript({ if($_ -ge $MinPasswordLength){$true} else{Throw 'Max value cannot be lesser than min value.'}})] [Alias('Max')] [int]$MaxPasswordLength = 12, # Specifies a fixed password length [Parameter(Mandatory=$false, ParameterSetName='FixedLength')] [ValidateRange(1,2147483647)] [int]$PasswordLength = 8, # Specifies an array of strings containing charactergroups from which the password will be generated. # At least one char from each group (string) will be used. [String[]]$InputStrings = @('abcdefghijkmnpqrstuvwxyz', 'ABCEFGHJKLMNPQRSTUVWXYZ', '23456789', '!"#%&'), # Specifies a string containing a character group from which the first character in the password will be generated. # Useful for systems which requires first char in password to be alphabetic. [String] $FirstChar, # Specifies number of passwords to generate. [ValidateRange(1,2147483647)] [int]$Count = 1 ) Begin { Function Get-Seed{ # Generate a seed for randomization $RandomBytes = New-Object -TypeName 'System.Byte[]' 4 $Random = New-Object -TypeName 'System.Security.Cryptography.RNGCryptoServiceProvider' $Random.GetBytes($RandomBytes) [BitConverter]::ToUInt32($RandomBytes, 0) } } Process { For($iteration = 1;$iteration -le $Count; $iteration++){ $Password = @{} # Create char arrays containing groups of possible chars [char[][]]$CharGroups = $InputStrings # Create char array containing all chars $AllChars = $CharGroups | ForEach-Object {[Char[]]$_} # Set password length if($PSCmdlet.ParameterSetName -eq 'RandomLength') { if($MinPasswordLength -eq $MaxPasswordLength) { # If password length is set, use set length $PasswordLength = $MinPasswordLength } else { # Otherwise randomize password length $PasswordLength = ((Get-Seed) % ($MaxPasswordLength + 1 - $MinPasswordLength)) + $MinPasswordLength } } # If FirstChar is defined, randomize first char in password from that string. if($PSBoundParameters.ContainsKey('FirstChar')){ $Password.Add(0,$FirstChar[((Get-Seed) % $FirstChar.Length)]) } # Randomize one char from each group Foreach($Group in $CharGroups) { if($Password.Count -lt $PasswordLength) { $Index = Get-Seed While ($Password.ContainsKey($Index)){ $Index = Get-Seed } $Password.Add($Index,$Group[((Get-Seed) % $Group.Count)]) } } # Fill out with chars from $AllChars for($i=$Password.Count;$i -lt $PasswordLength;$i++) { $Index = Get-Seed While ($Password.ContainsKey($Index)){ $Index = Get-Seed } $Password.Add($Index,$AllChars[((Get-Seed) % $AllChars.Count)]) } Write-Output -InputObject $(-join ($Password.GetEnumerator() | Sort-Object -Property Name | Select-Object -ExpandProperty Value)) } } } function Get-Phonetic { <# .Synopsis Generates a table with phonetic spelling from a collection of characters .DESCRIPTION Generates a table with phonetic spelling from a collection of characters .EXAMPLE "gjIgsj" | Get-Phonetic Input text: gjIgsj Char Phonetic ---- -------- g golf j juliett I INDIA g golf s sierra j juliett .OUTPUTS [String] .NOTES Written by Simon Wåhlin, blog.simonw.se I take no responsibility for any issues caused by this script. #> Param ( # List of characters to translate to phonetic alphabet [Parameter(Mandatory=$true,ValueFromPipeLine=$true)] [Char[]]$Char, # Hashtable containing a char as key and phonetic word as value [HashTable]$PhoneticTable = @{ 'a' = 'alpha' ;'b' = 'bravo' ;'c' = 'charlie';'d' = 'delta'; 'e' = 'echo' ;'f' = 'foxtrot' ;'g' = 'golf' ;'h' = 'hotel'; 'i' = 'india' ;'j' = 'juliett' ;'k' = 'kilo' ;'l' = 'lima' ; 'm' = 'mike' ;'n' = 'november';'o' = 'oscar' ;'p' = 'papa' ; 'q' = 'quebec' ;'r' = 'romeo' ;'s' = 'sierra' ;'t' = 'tango'; 'u' = 'uniform' ;'v' = 'victor' ;'w' = 'whiskey';'x' = 'x-ray'; 'y' = 'yankee' ;'z' = 'zulu' ;'0' = 'Zero' ;'1' = 'One' ; '2' = 'Two' ;'3' = 'Three' ;'4' = 'Four' ;'5' = 'Five' ; '6' = 'Six' ;'7' = 'Seven' ;'8' = 'Eight' ;'9' = 'Niner'; '.' = 'Point' ;'!' = 'Exclamationmark';'?' = 'Questionmark'; } ) Process { $Result = Foreach($Character in $Char) { if($PhoneticTable.ContainsKey("$Character")) { if([Char]::IsUpper([Char]$Character)) { [PSCustomObject]@{ Char = $Character;Phonetic = $PhoneticTable["$Character"].ToUpper() } } else { [PSCustomObject]@{ Char = $Character;Phonetic = $PhoneticTable["$Character"].ToLower() } } } else { [PSCustomObject]@{ Char = $Character;Phonetic = $Character } } } "`n{0}`n{1}" -f ('Input text: {0}'-f-join$Char), ($Result | Format-Table -AutoSize | Out-String) } } function passreport ($Identity) { $date = Get-Date $staff = [Environment]::UserName $P = $staff + " " + $date + " " + $Identity $P | out-file P:Active_DirectoryProcess.txt -append -force } Add-Type -AssemblyName System.Windows.Forms $Form = New-Object system.Windows.Forms.Form $Form.Text = "BCC Student Password Reset" $Form.TopMost = $true $Form.Width = 300 $Form.Height = 400 $Form.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon('C:UsersBprutsosDownloadsfavicon.ico') $outputBox = New-Object System.Windows.Forms.TextBox $outputBox.Location = New-Object System.Drawing.Size(15,30) $outputBox.Size = New-Object System.Drawing.Size(($Form.Width - 40),250) $outputBox.MultiLine = $True $outputBox.ScrollBars = "Vertical" $outputBox.Font = 'Consolas,10' $Form.Controls.Add($outputBox) $textBox2 = New-Object system.windows.Forms.TextBox $textBox2.Width = $Form.Size[0].Width - 40 $textBox2.Height = 20 $textBox2.location = new-object system.drawing.point(15,285) $textBox2.Font = "Microsoft Sans Serif,10" $textBox2.Text = "Student Number" $textBox2.Add_MouseLeave({Start-Sleep -Milliseconds 750; if($Textbox2.text -eq $null){$textBox2.Text = "Identity?"}}) $textbox2.Add_Click({if($Textbox2 -match "Identity?"){$textBox2.Clear()}}) $Form.controls.Add($textBox2) $button6 = New-Object system.windows.Forms.Button $button6.Text = "Reset Password" $button6.Width = 120 $button6.Height = 40 $button6.Add_Click({if($Textbox2 -ne "Identity?"){ Try{ $OutputBox.Text = Reset-SWADPassword -Identity $textBox2.Text | Out-String } Catch { $outputBox.Text = $Error | Out-String $Error.clear } }}) $button6.location = new-object System.Drawing.Size(15,310) $button6.Font = "Microsoft Sans Serif,10" $Form.controls.Add($button6) $exitToolStripMenuItem_Click={ #TODO: Place custom script here $form1.Close() } $aboutToolStripMenuItem_Click={ #TODO: Place custom script here [System.Windows.Forms.MessageBox]::Show("Menu Application v1.0","Menu Application"); } [void]$Form.ShowDialog() $Form.Dispose() 

submitted by /u/gearfuze
[link] [comments]

The post looking to add a menu bar appeared first on How to Code .NET.


Viewing all articles
Browse latest Browse all 8793

Latest Images

Trending Articles



Latest Images