Save code below in file: GenerateRandomPassword.ps1
|
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
#$AUTOSTART = $false if ( $host.name -eq "Windows PowerShell ISE Host" ) { $filename = $psISE.CurrentFile.FullPath.Replace(".ps1",".xaml") } else { $filename = $MyInvocation.MyCommand.Path.Replace(".ps1",".xaml") } $scriptpath = $filename -replace ($filename -split "\\" | Select-Object -Last 1),"" [xml]$xaml = Get-Content $filename -Raw [void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework') #[void][System.Windows.Forms.Application]::EnableVisualStyles() Add-Type -AssemblyName Microsoft.VisualBasic $reader=(New-Object System.Xml.XmlNodeReader $xaml) try { $Form=[Windows.Markup.XamlReader]::Load( $reader ) } catch { Write-Host "Unable to load Windows.Markup.XamlReader. Some possible causes for this problem include: .NET Framework is missing PowerShell must be launched with PowerShell -sta,invalid XAML code was encountered." } # Load nodes from XAML source and create variables $xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]") | ForEach { New-Variable -Name $_.Name -Value $Form.FindName($_.Name) -Force } function move-window { param( [int]$newX, [int]$newY ) BEGIN { $signature = @' [DllImport("user32.dll")] public static extern bool MoveWindow( IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); [DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll")] public static extern bool GetWindowRect( HandleRef hWnd, out RECT lpRect); public struct RECT { public int Left; // x position of upper-left corner public int Top; // y position of upper-left corner public int Right; // x position of lower-right corner public int Bottom; // y position of lower-right corner } '@ Add-Type -MemberDefinition $signature -Name Wutils -Namespace WindowsUtils } PROCESS{ $phandle = [WindowsUtils.Wutils]::GetForegroundWindow() $o = New-Object -TypeName System.Object $href = New-Object -TypeName System.RunTime.InteropServices.HandleRef -ArgumentList $o, $phandle $rct = New-Object WindowsUtils.Wutils+RECT [WindowsUtils.Wutils]::GetWindowRect($href, [ref]$rct) $width = $rct.Right - $rct.Left $height = 800 <# $height = $rct.Bottom = $rct.Top $rct.Right $rct.Left $rct.Bottom $rct.Top $width $height #> [WindowsUtils.Wutils]::MoveWindow($phandle, $newX, $newY, $width, $height, $true) } } # _________________________________________________ # G E N E R A T E P A S S W O R D F U N C T I O N # ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ # This function generates a password of specified length function GeneratePassword () { param ( [Int] $Length=11, [string] $chars = 'abcdefghikmnopqrstuvwxyzACDEFGJHKLMNPQRSTUVWXYZ23456789', [string] $specials ) function Get-RandomCharacters($length, $characters) { $random = 1..$length | ForEach-Object { Get-Random -Maximum $characters.length } $private:ofs="" return [String]$characters[$random] } $characters = "$($chars)$($specials)" #$characters #<# $password = Get-RandomCharacters -length $Length -characters $characters return [string]$password #> } #for($i=0;$i -lt 30;$i++){GeneratePassword} $CopySelPwd_Btn.IsEnabled = $false $CopyAllPwd_Btn.IsEnabled = $false $GeneratedPwds_Lbx.Add_SelectionChanged({ if( $GeneratedPwds_Lbx.SelectedIndex -ne -1 ) { $CopySelPwd_Btn.IsEnabled = $true } }) $PwdGenSingle_Btn.Add_Click({ $pwd = GeneratePassword -Length ([INT]$PwdLength_Tbx.Text.ToString()) $PwdBaseStr_Tbx.Text -specials $PwdSpecialStr_Tbx.Text $GeneratedPwds_Lbx.Items.Add($pwd) if( $GeneratedPwds_Lbx.Items.Count -gt 0 -and -not $CopyAllPwd_Btn.IsEnabled ){$CopyAllPwd_Btn.IsEnabled = $true} $pwd | Set-Clipboard }) $PwdGenMultiple_Btn.Add_Click({ for($i=0;$i -lt ([INT]$NmbrsOfPwds_Tbx.Text.ToString()) ; $i++ ){ $pwd = GeneratePassword -Length ([INT]$PwdLength_Tbx.Text.ToString()) $PwdBaseStr_Tbx.Text -specials $PwdSpecialStr_Tbx.Text $GeneratedPwds_Lbx.Items.Add($pwd) } if( $GeneratedPwds_Lbx.Items.Count -gt 0 -and -not $CopyAllPwd_Btn.IsEnabled ){$CopyAllPwd_Btn.IsEnabled = $true} }) $CopyAllPwd_Btn.Add_Click({ $GeneratedPwds_Lbx.Items | Set-Clipboard }) $CopySelPwd_Btn.Add_Click({ if( $GeneratedPwds_Lbx.SelectedIndex -ne -1 ) { $GeneratedPwds_Lbx.SelectedValue | Set-Clipboard } }) # move form to specific screen position, x,y $Form.Add_loaded({ }) $Form.Add_Closed({ }) # check if executed in Windows Powershell ISE, if not resize console if ( $host.name -eq "ConsoleHost" ) { # move-window 20 820 # [console]::WindowWidth=78; [console]::WindowHeight=5; [console]::BufferWidth=[console]::WindowWidth } $async = $form.Dispatcher.InvokeAsync({ $form.ShowDialog() | Out-Null }) $async.Wait() | Out-Null |
Save code below in file: GenerateRandomPassword.xaml
|
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 |
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Title="Random Password Generator" Height="450" Width="800" MinWidth="800" MinHeight="450"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="10"/> <ColumnDefinition Width="119*"/> <ColumnDefinition Width="416*"/> <ColumnDefinition Width="118*"/> <ColumnDefinition Width="119*"/> <ColumnDefinition Width="10"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="10"/> <RowDefinition Height="30"/> <RowDefinition Height="30"/> <RowDefinition Height="30"/> <RowDefinition Height="4"/> <RowDefinition Height="30"/> <RowDefinition/> <RowDefinition Height="10"/> </Grid.RowDefinitions> <Grid.Background> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="#FFADC5D1" Offset="0"/> <GradientStop Color="#FFB3D7EA" Offset="1"/> </LinearGradientBrush> </Grid.Background> <TextBox x:Name="PwdBaseStr_Tbx" Grid.Column="2" Height="23" Margin="4" Grid.Row="1" TextWrapping="Wrap" Text="abcdefghikmnopqrstuvwxyzACDEFGJHKLMNPQRSTUVWXYZ23456789" VerticalContentAlignment="Center" Padding="4,0,0,0" FontFamily="Segoe UI Light"/> <TextBox x:Name="PwdSpecialStr_Tbx" Grid.Column="2" Height="23" Margin="4" TextWrapping="Wrap" VerticalContentAlignment="Center" Padding="4,0,0,0" Grid.Row="2" FontFamily="Segoe UI Light" Text="!@#"/> <Label x:Name="PwdBaseStr_Lbl" Content="Base string" Grid.Column="1" Margin="0,2,2,2" Grid.Row="1" HorizontalAlignment="Right" VerticalContentAlignment="Center" Padding="0"/> <Label x:Name="PwdSpecialStr_Lbl" Content="Special characters" Grid.Column="1" Margin="0,2,2,2" Grid.Row="2" HorizontalAlignment="Right" VerticalContentAlignment="Center" Padding="0"/> <Label x:Name="PwdLength_Lbl" Content="Password length" Grid.Column="1" Margin="0,2,2,2" Grid.Row="3" HorizontalAlignment="Right" VerticalContentAlignment="Center" Padding="0"/> <TextBox x:Name="PwdLength_Tbx" Grid.Column="2" Height="23" Margin="4" TextWrapping="Wrap" VerticalContentAlignment="Center" Padding="4,0,0,0" Grid.Row="3" FontFamily="Segoe UI Light" Text="11"/> <Button x:Name="PwdGenSingle_Btn" Content="Generate Single Password" Grid.Column="3" Margin="4,2" Grid.Row="1" FontFamily="Segoe UI Light" FontStyle="Italic" Grid.ColumnSpan="2"/> <Button x:Name="PwdGenMultiple_Btn" Content="Generate Multiple Passwords" Grid.Column="3" Margin="4,2" Grid.Row="2" FontFamily="Segoe UI Light" FontStyle="Italic" Grid.ColumnSpan="2"/> <ListBox x:Name="GeneratedPwds_Lbx" Grid.Column="2" Margin="4,0" Grid.Row="5" Grid.RowSpan="2" FontFamily="Courier New"/> <Label x:Name="GeneratedPwds_Lbl" Content="Generated
passwords" Grid.Column="1" Margin="0,2,2,0" Grid.Row="5" HorizontalAlignment="Right" Padding="0" Grid.RowSpan="2"/> <TextBox x:Name="NmbrsOfPwds_Tbx" Grid.Column="4" Height="23" Margin="4" TextWrapping="Wrap" VerticalContentAlignment="Center" Padding="4,0,0,0" Grid.Row="3" FontFamily="Segoe UI Light" Text="10"/> <Label x:Name="NmbrsOfPwds_Lbl" Content="Password #" Grid.Column="3" Margin="0,2,2,2" Grid.Row="3" HorizontalAlignment="Right" VerticalContentAlignment="Center" Padding="0"/> <Button x:Name="CopySelPwd_Btn" Content="Copy Selected" Grid.Column="3" Margin="4,2" Grid.Row="5" FontFamily="Segoe UI Light" FontStyle="Italic"/> <Button x:Name="CopyAllPwd_Btn" Content="Copy All" Grid.Column="4" Margin="4,2" Grid.Row="5" FontFamily="Segoe UI Light" FontStyle="Italic"/> </Grid> </Window> |