Push-Location as a function's parameter

This topic contains 9 replies, has 3 voices, and was last updated by  Liam Fisher 3 months, 2 weeks ago.

  • Author
    Posts
  • #78205

    Colin Nichols
    Participant

    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?

  • #78207

    Don Jones
    Keymaster

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

  • #78214

    Don Jones
    Keymaster

    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.

    • #78216

      Colin Nichols
      Participant

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

  • #78272

    Liam Fisher
    Participant

    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

    • #78289

      Colin Nichols
      Participant

      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.

    • #78301

      Liam Fisher
      Participant

      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
          }
      }
      
    • #78310

      Colin Nichols
      Participant

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

    • #78311

      Liam Fisher
      Participant

      Not too soon, it's still in beta 😛

You must be logged in to reply to this topic.