install-credhub-cli.ps1
<#PSScriptInfo .VERSION 1.1 .GUID 0bb15bdb-4efa-46e4-95ff-b36cf8bedc01 .AUTHOR Kbott@pivotal.io .COMPANYNAME .COPYRIGHT .TAGS .LICENSEURI .PROJECTURI .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES .PRIVATEDATA #> <# .DESCRIPTION Install CF CLI Commandline Interface for Cloudfoundry #> # This helper script downloads an available om Version [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-incubator/credhub-cli/releases $windowsRelease = $request.links | where href -Match credhub-windows $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 CredhubRelease -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 $credhubRelease" New-Item -ItemType Directory -Path $DownloadDir -Force | Out-Null $Outfile = "$DownloadDir/credhub-windows-$($credhubrelease).tgz" Invoke-WebRequest -UseBasicParsing -Uri "https://github.com/cloudfoundry-incubator/credhub-cli/releases/download/$($credhubrelease)/credhub-windows-$($credhubrelease).tgz" -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 } |