/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" Math "," Exploring .NET types, classes and Enums from MSH "," Wipe the screen in MSH "," Blog series about Errorhandling in MSH "," This Month's line-break and Pipelines are Back "," [msh]cd.. "," MSH start. Function "," Argggg "," Where are my pipelines "," WMI help Part 3 (Methods) "," ")

Sunday, November 13, 2005

 


Get driveletters for All drives (Providers) in MSH



You might have noticed that you can change drive in MSH by just typing the driveletter and : and hit enter (e.g. A: c: as we would expect, same as in CMD) but this does not work for the non-disk providers as HKLM: Functions: etc.

as you want to do a LS in the Function:-"disk"
You have to do like this (for now) :

cd function:
ls

as just using Fuction: [enter] does not work :

MSH>function:
'function:' is not recognized as a Cmdlet, function, operable program, or script file.


Another interesting thing you see when you do the LS is that this (the DriveLetters) are only functions that call Set-Location :

if you look in Profile.MSH you will find this piece of Code :

& {
    for ($i = 0; $i -lt 26; $i++) 
    { 
        $funcname = ([System.Char]($i+65)) + ':'
        $str = "function global:$funcname { set-location $funcname } " 
        invoke-command $str 
    }
}


This makes a loop trough the alphabet from A-Z and Makes the Functions :

so if we do the Same for the get_drive output, we get All the Providers (-1) as driveletter-functions :

get-drive | forEach {$str = "function global:$($_.name): { set-location $($_.name): } ";invoke-command $str}


so we can change to All providers like this (as my PWD is in the ConsoleTitle you do not see the change but it changed ;-)):

MSH>env:
MSH>hklm:


That is All but one, the function is there but it will not work :

Function        Variable:                                                     set-location Variable:


MSH>variable:
'variable:' is not recognized as a Cmdlet, function, operable program, or script file.


*caution*

You might think you can leave out the original Code now (as we create the available driveletters too, so existing drive are created double, and non-existing drive are not there so why bother adding them), but if you think about it.. , what will happen when you add your USB-disk, you get a new drive-letter but It will not be added to the functions till you start MSH again, so that's why the are all created infront.

Also Place the New line of code inside of the & {} block (new scope) to keep your environment Clean, But outside the For Loop (as we only want to do this once, not 26 times) to make this more clear I pasted the Resulting Script-block below.

gr /\/\o\/\/

& {
    for ($i = 0; $i -lt 26; $i++) 
    { 
        $funcname = ([System.Char]($i+65)) + ':'
        $str = "function global:$funcname { set-location $funcname } " 
        invoke-command $str 
    }
    get-drive | forEach {$str = "function global:$($_.name): { set-location $($_.name): } ";invoke-command $str}
}



Comments: Post a Comment



<< Home

Archives

October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006  

$Links = ("PowerShell RC1 Docs"," PowerShell RC1 X86"," PowerShell RC1 X64"," Monad GettingStarted guide"," Monad Progamming Guide"," Monad SDK"," Monad videos on Channel 9"," MSH Community Workspace"," scripts.readify.net "," MonadSource"," www.reskit.net"," PowerShell Blog"," Under The Stairs"," computerperformance powershell Home"," proudlyserving"," MSH on wikipedia"," MSHWiki Channel 9"," Keith Hill's Blog"," Precision Computing"," PowerShell for fun"," MSH Memo (Japanese)"," monadblog")

find-blog -about "PowerShell","Monad" | out-Technorati.
find-blog -contains "","" | out-Technorati.
Google
 
Web mow001.blogspot.com

This page is powered by Blogger. Isn't yours?