install-bosh.ps1


<#PSScriptInfo
 
.VERSION 1.1
 
.GUID e1cee36d-9a20-4813-bc80-20fd1f82e2ba
 
.AUTHOR Kbott@pivotal.io
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
.PRIVATEDATA
 
#>


<#
 
.DESCRIPTION
 Install BoSH for Windows
 
#>
 
[CmdletBinding()]
param(
    [Parameter(Mandatory = $true)]
    [ValidateScript({
        write-verbose $_
        if(-Not ( split-path -LiteralPath $_ | Test-Path ) ){
            throw "Folder does not exist or is not a container"
        }
        return $true
    })]
        $DownloadDir
)
DynamicParam {
    function get-releases {
        [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
        $request = Invoke-WebRequest -UseBasicParsing -Uri https://github.com/cloudfoundry/bosh-cli/releases
        $windowsRelease = $request.links | where href -Match windows-amd64.exe
        $releases = $windowsRelease | ForEach-Object {($_.href -split "/")[-2]}
        write-verbose "getting releases"
        Write-Output $releases
    }
    function New-DynamicParam {
        param(

            [string]
            $Name,

            [string[]]
            $ValidateSet,

            [switch]
            $Mandatory,

            [string]
            $ParameterSetName = "__AllParameterSets",

            [int]
            $Position,

            [switch]
            $ValueFromPipelineByPropertyName,

            [string]
            $HelpMessage,

            [validatescript( {
                    if (-not ( $_ -is [System.Management.Automation.RuntimeDefinedParameterDictionary] -or -not $_) ) {
                        Throw "DPDictionary must be a System.Management.Automation.RuntimeDefinedParameterDictionary object, or not exist"
                    }
                    $True
                })]
            $DPDictionary = $false

        )
        #Create attribute object, add attributes, add to collection
        $ParamAttr = New-Object System.Management.Automation.ParameterAttribute
        $ParamAttr.ParameterSetName = $ParameterSetName
        if ($mandatory) {
            $ParamAttr.Mandatory = $True
        }
        if ($Position -ne $null) {
            $ParamAttr.Position = $Position
        }
        if ($ValueFromPipelineByPropertyName) {
            $ParamAttr.ValueFromPipelineByPropertyName = $True
        }
        if ($HelpMessage) {
            $ParamAttr.HelpMessage = $HelpMessage
        }

        $AttributeCollection = New-Object 'Collections.ObjectModel.Collection[System.Attribute]'
        $AttributeCollection.Add($ParamAttr)

        #param validation set if specified
        if ($ValidateSet) {
            $ParamOptions = New-Object System.Management.Automation.ValidateSetAttribute -ArgumentList $ValidateSet
            $AttributeCollection.Add($ParamOptions)
        }


        #Create the dynamic parameter
        $Parameter = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameter -ArgumentList @($Name, [string], $AttributeCollection)

        #Add the dynamic parameter to an existing dynamic parameter dictionary, or create the dictionary and add it
        if ($DPDictionary) {
            $DPDictionary.Add($Name, $Parameter)
        }
        else {
            $Dictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
            $Dictionary.Add($Name, $Parameter)
            $Dictionary
        }
    }
    $releases = get-releases
    $releaselist = @()
    foreach ($release in $releases ) {
        Write-Verbose $release
        $releaselist += $release


    }
    New-DynamicParam -Name BoSHRelease -ValidateSet $releaselist  -Mandatory
}
Begin {
    foreach ($param in $PSBoundParameters.Keys) {
        if (-not ( Get-Variable -name $param -scope 0 -ErrorAction SilentlyContinue ) -and "Verbose", "Debug" -notcontains $param ) {
            New-Variable -Name $Param -Value $PSBoundParameters.$param -Description DynParam
            Write-Verbose "Adding variable for dynamic parameter '$param' with value '$($PSBoundParameters.$param)'"
        }
    }

}

Process {
    write-host "Downloading release $BoSHRelease"
    New-Item -ItemType Directory -Path $DownloadDir -Force | Out-Null
    $Outfile = "$DownloadDir/bosh.exe"
    $BOSHsemver = $BoSHRelease -replace "v"
    Invoke-WebRequest -UseBasicParsing -Uri https://github.com/cloudfoundry/bosh-cli/releases/download/$($BoSHRelease)/bosh-cli-$($BoSHsemver)-windows-amd64.exe -OutFile $Outfile
    Unblock-File $Outfile
    
}
end {
    $object = New-Object psobject
    $object  | Add-Member -MemberType NoteProperty -Name Path -Value $Outfile
    $object  | Add-Member -MemberType NoteProperty -Name Version -Value (.$Outfile -version)
    Write-Output $object
}