Tuesday, June 21, 2016

PowerShell v3 in a Year Day 15 Resolve Path

Topic: Resolve-Path

PowerShell has two powerful name resolution engines when it comes to working with providers. In the spirit of traditional Windows file and folder naming wildcards have a long history that dates pre-PowerShell (get-help about_Wildcards). Newer PowerShell dynamics, namely the .NET engine, allow for PowerShell to take advantage of regular expressions as well (get-help about_Regular_Expressions). When working with Resolve-Path, PowerShell focuses more on the traditional wildcard resolution than the newer regular expression system provided via .NET. In short, Resolve-Path, "[r]esolves the wildcard characters in a path, and displays the path contents."

In practical terms, using Resolve-Path allows for filtering traditional string patterns so that PowerShell can then locate hits in the provider by interpreting wildcard characters, and, displaying items (and containers) at the location specified by the path. This can be any of the standard provider objects. Typically, the most basic file system objects, files and directories, come to mind, but, thanks to PowerShells provider system, registry keys and subkeys are equally valid results for a call to Resolve-Path.

Below are the main parameters to focus on as well as key details for those parameters:
  • Credential (takes a PSCredential object): 
    • Specifies a user account that has permission to perform this action. The default is the current user.
    • Type a user name, such as "User01" or "Domain01User01". Or, enter a PSCredential object, such as one generated by the Get-Credential cmdlet. If you type a user name, you will be prompted for a password.
    • This parameter is not supported by any providers installed with Windows PowerShell.
  • LiteralPath (takes an array of strings): Specifies the path to be resolved. The value of LiteralPath is used exactly as it is typed. No characters are interpreted as wildcards. If the path includes escape characters, enclose it in single quotation marks. Single quotation marks tell Windows PowerShell not to interpret any characters as escape sequences.
  • Path (takes an array of strings): A required parameter indicating the path to resolve. Also accepts pipelined value.
  • Relative (Boolean): Returns a relative path
  • UseTransaction (Boolean): Includes the command in the active transaction and is only value when a transaction is in process.
To try and make heads or tails of these items I have explored them to get a few details about the parameters. With regards to cmdlets two parameter sets, the major is one allows for a LiteralPath parameters whereas the other permits for a Path parameter. All other parameters are identical.

Credential

When using the Credential parameter PowerShell converts the string into a PSCredential object to perform this single cmdlets action. As noted in the help, the default is the current user, but, this can be useful when you need to grab a file on a machine over which one is not the owner. If you do not use the default user (that is you type a user name) you will be prompted for the password.

LiteralPath versus Path

Literal paths indicate the exact path to a file, folder, registry key or the like. No wildcard interpretation is performed on the path to resolve names to items. If you want to force wildcard searching, you must use the -Path parameter along with the wildcard arguments as outlined in the about_Wildcards help topic. 

Relative

When working with -Relative, the inclusion of this parameter indicates to PowerShell that it wants a relative path returned from the operation. So, if you do this,
Resolve-Path c: est.txt
to get an absolute path,
Path                                                                                                                                                                             
----                                                                                                                                                                              
C: est.txt   
it will return a PathInfo object:
Resolve-Path -Path C: est.txt |Get-Member 



   TypeName: System.Management.Automation.PathInfo
However, if you use -Relative, 
Resolve-Path -Path C: est.txt -Relative
it will return a string with the relative path to the file object
as shown below:
.... est.txt 
UseTransaction

Transactions are an aspect of PowerShell that is still in development. As noted in the Get-Help About_Transactions, you can get a list of the specific PSProviders that allow you to take advantage of this technology by running this command:
get-psprovider | where {$_.Capabilities -like"*transactions*"
At the moment, when I run this on a v3 capable system I get the following provider list:
Name                 Capabilities                                      Drives
----                 ------------                                      ------
Registry             ShouldProcess, Transactions                       {HKLM, HKCU}
As such, Resolve-Path will allow one to locate full or relative paths with these cmdlets, however, there is only one provider, the Registry, which allows for Resolve-Path to have effect. This being the case, I would suggest looking at Get-Help about_Transactions to get extensive details as there are a variety of examples to draw from in terms of how v3 permits one to solve pathing issues.

To get an in depth exploration of how to work with wildcards, check out the basic help chart from the about_Wildcards to see how -Path and Resolve-Path can work together to spell out exactly what you need to find:
Wildcard Description        Example  Match             Nomatch
-------- -------------------------- -------------------------
*        Matches zeroor    a*       A, ag, Apple      banana
         more characters

?        Matches exactly    ?n       an, in, on        ran
         one character in
         the specified
         position

[ ]      Matchesa range    [a-l]ookbook,cook,look  took
         of characters

[ ]      Matchesspecified  [bc]ook  book, cook        hook
         characters 

Related Post:

0 comments:

Post a Comment

 
Copyright 2009 Information Blog
Powered By Blogger