简体   繁体   English

Powershell参数集和管线输入

[英]Powershell parametersets and pipline input

Short Background: 简短背景:

The original function creates printers. 原始功能创建打印机。 Some of them have a build in printserver and can do stuff like scanning and / or print on special paper. 他们中的一些人在printserver中具有内置功能,可以执行诸如扫描和/或在特殊纸张上打印之类的工作。 Others are connected to a box and print via lpd / lpr. 其他的则连接到盒子并通过lpd / lpr打印。 This printers can print only. 该打印机只能打印。

Parameter description: 参数说明:

PrinterName and InvetoryID are common to both LprAddress and LprQueue indicates a printer that cannot use Scan2Folder, CreateBfbPrinters and PsAddress. LprAddress和LprQueue都使用PrinterName和InvetoryID,这表示不能使用Scan2Folder,CreateBfbPrinters和PsAddress的打印机。

I'm using parametersets and because there is no mandatory parameter that is exclusive for the "Printserver"-set I made this the default parameterset. 我正在使用参数集,并且因为没有“打印服务器”集专用的强制性参数,所以我将此设置为默认参数集。

Error: 错误:

Everything works as expected but input from pipline (by property name) seems to end in the parameterset "LPR" no matter what properties are provided. 一切都按预期工作,但是无论提供了什么属性,来自管线的输入(按属性名称)似乎都以参数集“ 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

Would have liked to write this as a comment, but I can't create comments this long and formatted nicely. 本来希望将其写为评论,但是我不能创建那么长且格式没有问题的评论。 However, I must say that I do not get the same behavior as you mention. 但是,我必须说,我没有得到与您提到的相同的行为。

When I run it in PowerShell version 4 (on a Win8.1 machine) it works completely as expected and all tests (except number 3, which is expected to fail) run nicely. 当我在PowerShell版本4(在Win8.1机器上)上运行它时,它可以完全按预期运行,并且所有测试(第3个除外,该数字均预期会失败)运行良好。

When I run it in PowerShell version 2 (on a Win8.1 machine with PS4 installed) I do get errors on test 4 and 5 as well, but I get other errors than you mention. 当我在PowerShell版本2中运行它(在安装了PS4的Win8.1机器上)时,在测试4和5上也确实出现了错误,但是我收到了除您提到的其他错误。 The error I receive on test 4 and 5 is: 我在测试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

My FullyQualifiedErrorId is InputObjectMissingMandatory while you mention getting AmbiguousParameterSet. 当您提到获取AmbiguousParameterSet时,我的FullyQualifiedErrorId是InputObjectMissingMandatory。 And, again, on PowerShell version 4 it works perfectly fine on my machine. 同样,在PowerShell版本4上,它在我的机器上也可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM