Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get-Mockaroo
# 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 www.mockaroo.com
.DESCRIPTION
Function to automatically get mock data from 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
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment