function push-project() {
[CmdletBinding()]
Param(
# Any other parameters can go here
)
DynamicParam {
# Set the dynamic parameters' name
$ProjectFolderPath = '[add projects folder here]'
$ParameterName = 'project'
# Create the dictionary
$RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
# Create the collection of attributes
$AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
# Create and set the parameters' attributes
$ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
$ParameterAttribute.Mandatory = $true
$ParameterAttribute.Position = 1
# Add the attributes to the attributes collection
$AttributeCollection.Add($ParameterAttribute)
# Generate and set the ValidateSet
$arrSet = Get-ChildItem -Path C:\Users\Ollie.Vaughan\Projects -Directory | Select-Object -ExpandProperty Name
$ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)
# Add the ValidateSet to the attributes collection
$AttributeCollection.Add($ValidateSetAttribute)
# Create and return the dynamic parameter
$RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection)
$RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter)
return $RuntimeParameterDictionary
}
begin {
# Bind the parameter to a friendly variable
$project = $PsBoundParameters[$ParameterName]
}
process {
$ProjectFolderPath += $project
push-location $ProjectFolderPath;
Invoke-Item .;
Invoke-Item *.sln;
}
}
new-alias -name pp -value push-project
Wednesday, 13 April 2016
Powershell script to open a project and an explorer window in the project root from powershell - cmd completion included
Idea pulled from the tutorial by Jim Christopher:
Pluralsight - Everyday PowerShell
Completed entirely using Martin Schvartzman's code:
# dynamic-validateset-in-a-dynamic-parameter
Monday, 4 April 2016
Subscribe to:
Posts (Atom)