Filter as autoload trigger; why does this work?

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 the
first 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 *

I do not understand what the issue is.  If you have to create a filter for every alias you are not really gaining anything over defining a function or alias.
I do not believe the filter does anything except create a reference:
PS C:\src> get-alias src
CommandType     Name                                              
ModuleName
Alias           src -> Set-XLocation                              
CDAlias
¯\_(ツ)_/¯

Similar Messages

  • Work-around The Whine: why does this work? And is it BAD?

    Hello everybody, sorry to bring up The Whine again, but I was wondering if anyone can figure out why the following trick works to eliminate the noise.
    Also, could doing this regularly (all the time, basically) potentially be bad for the system in any way?
    This is the work-around I've been using:
    1. Open Photobooth, then Force Quit that application (you must Force Quit rather than simply quit)
    2. Put the Macbook to sleep, waiting a few seconds until the indicator light starts pulsing.
    3. Wake up the computer.
    Voila. That always does the trick, eliminating the whine until either the next time I boot up or the next time I open and quit Photobooth in the normal way.
    I suppose force quitting may keep the processor slightly busy somehow?
    Don't know...

    John P.,
    You say that all the notebooks that you know of make
    some kind of noise. How many notebooks is that? Not
    trying to be a jerk or anything but it better be like
    dozens and dozens to make this claim. I have heard
    the exact opposite of this claim from users that have
    owned a few notebooks as well.
    Hi Jerome,
    No problem... I'll clarify..
    5 notebooks personally, so when I say "all notebooks that I know of", it's a true claim because that is exactly what I'm saying.
    If this noise is caused by capacitor that is at some
    miniature threshold then why does the noise go away
    when said remedies are employed? Does the capacitor
    get bigger? Doesn't make sense but I appreiate that
    you want to reassure original poster.
    Actually I may have misspoken--it may be called a "voltage regulator" and it sits directly West of the CPU and controller chip. They do make noise and from what I have been told and remember, they can dampen it w/ a capacitor but that's about it. It's just the nature of the beast for that type of component, I'm told.
    What "I" think it may be is the power fluctuations, or lack thereof, in the Intel chip causing it--when you put a load on it, there's a power fluctuation because your CPU usage is going up/down a little bit, but when you're not doing anything it's very little it causes it to make a little noise. No big deal, but just as long as it's not blaring in your ear when you're trying to use your computer. My ZV6000 does the same thing, but it doesn't matter when you've got a load or not--seems to do it more when I have Cool 'n' Quiet enabled.
    This is not right. I am sorry.
    If this noise is normal then I'll take no MacBook.
    You know it is being fixed apparently on the MacBook
    Pros by motherboard replacement.
    I never said anything about pro. I don't own one, nor have I used one. I can't say how profound in comparison to the Macbook (non pro) it is.
    I'm sure if Apple thought it was a big enough problem, they would act either more quickly about it, but it's not stopping me from checking my e-mail or going on the Web.

  • Why does this work?

    I've been practicing some IPSec VPN scenarios, starting with the typical site-to-site VPN:
    Router A
    crypto map mymap 10 ipsec-isakmp
    set peer 1.1.1.2
    set transform-set myset
    match address 101
    access-list 101 permit ip 10.1.1.0 0.0.0.255 10.1.2.0 0.0.0.255
    Router B
    crypto map mymap 10 ipsec-isakmp
    set peer 1.1.1.1
    set transform-set myset
    match address 101
    access-list 101 permit ip 10.1.2.0 0.0.0.255 10.1.1.0 0.0.0.255
    This works as expected. Traffic from 10.1.1.x on Router A gets picked up by the crypto map and sent to RouterB and vice versa. What is confusing me is a more advanced scenario where I'm tunnelling GRE over IPSec with OSPF (why? Because I can).
    http://www.cisco.com/en/US/tech/tk583/tk372/technologies_configuration_example09186a008009438e.shtml
    Everything works just fine, but I'm bothered by the ACLs being used in the example. I don't see how everthing should be working- OSPF is fat and happy, and if I ping one spoke from another spoke from a loopback interface, it works as well. In this case, is the network traffic being picked up by the Tunnel interface first, and all the crypto map sees is GRE traffic by the time the packets make it that far?

    Ian
    You ask a question about the access lists and I believe that it will be easier to answer the question if we have the access lists to refer to. So here they are:
    7206:access-list 130 permit gre host 14.36.88.6 host 14.38.88.40access-list 140 permit gre host 14.36.88.6 host 14.38.88.20access-list 150 permit gre host 14.36.88.6 host 14.38.88.102610:access-list 120 permit gre host 14.38.88.10 host 14.36.88.63620:access-list 110 permit gre host 14.38.88.20 host 14.36.88.6
    3640:
    access-list 100 permit gre host 14.38.88.40 host 14.36.88.6
    one of the first things to understand here (and you briefly mention it in your post) is that they are running OSPF dynamic routing protocol over the tunnels. So each router sees the path to get to the other router as being through the tunnel. So when the 2610 wants to get to the 7206 it will send that traffic to the tunnel interface. The tunnel interface will encapsulate the original packet in a GRE header. So yes the only thing that the access list will see is GRE traffic.
    Anything that goes through the tunnel (whether it be a data packet or an OSPF routing update) is encapsulated in a GRE header and the access list does not see the original data but sees only GRE.
    HTH
    Rick

  • Why does this work? (dmenu_run shell script...)

    I'm very confused at what this is doing. dwm knowledge would help to understand my question. I don't understand how this script can take a font name with a space in it, the way dwm's default config does, and understand it as one argument.
    #!/bin/sh
    exe=`dmenu_path | dmenu ${1+"$@"}` && exec $exe
    Before updating to a newer version of dmenu (where this is included), I had written my own dmenu_run script. Although I wrote it, I actually don't understand how this one worked either. It was a trial and error thing, and when I try to think it through I always conclude that it shouldn't work.
    #!/bin/bash
    IFS="\t" # (was actually a literal tab character in my script)
    `dmenu_path | dmenu $*`
    This does not work without redefining $IFS, so I figured that dwm is not somehow making sure that spaces are interpretted correctly. But by stopping spaces from separating arguments, shouldn't this cause huge problems for everything except the font name?
    An example of how this would be called by dwm:
    dmenu_run -fn -*-dejavu sans-medium-r-normal-*-12-*-*-*-*-*-*-*
    (Note the space in the font name)
    So what is going on here? I realize that my question is somewhat confusing. I'm happy to clarify anything that is confusing - I found it very hard to clearly ask this question.
    Last edited by fflarex (2008-11-07 05:06:24)

    First off, sorry for all the shell-speak. This is a bit of an edge case, so it's sort of unavoidable. That said, I'll summarize what you need to know at the end.
    The dwm version of the script uses $@ in double quotes. @ contains all the parameters passed to the shell, just like * does. The main useful difference is a special case: when double-quoted, $@ expands to the list of arguments, each as a single shell word. Double-quoted $* expands to the list of arguments as a single word delimited by $IFS. (Both variables expand to the list of arguments [with word splitting applied according to $IFS, hence your spaces problem] when unquoted.) Here's the logic flow:
    1. dwm calls dmenu_run with the list of arguments specified in your config.h. It calls it directly, not running a command through a shell, so word splitting is never performed and spaces in values don't matter.
    2. dmenu_run calls dmenu with the list of arguments it was given. It uses two shell tricks: first, quoted-@ to pass the list of arguments without word splitting. Second, the ${foo+bar} expansion syntax, which expands to bar if foo is defined, otherwise the null string (""). So it expands to the full list of arguments, each as a single word, only if arguments were passed (if $1 is defined).
    What you need to know: You almost always want to use '"$@"'. Unless you want all your arguments as a single string, in which case you want '"$*"'. Other forms are practically useless.
    Last edited by skymt (2008-11-07 14:26:58)

  • Why does this work in MS SQL and not in Oracle

    ALTER TABLE Stk
    DROP COLUMN ComCode;
    This BASIC statement works perfectly
    in MS SQL, but after 2 hours now, it
    simply refuses to work in Oracle,
    although the syntax seems correct, I
    get :
    ALTER TABLE Stk
    DROP COLUMN ComCode
    ORA-00905: missing keyword
    null

    Stop trying! Oracle simply does not support the DROP COLUMN syntax. A work around is:
    CREATE another table based on the original table, minus the column you want to drop.
    (CREATE TABLE new_table AS SELECT col1, col2 , ... FROM old_table)
    DROP the original table.
    (DROP TABLE old_table)
    RENAME the new table the old table.
    (RENAME new_table TO old_table)
    Your have to manually add the constraints, if the old table has any.
    Eric Ma

  • I cannot send an email from my iPad 2? No problem receiving, why does this happen? Have tried the suggestions for setting up email and after doing the sync mail through iTunes receiving worked great but still cannot send? Any help would be great

    I cannot send an email from my iPad 2? No problem receiving, why does this happen? Have tried the suggestions for setting up email and after doing the sync mail through iTunes receiving worked great but still cannot send? Any help would be great!

    The fact that you can receive means you have a valid e mail address, and have established the connection to the incoming server, so all of that works.  Since the send does not work, that means your outgoing server is rejecting whatever settings you used formthe outgoing set up.  Try them again. 
    Google your particular isp, and ipad and many times you will find the exact settings needed for your isp.  Or tell us here, and soneone else may be on the same isp.  Some mail services need you to change a port, or have a unique name for the outgoing server.  
    Kep trying.

  • The monitor of my MacBook Pro suddenly became all black, I tried to reset but it did not work, what should I do? Why does this happen?

    The monitor of my MacBook Pro suddenly became all black, I tried to reset but it did not work, what should I do? Why does this happen?

    I advise to reply to the one you want to reply to.
    You have had good information here. I will not repeat that here.
    I would like to add:
    you have incompatible software: start in SafeMode, read Woodmeister and see if it is free of issues.
    Akamai is crap software needed or not, Huawei the same.
    It is very possible that the keygenerator you used for generating a key for some softwares has infected your mac.
    The non-regular software with the false key(s) are not compatible probably or generate malware. Luckily you can not update them automatically because the keygenerator blocked the software for contacting the developer... I propose to consider getting rid of those softwares by really good uninstalling.
    You have a beautiful mac, don't degenerate it with crapped software. There are alternatives for expensive software, for example Adobe Photoshop has a free alternative (with a less beautiful interface) in GIMP. And so on.
    Lex

  • TS4268 My iMessage doesn't work on my iPad. My software is up to date, wifi is working, apple ID is working but the verification stops and goes back to sign on. Why does this happen?

    My iMessage doesn't work on my iPad. My software is up to date, wifi is working, apple ID is working but the verification stops and goes back to sign on. Why does this happen?

    Look at no. 5 in this text which was copied from Apple's support site for troubleshooting Messages and FaceTime activation.
    Troubleshooting Apple ID activation
    After each step, toggle iMessage and FaceTime off and then on again in Settings > Messages and Settings > FaceTime.
    Update to the latest version of iOS.
    Ensure that you have an active Internet connection. You can complete activation using Wi-Fi or a cellular data connection. Check your Wi-Fi network using standard Wi-Fi network troubleshooting.
    If you receive an error when signing in to your Apple ID, visit myinfo.apple.com, sign in to your account, and ensure that the primary email address has been verified.
    Ensure that FaceTime has not been restricted: Settings > General > Restrictions > FaceTime.
    In Settings > General > Date & Time, ensure that Set Automatically is on. If Set Automatically is on, but the incorrect time zone is displayed, turn Set Automatically off and then choose the correct time zone, date, and time. Then turn Set Automatically on again.
    Enable iMessage and FaceTime while connected to another Wi-Fi network in a different location.
    This is the support kb article.
    http://support.apple.com/kb/ts4268

  • Combobox in array--why does it work this way?

    I have attached a snippet of a subVI I'm using to initialize the items and set the values of comboboxes in an array. I modified it slightly so the array being referenced is on this block diagram, not a top level diagram. It took me a while to come to this solution; before I had the combobox part in a for loop and was trying to loop through each array element, get a reference to that element, and set the strings[] and value properties but it was acting all wacky. Then I changed it to the way below and it automatically populates all the comboboxes with the correct values, and then sets their values each to be different (which is what I want!) My question is, why does it work this way? I understand all elements in an array need to be the same (although I did find a work around here.), but the part about setting the values to each be different I was trying to do manually with a property node and a value property, when I realized the combobox just handles it all itself. Is this just a feature of a combobox in an array I need to note for future reference? 
    edit: I noticed the testers array reference label changed, so hopefully it's still referencing the same control. You may need to relink it.
    CLA, LabVIEW Versions 2010-2013
    Attachments:
    combobox in array snippet.png ‏29 KB

    nathand wrote:
    Ben wrote:
    nathand wrote:
    You cannot get a reference to an individual element in an array control.  The ArrayElem reference will always refer to the same control regardless of what array index is visible.  This isn't specific to a combo-box, every array works like this.
    I have not tested it lately but as of the time of this Nugget I found that it will point to the top left visable array element.
    The ArrayElem property always returns the same reference no matter which element of the array is in the top-left corner, as demonstrated by the code below (screenshot instead of snippet to avoid mangled references and property nodes).
    Yes that is true but the value returned when using a property node >>> value will return the top left displayed value. In the code included in that Nugget, I set the size of the control such that it only shows a single element then by manipulating the index control I can raed each element in the array.
    At least that is how it behaved back in the day of that nugget.
    Yes, my wife left me home alone with a case of beer and LV to find that tidbit. I suspect that feature will one day disapear without anyone noticing.
    Ben
    Ben Rayner
    I am currently active on.. MainStream Preppers
    Rayner's Ridge is under construction

  • When ever i turn cellular data off then back on my mobile internet doesent work why does this happen?, when ever i turn cellular data off then back on my mobile internet doesent work why does this happen?

    when ever i turn cellular data off then back on my mobile internet doesent work why does this happen?, when ever i turn cellular data off then back on my mobile internet doesent work why does this happen?

    You only asked the question 4 times.  You have to repeat it at least 10 times to get an answer here, and then only if you supply useful info about your phone.

  • Why does this message keep appearing when i try to instal windows 7 through bootcamp? - "something went wrong and the USB cannot be configured for the installation"

    Why does this message keep appearing when Itry to instal windows 7 through bootcamp? - "Your bootable USB drive could not be created. An error occurred while copying the Windows installation files."

    Found a solution!
    Follow the extended version of these directions here:
    https://discussions.apple.com/docs/DOC-3581
    Worked like a charm!

  • HT2500 I sometimes get e-mails where the sender sends me several PDF's attachments. These attachments appear already opened on the e-mail unlike most PDF's which I open by clicking on the PDF icon. Why does this happen?

    I sometimes get e-mails with pdf attachments. However instead of opening a PDF icon the body of the e-mail message has the PDF's already opened. Why does this happen? Is there anything I can change on my mac book pro to make these PDF's come thru with their individual icons which can then be opened.

    This is a long-standing design flaw in Apple Mail.
    Prior to Mavericks, this command in Terminal would stop the annoying behavior:
    defaults write com.apple.mail DisableInlineAttachmentViewing -bool yes
    This Terminal command stopped working in Mail 7.1 under Mavericks. I have reported this to Apple as both a feature request and a bug. You should too.
    http://www.apple.com/feeback

  • HP Photosmart C8180 Printer! WHY does this keep happening every day??

    I have only had this for 3 months and it already has problems.
    It keeps telling me its out of paper when it is not.
    i always have at least 20 sheets in there!
    You can hear the rollers going but it can't pick up the paper and I only use high quality A4 paper in there.
    i then have to pull the tray out and push it back in, and yes i have the paper inserted in there correctly
    also, sometimes when i get this "out of paper" message, i ignore it and just press OK and it works fine 
    why does this keep happening?
    this is disgusting coming from a Quality brand like HP
    think its time to go to BBC Watchdog! 
    A printer is an item meant to print. 
    It is generally helpful if a printer recognizes such out-of-the-ordinary, eccentric, unheard-of media such as...oh, I dunno, 8.5 x 11-inch PAPER?????
    It claims repeatedly to have run out of paper. No paper. No paper. Insert paper into paper tray. Now, granted, the minimalist quality of the paper tray--it holds 20 pages, roughly--might convince you that I had indeed used up all my paper; this is not the case. 18 of the 20 sheets are still there. So I readjust that paper, close the tray. It starts to pull in a piece of paper, and then there is the giant crunchy noise of a paper jam in progress. And forget about “easy access” for removing them, either.
    In the months I have had this printer, I don’t believe it has EVER allowed me to print five pages consecutively without some sort of problem.  To me, this is known as an EPIC FAIL. 
    Message Edited by Waynester on 04-16-2009 04:09 PM

    So how is the printer set up?  Is it wireless or USB?  If USB you will need a cable, purchased seperately.  It is a standard A/B USB cable widely available inexpensively.  (The $1 cables form the dollar store work fine....
    Running the diagnostics at http://www.hp.com/go/tools may help resolve the issue.
    Bob Headrick,  HP Expert
    I am not an employee of HP, I am a volunteer posting here on my own time.
    If your problem is solved please click the "Accept as Solution" button ------------V
    If my answer was helpful please click the "Thumbs Up" to say "Thank You"--V

  • When I open about this mac - more info and I switch through the tabs (display,memory,storage,support,service) it lags (fps drops, animation looks choppy not smooth). Why does this happen? (over 300 gb free,same programs installed,free desk-downloads)

    When I open about this mac -> more info and I switch through the tabs (display,memory,storage,support,service) it lags (fps drops, animation looks choppy not smooth) even though all the mac (programs , interface ) works smooth . Why does this happen? (over 300 gb free,same programs installed,free desktop-downloads -no garbage there- everything neat and tidy.) Need some help. Only there it the animations goes like 2 fps and is not smooth. I do not understand why because the programs are the same so its the usage. Nothing is changed, yet i experience this little issue for a couple of days.

    OK, I'm confused. When I do About this Mac > More Info in OS10.9, I don't see any animation other than the window resizing. Is that the animation you are describing?
    Which if any of the following apply to your computer:
    1) Are you running any anti-virus/internet security applications?
    2) Are you running any "cleaning/tune-up/optimizations" applications?
    3) Any peer-to-peer or torrent downloading software?
    4) Any third-party disk backup software that came bundled with an external hard drive?
    5) Any online backup scheme other than iCloud (Carbonite; GoogleDrive; MS One Drive)?
    6) Did your financial institution ask you to install Trusteer EndPoint Protection (also known as Trusteer Rapport)?

  • I am always getting the message ' could not complete due to program error" Why does this happen

    I am always getting the message ' could not complete due to program error" Why does this happen

    Without knowing a lot more details we can't tell you anything. This is just the most generic Photoshop error message imaginable. What system are you on? What version of Photoshop? what are you doing when the error occurs? Provide as much info as you can. Refer to this as a start:
    Working with your Operating System’s Tools
    Mylenium

Maybe you are looking for

  • Excel changes needed from RTF Template

    Hi all, I created RTF template which it publish in excel format.I am getting correct report but here i want some Excel settings from RTF template. These all properties i want in Excel  output file which is generated from RTF. 1) Page number must be d

  • Create Webservice

    Hi all I want to create a webservice which will return me list of some objects. eg. I want to retrieve list of po's where each po can include po description, po number etc. PO    PONUMBER   PODESC I want my webservice to return the list of above type

  • Force persistent layout / initial view

    Hello @all, I can set the layout of a pdf document by the doc properties methods, f.e.: this.layout = "TwoColumnLeft"; But after saving the active pdf document these settings are lost. :-/ I need a way to make these settings persistent. Manuelly this

  • Why won't my creative cloud load?

    It stop doing this..why won't it load?

  • WDS bridge not working

    Based on the documentation and this message: Saxphile, "Use AX as an Ethernet bridge and a WDS relay simutaneously" #2, 03:39pm Oct 10, 2005 CDT I should be able to use an AEX in WDS mode to connect an ethernet device to my WLAN, using the the AEX as