Skip to content

Instantly share code, notes, and snippets.

@adamskt
Last active October 24, 2016 16:42
Show Gist options
  • Save adamskt/21771391845cdd79397fc71ec6f54fd4 to your computer and use it in GitHub Desktop.
Save adamskt/21771391845cdd79397fc71ec6f54fd4 to your computer and use it in GitHub Desktop.
Moving Quickly Among Branches With PowerShell Dynamic Parameters
function Get-AllBuildFolders([string] $root) {
$search = [uri]::EscapeDataString("path:$($root) folder:Build child:Framework.ps1")
$uri = "http://localhost:8081/?s=$($search)&j=1&c=255&path_column=1" #Fix url to your Everything port
$result = Invoke-RestMethod -Uri $uri
return $result.results | % { Join-Path -Path $_.path -ChildPath $_.name }
}
function Get-Workspace {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)]
[ValidateSet("D:\Src\Main", "D:\Src\Branch1", "D:\Src\Branch2")] #Put your branch roots here
[string]
$branch
)
DynamicParam {
$ParameterName = 'workspace'
$attribute = new-object System.Management.Automation.ParameterAttribute
$attribute.ParameterSetName = "__AllParameterSets"
$attribute.Mandatory = $true
$attribute.Position = 1
$attributeCollection = new-object -Type System.Collections.ObjectModel.Collection[System.Attribute]
$attributeCollection.Add($attribute)
$values = Get-AllBuildFolders($branch)
$ValidateSet = new-object System.Management.Automation.ValidateSetAttribute($values)
$attributeCollection.Add($ValidateSet)
$dynamicParameter = new-object -Type System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $attributeCollection)
$paramDictionary = new-object -Type System.Management.Automation.RuntimeDefinedParameterDictionary
$paramDictionary.Add($ParameterName, $dynamicParameter)
return $paramDictionary
}
begin {
$ws = $PSBoundParameters[$ParameterName]
}
process {
cd $ws
}
end {}
}
Set-Alias goto Get-Workspace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment