なかなかどうして、functionなどに与えるparamは奥深く試行錯誤が続きます。 が、1つのベースとする形が見えてきたので備忘録に。
ベース候補
基本的には、こういった感じを考えています。
[CmdletBinding()]時のDefaultParameterSetNameに始まり、paramでの各種[parameter()]宣言など、多くの要素を以下に取捨選択するかなわけです…。
動作自体は何の意味もないサンプルですが…w
function Get-BasicCmdlet{ [CmdletBinding( SupportsShouldProcess = $false, ConfirmImpact = "none", DefaultParameterSetName = "" )] param ( [Parameter( HelpMessage = "Input path of ....", Position = 0, Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true )] [ValidateNotNullOrEmpty()] [ValidateScript({Test-Path $_.FullName})] [IO.FileInfo[]] $path, [Alias("PassThru")] [switch] $PassThrough ) begin { try { # Add-Typeでdllをロードする # Add-Type -Path D:\Document\Program\Powershell\Sound\SmallBasic1.0\SmallBasicLibrary.dll # Add-Typeでアセンブリをロードする # Add-Type -AssemblyName presentationframework } catch { #Alread Type added. } } process { if ($PassThrough) { Get-ChildItem $path | Add-Member -MemberType noteproperty -Name GetChildItem -Value $path -Force -PassThru } } end { return [PSCustomObject]@{ Path="$path"; File=(Split-Path $path -Leaf); Parent=(Split-Path $path -Parent) Qualifier=(Split-Path $path -Qualifier ); } } } $pathResolved = Resolve-Path "D:\Document\program\Powershell\usp3" Get-BasicCmdlet -path $pathResolved.Path -PassThrough Get-BasicCmdlet -path $pathResolved.Path
PassThuスイッチ付きだと、こういった出力です。
# PassThru 有効時のみの出力 Mode LastWriteTime Length Name ---- ------------- ------ ---- d---- 2013/02/17 23:57 1 d---- 2013/02/18 13:07 10 d---- 2013/02/18 0:37 2 d---- 2013/02/18 0:37 3 d---- 2013/02/18 0:45 4 d---- 2013/02/18 1:42 5 d---- 2013/02/18 1:48 6 d---- 2013/02/20 3:44 7 d---- 2013/02/18 11:11 8 d---- 2013/02/18 0:09 9 d---- 2013/02/18 13:49 延長戦 -a--- 2013/02/18 16:06 431 By_PowerShell - コピー.ps1 -a--- 2013/02/18 15:38 6399 By_PowerShell.ps1 # PassTru後にセットしてあるreturn出力 Path : D:\Document\program\Powershell\usp3 File : usp3 Parent : D:\Document\program\Powershell Qualifier : D:
PassThuスイッチ付きなしだと、こういった出力です。
Path : D:\Document\program\Powershell\usp3 File : usp3 Parent : D:\Document\program\Powershell Qualifier : D:
まだまだ試行錯誤は続きます。