Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Filter as autoload trigger; why does this work?

$
0
0

In my efforts to use PS consoles more and CMD consoles less, I've been "porting" my environment over. One of the things I can't live without is my 'cd aliases'. I have several aliases like: "src=cd /d c:\src" (loaded in doskey). Separate functions for each alias seemed kludgy, so after a bit of playing around, I came up the little module below to mimic the functionality. The function takes a look at the alias that invoked it and sets the location based on values in a hashtable.

Since the aliases are dynamically created and thus aren't available to trigger the autoload, I decided to add a filter to do the triggering (I could just import the module in my profile, but I try to keep my profile lean).

My thought was that I could type 'src' and if the module wasn't loaded, it would cause the autoload to happen which would override the filter 'src' with the alias 'src'. Typing 'src' a second time would actually set my location to my source directory. When I tested the 'filter autoload' however, I was pleasantly surprised that not only did it autoload the module, but it did the set-location on thefirst invocation.

My guess is that PS sees the command 'src' and when it's passed looks at the modules that can satisfy the command. Finding a filter named 'src' in CdAlias.psm1, it autoloads. Then PS, knowing that the needed module is now loaded, resolves 'src' against the now-current environment, finds the alias, resolves that to Set-XLocation and we are off to the races. That said, I'm only guessing here, and since this seems like something that could be useful in other places, it would be cool if someone could confirm my guess or explain what's really happening.

thanks,

K.

Note: This is the prototype, I will actually store the hashtable in the registry along with functions to edit the entries.

<#
.Synopsis
   Sets location based on the alias name used to call the function
.DESCRIPTION
   Uses the name of the alias used to call the function to look up
   a path in the script-scoped dictionary 'xlocation' then sets
   the location to that value.
.PARAMETER ChildPath
   Optional child path, if passed it will be joined with the base.
.NOTES
   Not sure why the filter trick works, but it does. Since src is my
   most commonly used alias location, I can use it to auto-load. What
   really confuses me is that it still sets the location to src the
   first time I type it.
#>
function Set-XLocation
{
    Param
    (
        [string]$ChildPath
    )

    $k = $MyInvocation.InvocationName
    if ($Script:xlocation.ContainsKey($k))
    {
        Set-Location -Path (Join-Path -Path $Script:xlocation[$k] -ChildPath $ChildPath)
    }
    else
    {
        Write-Warning "`$xlocation does not contain the key '$k'."
    }

}
$Script:xlocation = @{
    src = 'C:\src';
    one = 'C:\src\one';
    zero = 'C:\src\zero';
    mod = "$mod";
}
foreach ($k in $Script:xlocation.Keys) { Set-Alias -Name $k -Value Set-XLocation }

filter src
{
}

Export-ModuleMember -Function Set-XLocation -Alias *


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>