Last active
August 29, 2015 14:16
-
-
Save mlapida/5902abfa54e9f7ef986d to your computer and use it in GitHub Desktop.
A PowerShell script to spit out a random Marvel Superhero when called. This is great for random server naming and automation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Hat Tip http://www.virtuallyghetto.com/2014/02/having-some-fun-with-marvel-comics-api.html | |
#Instructions: Aquire a Marvel API key from https://developer.marvel.com and place them in lines 63 & 64. Import this module into a VM build script and call Get-Hero to grab a random Marvel super hero. | |
#Imported Get-Hash function (credits http://dbadailystuff.com/2013/03/11/get-hash-a-powershell-hash-function/) | |
function Get-Hash | |
{ | |
Param | |
( | |
[parameter(Mandatory=$true, ValueFromPipeline=$true, ParameterSetName="set1")] | |
[String] | |
$text, | |
[parameter(Position=0, Mandatory=$true, ValueFromPipeline=$false, ParameterSetName="set2")] | |
[String] | |
$file = "", | |
[parameter(Mandatory=$false, ValueFromPipeline=$false)] | |
[ValidateSet("MD5", "SHA", "SHA1", "SHA-256", "SHA-384", "SHA-512")] | |
[String] | |
$algorithm = "SHA1" | |
) | |
Begin | |
{ | |
$hashAlgorithm = [System.Security.Cryptography.HashAlgorithm]::Create($algorithm) | |
} | |
Process | |
{ | |
$md5StringBuilder = New-Object System.Text.StringBuilder 50 | |
$ue = New-Object System.Text.UTF8Encoding | |
if ($file){ | |
try { | |
if (!(Test-Path -literalpath $file)){ | |
throw "Test-Path returned false." | |
} | |
} | |
catch { | |
throw "Get-Hash - File not found or without permisions: [$file]. $_" | |
} | |
try { | |
[System.IO.FileStream]$fileStream = [System.IO.File]::Open($file, [System.IO.FileMode]::Open); | |
$hashAlgorithm.ComputeHash($fileStream) | % { [void] $md5StringBuilder.Append($_.ToString("x2")) } | |
} | |
catch { | |
throw "Get-Hash - Error reading or hashing the file: [$file]" | |
} | |
finally { | |
$fileStream.Close() | |
$fileStream.Dispose() | |
} | |
} | |
else { | |
$hashAlgorithm.ComputeHash($ue.GetBytes($text)) | % { [void] $md5StringBuilder.Append($_.ToString("x2")) } | |
} | |
return $md5StringBuilder.ToString() | |
} | |
} | |
function Get-Hero | |
{ | |
#You can obtain a Marvel API key from https://developer.marvel.com | |
$MarvelPublic = "your public key" | |
$MarvelPrivate = "your private key" | |
$MarvelRandom = Get-Random | |
$MarvelOffset = Get-Random -Maximum 1484 | |
#Form the hash as Marvel requires | |
$tohash = $MarvelRandom.ToString() + $MarvelPrivate.ToString() + $MarvelPublic.ToString() | |
$md5 = Get-Hash -text $tohash -algorithm MD5 | |
#Call the API gatway | |
$url = "https://gateway.marvel.com:443/v1/public/characters?&offset=$MarvelOffset&limit=1&apikey=$MarvelPublic&hash=$md5&ts=$MarvelRandom" | |
$results = Invoke-WebRequest $url | |
$superheros = $results.Content | |
$output = ConvertFrom-Json $superheros | |
#Remove spaces from the results | |
($output.data.results.name).Split(" ") | ForEach { | |
$SemiCleanHero += $_ | |
} | |
#Remove periods from the results | |
($SemiCleanHero).Split(".") | ForEach { | |
$CleanHero += $_ | |
} | |
#Only include anything to the left of a parenthesis | |
$CleanHero = $CleanHero.Split("(")[0] | |
return $CleanHero | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment