PoshCode Logo PowerShell Code Repository

Get-RandomNames by baschuel 19 months ago (modification of post by baschuel view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/3960"></script>download | new post

(Fun)Function to create a random list of Names (surname, givenname).
I use it to create a list of non existing persons to import in test environments.

  1. function Get-RandomNames {
  2. <#
  3. .SYNOPSIS
  4. Gets Full Names from a List of Names from http://names.mongabay.com
  5. .DESCRIPTION
  6. Downloads the Names from the Websites and randomizes the order of Names and gives back an Object with surname, lastname and gender
  7. .PARAMETER MaxNames
  8. Number of names returned by the function
  9. .PARAMETER Gender
  10. Gender of the names
  11. .EXAMPLE
  12. Get-RandomNames -Maxnames 20 -Gender Female
  13. .EXAMPLE
  14. Get-RandomNames
  15. .NOTES
  16. Name: Get-RandomNames
  17. Author: baschuel
  18. Date: 17.02.2013
  19. Version: 1.0
  20. Thanks to http://names.mongabay.com
  21. #>
  22.     [CmdletBinding()]
  23.     param (
  24.         [parameter(Position=0)]
  25.         [int]$MaxNames = 10,
  26.         [parameter(Position=1)]
  27.         [string]$Gender = "Male"      
  28.     )
  29.     BEGIN{
  30.         $surnameslink = "http://names.mongabay.com/most_common_surnames.htm"
  31.                 $malenameslink = "http://names.mongabay.com/male_names_alpha.htm"
  32.                 $femalenameslink = "http://names.mongabay.com/female_names_alpha.htm"
  33.     }#begin
  34.    
  35.     PROCESS{
  36.                
  37.                
  38.         function get-names ($url) {
  39.            
  40.             Try {
  41.            
  42.                 $web = Invoke-WebRequest -UseBasicParsing -Uri $url -ErrorAction Stop
  43.  
  44.                 $html = $web.Content
  45.  
  46.                 $regex = [RegEx]'((?:<td>)(.*?)(?:</td>))+'
  47.  
  48.                 $Matches = $regex.Matches($html)
  49.            
  50.                 $matches | ForEach-Object {
  51.                     If ($_.Groups[2].Captures[0].Value -ge 1) {
  52.                    
  53.                         $hash = @{Name = $_.Groups[2].Captures[0].Value;
  54.                                   Rank = [int]$_.Groups[2].Captures[3].Value}
  55.                         New-Object -TypeName PSObject -Property $hash
  56.                    
  57.                     }#If
  58.                 }#Foreach-Object
  59.  
  60.             } Catch {
  61.  
  62.                 Write-Warning "Can't access the data from $url."
  63.                 Write-Warning "$_.Exception.Message"
  64.                 Break
  65.  
  66.             }
  67.            
  68.         }#Function get-names
  69.  
  70.  
  71.         If ($Gender -eq "Male") {
  72.            
  73.             $AllMaleFirstNames = (get-Names $malenameslink).name
  74.             $AllSurnames = (get-names $surnameslink).name
  75.            
  76.             If ($AllMaleFirstNames.Count -le $AllSurnames.Count) {
  77.                 $UpperRange = $AllMaleFirstNames.Count
  78.             } else {
  79.                 $UpperRange = $AllSurnames.Count
  80.             }
  81.            
  82.  
  83.             If (($MaxNames -le $AllMaleFirstNames.Count) -and ($MaxNames -le $AllSurnames.Count)) {
  84.  
  85.                 1..$UpperRange |
  86.                 Get-Random -Count $MaxNames |
  87.                 ForEach-Object {
  88.                     $hash = @{Givenname = $AllMaleFirstNames[$_];
  89.                               Surname = $AllSurnames[$_];
  90.                               Gender = "Male"}
  91.                    
  92.                     $hash.Givenname = $($hash.Givenname[0]) + $hash.givenname.Substring(1,$hash.givenname.Length-1).ToLower()
  93.                     $hash.Surname = $($hash.Surname[0]) + $hash.surname.Substring(1,$hash.surname.Length-1).ToLower()
  94.                    
  95.                     New-Object -TypeName PSObject -Property $hash
  96.                 } # Foreach-Object
  97.  
  98.             } Else {
  99.    
  100.                 Write-Warning "Don't know so many names! Try a smaller number"
  101.  
  102.             }#If
  103.  
  104.         } elseIf ($Gender -eq "Female") {
  105.        
  106.             $AllFeMaleFirstNames = (get-Names $femalenameslink).name
  107.             $AllSurnames = (get-names $surnameslink).name
  108.            
  109.             If ($AllFeMaleFirstNames.Count -le $AllSurnames.Count) {
  110.                 $UpperRange = $AllMaleFirstNames.Count
  111.             } else {
  112.                 $UpperRange = $AllSurnames.Count
  113.             }
  114.             If (($MaxNames -le $AllFeMaleFirstNames.Count) -and ($MaxNames -le $AllSurnames.Count)) {
  115.  
  116.                 1..$UpperRange |
  117.                 Get-Random -Count $MaxNames |
  118.                 ForEach-Object {
  119.                     $hash = @{Givenname = $AllFeMaleFirstNames[$_];
  120.                               Surname = $AllSurnames[$_];
  121.                               Gender = "Female"}
  122.                    
  123.                     $hash.Givenname = $($hash.Givenname[0]) + $hash.givenname.Substring(1,$hash.givenname.Length-1).ToLower()
  124.                     $hash.Surname = $($hash.Surname[0]) + $hash.surname.Substring(1,$hash.surname.Length-1).ToLower()
  125.                    
  126.                     New-Object -TypeName PSObject -Property $hash
  127.                 } # Foreach-Object
  128.  
  129.             } Else {
  130.    
  131.                 Write-Warning "Don't know so many names! Try a smaller number"
  132.  
  133.             }#If
  134.         }#If
  135.        
  136.     }
  137.  
  138. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me