Author Posts

August 28, 2017 at 8:48 pm

I'm looking for a way to use push-location with a function's parameter. My goal is that a user will type "Import-SomeFile -Path ". When the user hits tab, then -Path would push-location to a network share to recurse through that directory. Is this possible/feasible/advisable?

August 28, 2017 at 8:52 pm

So, you want to enable parameter auto-complete for some pre-determined network location?

August 28, 2017 at 8:58 pm

Yes sir, that's the goal.

August 28, 2017 at 8:58 pm

Start here – https://foxdeploy.com/2017/01/13/adding-tab-completion-to-your-powershell-functions/. Stephen did an excellent job laying out the basic framework. You need a Dynamic Parameter.

August 28, 2017 at 9:00 pm

Fantastic, thanks for the blazing fast responses. I'll take a look!

August 29, 2017 at 12:36 pm

Hi Colin,

In addition to the above, functionality for dynamic ValidateSet is coming in the next PowerShell release, keep an eye out!
https://github.com/PowerShell/PowerShell/pull/3784

Liam

August 29, 2017 at 1:37 pm

Liam, thanks for the heads up! This is my first experience with Dynamic Parameters and they do seem to have a lot of code around them for this functionality to work, so it'll be exciting to see that get folded in and make it more succinct. I'll keep an eye out.

August 29, 2017 at 2:00 pm

Here's an example of the unreleased PowerShell 6 functionality (as per your requirements 'caus I'm nice)

class ValidateSetPath : System.Management.Automation.IValidateSetValuesGenerator
{
    [string[]] GetValidValues()
    {
        return (Get-ChildItem -Path '\\Network\Path' -File).Name
    }
}

function Test-ValidateSet
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [ValidateSet([ValidateSetPath])]
        [string[]]
        $Item
    )

    $Item | ForEach-Object {
        $Path = Join-Path -Path '\\Network\Path' -ChildPath $_

        Get-Item -Path $Path
    }
}

August 29, 2017 at 2:34 pm

That's super helpful. I'm going to get that incorporated right in! 🙂

August 29, 2017 at 2:42 pm

Not too soon, it's still in beta 😛