<# Get-TempPw.ps1 alan dot kaplan at va dot gov 9-7-2015 This script creates passwords based on random word dictionary plus number and character One letter of the word is randomly capitalized, with the random number(s) and character(s) appended to the end. The purpose of the script is to create a more user friendly temporary password for password resets. #> #Static settings #Minimum length of random word $minWordLen = 8 #Minimum length of random numbers $numcount = 1 #Minimum length of random special characters $speccount = 1 [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null $WebClient = New-Object System.Net.WebClient $WebClient.Encoding = [System.Text.Encoding]::"Default" # Password list based on http://blogs.technet.com/b/heyscriptingguy/archive/2013/06/03/generating-a-new-password-with-windows-powershell.aspx #Numbers characters only $a=$NULL $ASCIINumbers=$NULL;For ($a=48;$a –le 57;$a++) {$ASCIINumbers+=,[char][byte]$a } #Special are in multiple places in the table #To include space, start at 32 # !" # $ % & ' ( ) * + , - . / $ASCIISpecial=$NULL; $a=$Null For ($a=33;$a –le 47;$a++) {$ASCIISpecial+=,[char][byte]$a } #http://sqlchow.wordpress.com/2013/03/04/shuffle-the-deck-using-powershell/ Function Get-ShuffledArray { param( [Array]$gnArr ) $len = $gnArr.Length; while($len) { $i = Get-Random ($len --); $tmp = $gnArr[$len]; $gnArr[$len] = $gnArr[$i]; $gnArr[$i] = $tmp; } return $gnArr } Function Get-RandCharacters() { Param( [int]$length=10, [string[]]$sourcedata ) For ($loop=1; $loop –le $length; $loop++) { $TempPassword+=($sourcedata | GET-RANDOM) } return $TempPassword } Function Capitalize-nthLetter($word, $pos){ $arr=($word)[0..$word.Length] $arr[$pos] = $($arr[$pos].ToString()).toupper() $arr -join "" } #http://superuser.com/questions/611986/how-to-prevent-carriage-return-being-copied-to-clipboard-in-powershell function Out-ClipboardText { param( [Parameter( Position=0, Mandatory=$true, ValueFromPipeline=$true) ] [String]$text ) process { powershell -sta -noprofile -command "add-type -an system.windows.forms; [System.Windows.Forms.Clipboard]::SetText('$text')" } } Function Main (){ #Get a word at least 8 characters from randomword.setgetgo.com do { [string]$RandWord = ($WebClient.DownloadString("http://randomword.setgetgo.com/get.php")).trim() } Until ($RandWord.Length -ge $minWordLen ) #Pick a random number between zero and the Maxlength -1 $maxLen = $RandWord.Length -1 $i = get-Random -Minimum 0 -Maximum ($maxLen -1) #Capitalize a letter $RandWord = Capitalize-nthLetter $RandWord $i #Get a number $Nums= GET-RandCharacters -length $numcount -sourcedata $ASCIINumbers #Get a special character $Spec = GET-RandCharacters -length $speccount -sourcedata $ASCIISpecial #combine, ensuring no extra lines $txt = "$Nums$Spec".trim() $Txt = [regex]::split($txt,"") #randomize order of characters $RandTxt =Get-ShuffledArray $Txt $ToEnd = ($RandTxt -join("")).Trim() return "$RandWord$ToEnd" } do { $NewPass = Main $result = [System.Windows.Forms.MessageBox]::Show("Accept and Copy $NewPass to Clipboard?","Review", "YesNo” , "Question” , "Button1") } while ($result.value__ -ne 6) #Use function because Clip adds a CR $NewPass | Out-ClipboardText $wshShell = new-object -comobject wscript.shell $r=$wshShell.popup("Password copied",3,"Done",1)