UPDATE: The function now also supports creating dynamic data sets!

Have you ever been in a situation where you wish you had some random data lying around while testing some code or whatnot? I do all the time. What I usually do is hunt around my disk after some random csv that I can use. Then someone on Twitter posted about an online random generator called Mockaroo. And guess what, it has an API.

I quickly cobbled together a wrapper function that you can use to generate data in csv format. Head on over to http://www.mockaroo.com/ and register an account to get your free API key, create some schemas (data sets) and you are good to go.

The API supports creating data sets on the go, but the current version of the wrapper only supports pre-made ones, so if you want to contribute, head on over to my gist at github, fork my code and help make it better!

# the EnumUtils code was nicked from Wayne Hartman (http://blog.waynehartman.com/articles/84.aspx)
Add-Type TypeDefinition @"
using System;
using System.Reflection;
using System.ComponentModel;
public enum MockarooType
{
Boolean,
City,
Color,
[DescriptionAttribute("Company Name")]CompanyName,
Country,
[DescriptionAttribute("Country Code")]CountryCode,
[DescriptionAttribute("Credit Card #")]CreditCardNum,
[DescriptionAttribute("Credit Card Type")]CreditCardType,
Currency,
[DescriptionAttribute("Currency Code")]CurrencyCode,
[DescriptionAttribute("Domain Name")]DomainName,
[DescriptionAttribute("Email Address")]EmailAddress,
[DescriptionAttribute("File Name")]FileName,
[DescriptionAttribute("First Name")]FirstName,
[DescriptionAttribute("First Name (Euoropean)")]FirstNameEuropean,
[DescriptionAttribute("First Name (Female)")]FirstNameFemale,
[DescriptionAttribute("First Name (Male)")]FirstNameMale,
Frequency,
[DescriptionAttribute("Full Name")]FullName,
Gender,
[DescriptionAttribute("Gender (abbrev)")]GenderAbbrev,
GUID,
[DescriptionAttribute("IP Address v4")]IPv4,
[DescriptionAttribute("IP Address v6")]IPv6,
[DescriptionAttribute("Job Title")]JobTitle,
Language,
[DescriptionAttribute("Last Name")]LastName,
Latitude,
Longitude,
[DescriptionAttribute("MAC Address")]MACAddress,
[DescriptionAttribute("MIME Type")]MIMEType,
Password,
Phone,
[DescriptionAttribute("Postal Code")]PostalCode,
Race,
[DescriptionAttribute("Row Number")]RowNumber,
State,
[DescriptionAttribute("State (abbrev)")]StateAbbrev,
[DescriptionAttribute("Street Address")]StreetAddress,
[DescriptionAttribute("Street Name")]StreetName,
[DescriptionAttribute("Street Number")]StreetNumber,
[DescriptionAttribute("Street Suffix")]StreetSuffix,
Suffix,
[DescriptionAttribute("Time Zone")]TimeZone,
Title,
[DescriptionAttribute("Top Level Domain")]TLD,
Username
}
public class EnumUtils
{
public static string stringValueOf(Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes = (DescriptionAttribute[]) fi.GetCustomAttributes( typeof(DescriptionAttribute), false);
if (attributes.Length > 0)
{
return attributes[0].Description;
}
else
{
return value.ToString();
}
}
public static object enumValueOf(string value, Type enumType)
{
string[] names = Enum.GetNames(enumType);
foreach (string name in names)
{
if (stringValueOf((Enum)Enum.Parse(enumType, name)).Equals(value))
{
return Enum.Parse(enumType, name);
}
}
throw new ArgumentException("The string is not a description or value of the specified enum.");
}
}
"@
function Get-Mockaroo {
<#
.SYNOPSIS
Get mock data from http://www.mockaroo.com
.DESCRIPTION
Function to automatically get mock data from http://www.mockaroo.com.
You need to register to get API key. Using the free key you can issue up to 1000 request per day.
.EXAMPLE
Get-Mockaroo -APIKey $myApiKey -Schema 'test' -Count 10 | ConvertFrom-Csv | Format-Table
Get 10 rows from the saved schema called 'test', formatted as a table.
.EXAMPLE
Get-Mockaroo -APIKey $myApiKey -Fields @(
(New-MockarooField 'Name' -Type FullName),
(New-MockarooField 'City' -Type City),
(New-MockarooField 'Company' -Type CompanyName),
(New-MockarooFieldCustomList 'Type' -Values Retail,Online),
(New-MockarooFieldNumber 'Years' -Min 1 -Max 30 -PercentBlank 10 -Decimals 0),
(New-MockarooFieldMoney 'Total Sale' -Min 1000 -Max 10000)
) -Count 10 | ConvertFrom-Csv | Format-Table
Create a custom query using an array of field specifications.
.NOTES
Author: Øyvind Kallstad
Date: 21.11.2014
Version: 1.1
#>
[CmdletBinding(DefaultParameterSetName = 'Schema')]
param (
# Your API key
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string] $APIKey,
# Name of saved schema
[Parameter(Mandatory, ParameterSetName = 'Schema')]
[ValidateNotNullOrEmpty()]
[string] $Schema,
# Array of fields
[Parameter(Mandatory, ParameterSetName = 'Fields')]
[ValidateNotNullOrEmpty()]
[object[]] $Fields,
# Number of rows to return (when using with a schema, 1 = all rows)
[Parameter()]
[ValidateNotNull()]
[int] $Count = 1
)
if ($Fields) {
# convert field object(s) to json
$fieldsJSON = $Fields | ConvertTo-Json Compress
# if array count 1, we need to manually add [ and ] around the json string for the API call to work
if ($Fields.Count -eq 1) {
$fieldsJSON = $fieldsJSON.Insert(($fieldsJSON.LastIndexOfAny($fieldsJSON[-1]))+1,']').Insert(0,'[')
}
Write-Output (Invoke-RestMethod Uri ([uri]"http://www.mockaroo.com/api/generate.csv?key=$($APIKey)&count=$($Count)&fields=$($fieldsJSON)"))
}
else {
Write-Output (Invoke-RestMethod Uri ([uri]"http://www.mockaroo.com/api/generate.csv?key=$($APIKey)&count=$($Count)&schema=$($Schema)"))
}
}
function New-MockarooFieldNumber {
[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory)]
[ValidateNotNullorEmpty()]
[string] $Name,
# The minimum value
[Parameter()]
[ValidateRange(1,[Int32]::MaxValue)]
[int] $Min = 1,
# The maximum value
[Parameter()]
[ValidateRange(1,[Int32]::MaxValue)]
[int] $Max = 100,
# The number of decimals
[Parameter()]
[ValidateRange(0,10)]
[int] $Decimals = 2,
# An integer between 0 and 100 that determines what percent of the generated values will be null
[Parameter()]
[ValidateRange(0,100)]
[int] $PercentBlank = 0
)
Write-Output ([PSCustomObject][Ordered]@{
name = $Name
type = 'Number'
min = $Min
max = $Max
decimals = $Decimals
percentBlank = $PercentBlank
})
}
function New-MockarooFieldMoney {
[CmdletBinding()]
param (
# The name of the field
[Parameter(Position = 0, Mandatory)]
[ValidateNotNullorEmpty()]
[string] $Name,
# The minimum value
[Parameter()]
[ValidateRange(1,[Int32]::MaxValue)]
[int] $Min = 1,
# The maximum value
[Parameter()]
[ValidateRange(1,[Int32]::MaxValue)]
[int] $Max = 10,
# Currency symbol
[Parameter()]
[ValidateSet('$','£','','¥','random','none')]
[string] $Symbol = '$',
# An integer between 0 and 100 that determines what percent of the generated values will be null
[Parameter()]
[ValidateRange(0,100)]
[int] $PercentBlank = 0
)
Write-Output ([PSCustomObject][Ordered]@{
name = $Name
type = 'Money'
min = $Min
max = $Max
symbol = $Symbol
percentBlank = $PercentBlank
})
}
function New-MockarooFieldCustomList {
[CmdletBinding()]
param (
# The name of the field
[Parameter(Position = 0, Mandatory)]
[ValidateNotNullorEmpty()]
[string] $Name,
# An array of values to pick from. Each value should be a string.
[Parameter()]
[string[]] $Values,
[Parameter()]
[ValidateSet('random','sequential')]
[string] $SelectionStyle = 'random'
)
Write-Output ([PSCustomObject][Ordered]@{
name = $Name
type = 'Custom List'
values = $Values
selectionStyle = $SelectionStyle
})
}
function New-MockarooField {
[CmdletBinding()]
param (
# The name of the field
[Parameter(Position = 0, Mandatory)]
[ValidateNotNullorEmpty()]
[string] $Name,
# The data type of the field
[Parameter()]
[MockarooType] $Type,
# An integer between 0 and 100 that determines what percent of the generated values will be null
[Parameter()]
[ValidateRange(0,100)]
[int] $PercentBlank = 0
)
Write-Output ([PSCustomObject][Ordered]@{
name = $Name
type = [EnumUtils]::stringValueOf([MockarooType]::$Type)
percentBlank = $PercentBlank
})
}

view raw
Get-Mockaroo.ps1
hosted with ❤ by GitHub

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s