Powershell参数集和管道输入

时间:2014-01-21 08:44:59

标签: powershell

简短背景:

原始功能会创建打印机。其中一些在打印服务器中有内置功能,可以在特殊纸张上进行扫描和/或打印等操作。其他人连接到一个盒子并通过lpd / lpr打印。此打印机只能打印。

参数说明:

PrinterName和InvetoryID对两者都是通用的 LprAddress和LprQueue表示无法使用Scan2Folder,CreateBfbPrinters和PsAddress的打印机。

我正在使用参数集,因为没有强制参数专用于“Printserver” - 我将其设为默认参数集。

错误:

一切都按预期工作,但是无论提供什么属性,pipline(按属性名称)的输入似乎都在参数集“LPR”中结束。

function Test-ParamsetPipeline {
    [CmdletBinding(DefaultParameterSetName="Printserver")]
    Param(
        [parameter(Mandatory=$true,Position=1,ValueFromPipelinebyPropertyName=$True)]
        [String]$PrinterName,
        [parameter(Mandatory=$true,Position=2,ValueFromPipelinebyPropertyName=$True)]
        [String]$InventoryID,
        [parameter(Mandatory=$true,ValueFromPipelinebyPropertyName=$True,ParameterSetName="LPR")]
        [String]$LprAddress,
        [parameter(Mandatory=$true,ValueFromPipelinebyPropertyName=$True,ParameterSetName="LPR")]
        [string]$LprQueue,
        [parameter(ValueFromPipelinebyPropertyName=$True)]
        [Switch]$CreateBfbPrinters,
        [parameter(ParameterSetName="Printserver")]
        [Switch]$Scan2Folder,
        [parameter(ParameterSetName="Printserver")]
        [System.Security.SecureString]$PasswordDeviceaccount,
        [parameter(ValueFromPipelinebyPropertyName=$True,ParameterSetName="Printserver")]
        [String]$PsAddress
    )

    PROCESS {
        Write-Output $psCmdlet.ParameterSetName
    }
}

#Test1
Test-ParamsetPipeline -PrinterName "Printer1" -InventoryID "12345"
#No error - works as expected

#Test2
Test-ParamsetPipeline -PrinterName "Printer1" -InventoryID "12345" -LprAddress "1.1.1.1" -LprQueue "lp1"
#No error - works as expected

#Test3
Test-ParamsetPipeline -PrinterName "Printer1" -InventoryID "12345" -LprAddress "1.1.1.1" -LprQueue "lp1" -Scan2Folder
#Error - works as expected (Scan2Folder is not allowed on LPR-Printers)

#Test4
$Obj = New-Object PSObject -Property @{
    PrinterName = "Printer1"
    InventoryID = "12345"
}
$Obj | Test-ParamsetPipeline
#Error:
# Test-ParamsetPipeline : Der Parametersatz kann mit den angegebenen benannten Parametern nicht aufgelöst werden.
# Bei \\elbekliniken.de\system\EkScripts\Dev\Test.ps1:36 Zeichen:22
#+ Test-ParamsetPipeline <<<<  -PrinterName "Printer1" -InventoryID "12345" -LprAddress "1.1.1.1" -LprQueue "lp1" -Scan2Folder
#    + CategoryInfo          : InvalidArgument: (:) [Test-ParamsetPipeline], ParameterBindingException
#    + FullyQualifiedErrorId : AmbiguousParameterSet,Test-ParamsetPipeline
#
# Translation: Cannot resolve Parameterset with the given parameters.

#Test5
$Obj = New-Object PSObject -Property @{
    PrinterName = "Printer1"
    InventoryID = "12345"
    PsAddress = "2.2.2.2"
}
$Obj | Test-ParamsetPipeline
# Same error as above

#Test6
$Obj = New-Object PSObject -Property @{
    PrinterName = "Printer1"
    InventoryID = "12345"
    LprAddress = "1.1.1.1"
    LprQueue = "lp1"
}
$Obj | Test-ParamsetPipeline
#No error - works as expected

1 个答案:

答案 0 :(得分:0)

本来希望把它写成评论,但我不能创建这么长的评论并且格式很好。但是,我必须说我没有像你提到的那样行为。

当我在PowerShell版本4(在Win8.1机器上)运行它时,它完全按预期工作,并且所有测试(除了数字3,预计会失败)运行良好。

当我在PowerShell版本2中运行它时(在安装了PS4的Win8.1机器上)我在测试4和5上也出现错误,但是我得到的错误比你提到的还要多。我在测试4和5中收到的错误是:

Test-ParamsetPipeline : The input object cannot be bound because it did not contain the information required to bind all mandatory parameters:  LprAddress LprQueue
At <mypath>\paramsettest.ps1:60 char:29
+ $Obj | Test-ParamsetPipeline <<<<
    + CategoryInfo          : InvalidArgument: (@{PsAddress=2.2...rName=Printer1}:PSObject) [Test-ParamsetPipeline], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectMissingMandatory,Test-ParamsetPipeline

当您提到获取AmbiguousParameterSet时,我的FullyQualifiedErrorId是InputObjectMissingMandatory。而且,在PowerShell版本4上,它在我的机器上运行得非常好。