C# Powershell Commandlet 使用两个参数,但最多可以指定一个
C# Powershell Commandlet to work with two parameters, but at most one can be specified
我正在用 C# 实现 Powershell commandlet。 commandlet 可以传递两个参数。两者都标记为可选(因为我事先不知道用户会发出 Get-MsiInstalled -Product "0449F3E3-2337-45EF-BE77-60F4B9CD8822"
还是 Get-MsiInstalled -MsiFile "C:filePath\file.msi"
.
此外,如果用户未提供任何内容并发出 Get-MsiInstalled 将抛出异常(必须指定 MSI 或 Product 参数)。
目前,实现最多不能期望一个参数。相反,用户可以提供两个参数,这是无效调用。
public class GetAHPIsMsiInstalled : Cmdlet
{
[Parameter(Mandatory = false, ValueFromPipeline = true,
HelpMessage = "Product Guid to search Installable Package.", Position = 1)]
[UsedImplicitly]
public string Product { get; set; }
[Parameter(Mandatory = false, ValueFromPipeline = true, HelpMessage = "Installable Package to search.",
Position = 1)]
[UsedImplicitly]
public string MsiFile { get; set; }
protected override void BeginProcessing()
{
if (string.IsNullOrEmpty(Product) && string.IsNullOrEmpty(MsiFile))
ThrowTerminatingError(new ErrorRecord(new
ParameterBindingException("At most one parameter must be provided"))
base.BeginProcessing();
................................
}
}
参数集是指定互斥参数集的方法。
例如,Get-Process
可以用 -Name
或 -Id
调用,但不能同时调用:
Get-Process [[-Name] <String[]>] [-ComputerName <String[]>] [-FileVersionInfo] [-Module] [<CommonParameters>]
Get-Process [-ComputerName <String[]>] [-FileVersionInfo] -Id <Int32[]> [-Module] [<CommonParameters>]
Get-Process [-ComputerName <String[]>] [-FileVersionInfo] -InputObject <Process[]> [-Module] [<CommonParameters>]
Get-Process -Id <Int32[]> -IncludeUserName [<CommonParameters>]
Get-Process [[-Name] <String[]>] -IncludeUserName [<CommonParameters>]
Get-Process -IncludeUserName -InputObject <Process[]> [<CommonParameters>]
您可以在 C# 或
直接在 PowerShell
我正在用 C# 实现 Powershell commandlet。 commandlet 可以传递两个参数。两者都标记为可选(因为我事先不知道用户会发出 Get-MsiInstalled -Product "0449F3E3-2337-45EF-BE77-60F4B9CD8822"
还是 Get-MsiInstalled -MsiFile "C:filePath\file.msi"
.
此外,如果用户未提供任何内容并发出 Get-MsiInstalled 将抛出异常(必须指定 MSI 或 Product 参数)。
目前,实现最多不能期望一个参数。相反,用户可以提供两个参数,这是无效调用。
public class GetAHPIsMsiInstalled : Cmdlet
{
[Parameter(Mandatory = false, ValueFromPipeline = true,
HelpMessage = "Product Guid to search Installable Package.", Position = 1)]
[UsedImplicitly]
public string Product { get; set; }
[Parameter(Mandatory = false, ValueFromPipeline = true, HelpMessage = "Installable Package to search.",
Position = 1)]
[UsedImplicitly]
public string MsiFile { get; set; }
protected override void BeginProcessing()
{
if (string.IsNullOrEmpty(Product) && string.IsNullOrEmpty(MsiFile))
ThrowTerminatingError(new ErrorRecord(new
ParameterBindingException("At most one parameter must be provided"))
base.BeginProcessing();
................................
}
}
参数集是指定互斥参数集的方法。
例如,Get-Process
可以用 -Name
或 -Id
调用,但不能同时调用:
Get-Process [[-Name] <String[]>] [-ComputerName <String[]>] [-FileVersionInfo] [-Module] [<CommonParameters>]
Get-Process [-ComputerName <String[]>] [-FileVersionInfo] -Id <Int32[]> [-Module] [<CommonParameters>]
Get-Process [-ComputerName <String[]>] [-FileVersionInfo] -InputObject <Process[]> [-Module] [<CommonParameters>]
Get-Process -Id <Int32[]> -IncludeUserName [<CommonParameters>]
Get-Process [[-Name] <String[]>] -IncludeUserName [<CommonParameters>]
Get-Process -IncludeUserName -InputObject <Process[]> [<CommonParameters>]
您可以在 C# 或 直接在 PowerShell