on 06-Jan-2009 07:23
Welcome to this addition of the PowerShell ABC's where you'll find 26 posts detailing a component of the PowerShell scripting language, one letter at a time. Today's letter is the letter "L". For "L" I'm going to give an overview on Location.
The current working location is the default location to which commands point if you don't supply an explicit path to the item or location that is affected by the command.
In most cases, the current working location is a directory on a drive supplied by the built-in FileSystem provider but it doesn't have to be.
Using the Get-PSProvider Cmdlet, you can see a list of all installed PowerShell Providers (PSProviders) on your system.
PS C:\>Get-PSProvider
Name Capabilities Drives
---- ------------ ------
WSMan Credentials {}
Alias ShouldProcess {Alias}
Environment ShouldProcess {Env}
FileSystem Filter, ShouldProcess {C, D, E, A...}
Function ShouldProcess {Function}
Registry ShouldProcess, Transactions {HKLM, HKCU}
Variable ShouldProcess {Variable}
Certificate ShouldProcess {cert}
iControlProvider Credentials {}
And you can then use the Get-PSDrive to get a listing of all the virtual drives that you can set locations on.
PS C:\> Get-PSDrive
Name Provider Root CurrentLocation
---- -------- ---- ---------------
A FileSystem A:\
Alias Alias
C FileSystem C:\
cert Certificate \
D FileSystem D:\
E FileSystem E:\
Env Environment
F FileSystem F:\
foo iControlPr... theboss LTM
Function Function
G FileSystem G:\
H FileSystem H:\
HKCU Registry HKEY_CURRENT_USER
HKLM Registry HKEY_LOCAL_MACHINE
I FileSystem I:\
K FileSystem K:\
L FileSystem L:\
V FileSystem V:\
Variable Variable
You'll notice that there are pre-configured PSDrives for all the physical and virtual drives on your system including network shares. There are also Alias, Certificate, Environment, Function, Registry, and Variable drives created with the built-in providers.
So, what's up with that "foo" drive in there?
PowerShell allows you to extend the PSProvider model by creating your own to do whatever you wish. By supporting the correct interfaces and building a Cmdlet, you can create a provider to do some fun things. I wrote a provider a while back when I was building my iControl PowerShell Cmdlet that allows you to treat your BIG-IP Network Configuration like a local storage system. Here's a little example of how you use the Location Cmdlet's to navigate through the Network Configuration with a properly configured iControlProvider:
PS C:\> Set-Location foo:\
PS foo:\> Get-ChildItem
GTM
LTM
Management
Networking
System
PS foo:\> Set-Location LTM
PS foo:\LTM> Get-ChildItem
Nodes
Pools
Rules
VirtualServers
PS foo:\LTM> Push-Location Pools
PS foo:\LTM\Pools> Get-ChildItem | select Name, Availability, Enabled
Name Availability Enabled
---- ------------ -------
xpbert-http AVAILABILITY_STATUS_GREEN ENABLED_STATUS_ENABLED
xpbert-ssh AVAILABILITY_STATUS_GREEN ENABLED_STATUS_ENABLED
pool_2 AVAILABILITY_STATUS_RED ENABLED_STATUS_ENABLED
pool_1 AVAILABILITY_STATUS_GREEN ENABLED_STATUS_ENABLED
catbert-ssh AVAILABILITY_STATUS_RED ENABLED_STATUS_ENABLED
catbert-http AVAILABILITY_STATUS_RED ENABLED_STATUS_ENABLED
PS foo:\LTM\Pools> Pop-Location
PS foo:\LTM>
The Location Cmdlets
The built-in Cmdlet's that you can use to navigate through provider stores are the following:
Get-Location - Gets information about the current working location
Set-Location - Sets the current working location to a specified location.
Push-Location - Adds the current location to the top of a list of locations ("stack").
Pop-Location - Changes the current location to the location most recently pushed onto the stack. You can pop the location from the default stack or from a stack that you create by using Push-Location.
In the good old days of "pushd" and "popd", you were limited to a single "stack", but no longer! Each of these Cmdlet's allow for the use of user specified stacks allowing you to build your own arsenal of previous storage location history.