Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

I *heart* PowerShell

When I saw the example in I *heart* python , and the remark about PowerShell in it, I could not resist doing the same exercise in PowerShell :

$codes = @{}

# some existing coded for testing and make sure we have at least one duplicate

$existingCodes = 'poipoi','blinky'
$codes['poipoi']= 0

# Create new codes

$r = new-object random
(
1..500)|% {
 
$tmp = ''
 
1..6 |% {$tmp += [char]$r.Next(97,123)}
 
$codes[$tmp]= 0
}

# Duplicates

$duplicates = $codes.keys|? {$existingCodes -contains $_}

# valid ones

$valid = $codes.keys|? {$existingCodes -notContains $_}

Remarks

  • I use the same dictionary trick, only in PowerShell it is called a HashTable and is declared like this @{}  
  • As I was to lazy to type the Alphabet I created the letter list first :
    • $letters = [char[]](97..122)
  • but later as I needed a random character anyway, I included this logic directly in the loop
  • As I suspect that we need the valid ones more as the Duplicates I store them in $valid

* Update * to filter out the letters (I missed in example above) the script again using $letters Without l or q

$Codes = @{}
$letters = [char[]](97..122) -notmatch 'l|q'
$r = new-object random
(1..500) |% {
  $tmp = ''
  1..6 |% {$tmp += $letters[($r.Next(0,24))]}
  $codes[$tmp] = 0
}

So if you do not have (Iron)Python handy, you can use PowerShell as well to create a on-the-fly list of password-style codes for e-xamit.

Enjoy,

Greetings /\/\o\/\/

Published Thursday, October 16, 2008 7:37 PM by MoW
Filed under:

Comments

# Interesting Finds: October 17, 2008

Friday, October 17, 2008 10:17 AM by Jason Haley
Anonymous comments are disabled