Last active
          January 16, 2017 09:31 
        
      - 
      
- 
        Save Sarafian/20eb2a449d6e2d6260015de60946c6fb to your computer and use it in GitHub Desktop. 
    Download the AWS cloud formation samples in both JSON and AWS format
  
        
  
    
      This file contains hidden or 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
    
  
  
    
  | param( | |
| [Parameter(Mandatory=$false)] | |
| [ValidateSet("ap-northeast-1","ap-northeast-2","ap-south-1","ap-southeast-1","ap-southeast-2","ca-central-1","eu-central-1","eu-west-1","eu-west-2","sa-east-1","us-east-1","us-east-2","us-west-1","us-west-2")] | |
| [string]$Region="eu-west-1", | |
| [Parameter(Mandatory=$false)] | |
| [string]$OutputPath="$env:USERPROFILE\Documents\AWSCloudFormation-samples-json-yaml" | |
| ) | |
| Set-StrictMode -Version latest | |
| $rubyPath=& "C:\Windows\System32\where.exe" ruby 2>&1 | |
| if(-not $?) | |
| { | |
| Write-Warning "ruby.exe is not available. Stopping" | |
| return | |
| } | |
| # US East (Northern Virginia) Region url is | |
| # https://s3.amazonaws.com/cloudformation-examples-us-east-1/AWSCloudFormation-samples.zip | |
| if($region -eq "us-east-1") | |
| { | |
| $sampleUrl="https://s3.amazonaws.com/cloudformation-examples-$region/AWSCloudFormation-samples.zip" | |
| } | |
| else | |
| { | |
| $sampleUrl="https://s3-$region.amazonaws.com/cloudformation-examples-$region/AWSCloudFormation-samples.zip" | |
| } | |
| $tempSamplePath=Join-Path $env:TEMP "AWSCloudFormation-samples.zip" | |
| if(Test-Path -Path $tempSamplePath) | |
| { | |
| Remove-Item -Path $tempSamplePath -Force | |
| } | |
| Invoke-WebRequest -Uri $sampleUrl -UseBasicParsing -OutFile $tempSamplePath | |
| $expandPath=Join-Path $env:TEMP "AWSCloudFormation-samples" | |
| if(Test-Path -Path $expandPath) | |
| { | |
| Remove-Item -Path $expandPath -Recurse -Force | |
| } | |
| Expand-Archive -Path $tempSamplePath -DestinationPath $expandPath | |
| Get-ChildItem -Path $expandPath |ForEach-Object { | |
| $jsonPath="$($_.FullName).json" | |
| $yamlPath="$($_.FullName).yaml" | |
| $_|Rename-Item -NewName $jsonPath | |
| $rubyArgs=@( | |
| "-ryaml" | |
| "-rjson" | |
| "-e" | |
| "puts YAML.dump(JSON.parse(File.read('$jsonPath')))" | |
| ) | |
| Write-Host "Converting $jsonPath to $yamlPath" | |
| & $rubyPath $rubyArgs 2>&1 |Out-File -Encoding ascii -FilePath $yamlPath -Force | |
| } | |
| if(-not (Test-Path $OutputPath -PathType Container)) | |
| { | |
| $null=New-Item $OutputPath -ItemType Directory | |
| } | |
| Move-Item -Path "$expandPath\*.*" -Destination $OutputPath -Force | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
Requires ruby to be installed. Quick way to install with Chocolatey
choco install ruby -y.Usage examples