If I wrote "idiomatic" PowerShell, what types of parameters would I use for functions that work with files or directories?
For example, I created a module containing this function:
$bookmarks = @{}
$bookmarkKeys = @{}
function Set-Bookmark {
param(
[Parameter(Mandatory = $true)]
[string] $Id,
[System.Management.Automation.DirectoryInfo] $Path = (Get-Location))
$script:bookmarks[$Id] = $Path
$script:bookmarkKeys[$Path.Path] = $Id
}
The problem is that the following does not work:
PS>Set-Bookmark -Id Code -Path C:\Code
Set-Bookmark : Cannot process argument transformation on parameter 'Path'.
Cannot convert the "C:\Code" value of type "System.String" to type
"System.Management.Automation.PathInfo".
At line:1 char:29
+ Set-Bookmark -Id Code -Path C:\Code
+ ~~~~~~~
+ CategoryInfo : InvalidData: (:) [Set-Bookmark], ParameterBindingArgumentTransformationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-Bookmark
Oddly enough, this is not the case (but for a slightly different reason):
PS>Set-Bookmark -Id Code -Path (gi C:\Code)
Set-Bookmark : Cannot process argument transformation on parameter 'Path'.
Cannot convert the "C:\Code" value of type "System.IO.DirectoryInfo" to type
"System.Management.Automation.PathInfo".
At line:1 char:29
+ Set-Bookmark -Id Code -Path (gi C:\Code)
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Set-Bookmark], ParameterBindingArgumentTransformationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-Bookmark
So, should I define my parameters as strings (i.e. the lowest common denominator) and then try to use / parse them in more meaningful types? It doesn’t sound like that, because it’s a shame if I work with the results of things like Get-Item, and they are transferred to stringfor transmission, only for this function, the type of level is again.