标签: dynamicparameters

动态参数值取决于另一个动态参数值

启动前提:限制性很强的环境,Windows 7 SP1,Powershell 3.0。使用外部库的可能性有限或不可能。

我正在尝试重写我之前创建的 bash 工具,这次使用 PowerShell。在 bash 中,我实现了自动完成以使该工具更加用户友好,我想为 PowerShell 版本做同样的事情。

bash 版本是这样工作的:

./launcher <Tab> => ./launcher test (or dev, prod, etc.)
./launcher test <Tab> => ./launcher test app1 (or app2, app3, etc.)
./launcher test app1 <Tab> => ./launcher test app1 command1 (or command2, command3, etc.).
Run Code Online (Sandbox Code Playgroud)

如您所见,一切都是动态的。环境列表是动态的,应用程序列表是动态的,根据选择的环境,命令列表也是动态的。

问题出在测试 → 应用程序连接上。我想根据用户已经选择的环境显示正确的应用程序。

使用 PowerShell 的 DynamicParam,我可以获得基于文件夹列表的动态环境列表。然而,我不能(或者至少我还没有找到如何)做另一个文件夹列表,但这次使用基于现有用户选择的变量。

当前代码:

function ParameterCompletion {
    $RuntimeParameterDictionary = New-Object Management.Automation.RuntimeDefinedParameterDictionary

    # Block 1.
    $AttributeCollection = New-Object Collections.ObjectModel.Collection[System.Attribute]

    $ParameterName = "Environment1"
    $ParameterAttribute = New-Object Management.Automation.ParameterAttribute
    $ParameterAttribute.Mandatory = $true …
Run Code Online (Sandbox Code Playgroud)

bash powershell autocomplete dynamicparameters

6
推荐指数
1
解决办法
1161
查看次数

对于 varbinary 数据类型,DynamicParameters (Dapper) 的正确用法是什么?

除其他外,存储过程返回 varbinary(max) 作为输出。我不明白如何使用 Dapper 访问此信息。

下面是一些说明问题的简化示例代码。我向 StoredProcedure 提供一些参数,并希望返回一张在 SQL Server 上存储为 varbinary(max) 的照片。

varbinary 没有 DbType。我尝试使用 DbType.Binary 但这会导致 Dapper 中出现异常。如果我去掉照片参数,我期望返回的所有其他参数(为了简洁起见,从示例中删除了这些参数)都将起作用。所以唯一的问题是检索 varbinary 数据。

实现这一目标的正确方法是什么?

using (var connection = new System.Data.SqlClient.SqlConnection(HelperClassesBJH.HelperMethods.ConString("ProductionLocal")))
        {
            connection.Open();

            DynamicParameters p = new DynamicParameters();

            p.Add("@OpID", id, DbType.Int32, ParameterDirection.Input);
            p.Add("@StageID", Properties.Settings.Default.StageID, DbType.Int32, ParameterDirection.Input);               
            p.Add("@Photo", dbType: DbType.Binary, direction: ParameterDirection.Output);                

            try
            {
                connection.Execute(sql, p, commandType: CommandType.StoredProcedure);

                op.Photo = p.Get<byte[]>("@Photo");                    
            }
            catch {}

        }
Run Code Online (Sandbox Code Playgroud)

更新:

我发现我必须在 DynamicParameters 构造函数中提供“value”参数。这避免了我遇到的异常。我无法理解为什么我需要提供一个值,因为该参数是一个输出,并且我提供的值没有被使用。这是修改后的代码:

DynamicParameters p = new DynamicParameters();
MemoryStream b = new MemoryStream();

p.Add("@OpID", id, DbType.Int32, ParameterDirection.Input); …
Run Code Online (Sandbox Code Playgroud)

dapper dynamicparameters

5
推荐指数
1
解决办法
2929
查看次数

PowerShell - 我可以使参数集依赖于另一个参数的值吗?

假设我有一个类似的函数:

function Authenticate
{
    param
    (
        [ValidateSet('WindowsAuthentication','UsernameAndPassword')]
        [string]$AuthenticationType,

        [Parameter(ParameterSetName='ParamSet1')]
        [string]$Username,

        [Parameter(ParameterSetName='ParamSet1')]
        [string]$Password
    )
  ..
}
Run Code Online (Sandbox Code Playgroud)

我想执行这些规则:

  • ParamSet1如果出现以下情况,则强制设置参数$AuthenticationType = 'UsernameAndPassword'
  • 完全“禁用”ParamSet1如果$AuthenticationType = 'WindowsAuthentication'

这可能吗?

powershell parameter-sets dynamicparameters

2
推荐指数
1
解决办法
6430
查看次数

如何在 Powershell 中指定静态和动态位置参数?

编辑

根据疯狂技术人员的建议,我已在 PowerShell UserVoice 网站上提交了一份错误报告:https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/20034763-dynamic-parameters-and-positional-parameters -不-不

原问题

我希望能够在 PowerShell 函数中指定包括静态和动态参数的位置参数。例如我有

function Test-Positional{
[CmdletBinding(PositionalBinding=$false)]
param(
    [Parameter(Mandatory=$false,Position=3)][string]$Param4
)
dynamicparam {
    $paramDictionary = new-object -Type System.Management.Automation.RuntimeDefinedParameterDictionary

    $paramname1 = "Param1"
    $values1 = 'some','list','of','values' #would normally get these dynamically
    $attributes1 = new-object System.Management.Automation.ParameterAttribute
    $attributes1.ParameterSetName = "__AllParameterSets"
    $attributes1.Mandatory = $true
    $attributes1.Position = 0
    $attributeCollection1 = new-object -Type System.Collections.ObjectModel.Collection[System.Attribute]
    $attributeCollection1.Add($attributes1)
    $ValidateSet1 = new-object System.Management.Automation.ValidateSetAttribute($values1)
    $attributeCollection1.Add($ValidateSet1)
    $dynParam1 = new-object -Type System.Management.Automation.RuntimeDefinedParameter($paramname1, [string], $attributeCollection1)

    $paramname2 = "Param2"
    $values2 = 'another','list','like','before'
    $attributes2 = new-object System.Management.Automation.ParameterAttribute
    $attributes2.ParameterSetName = "__AllParameterSets"
    $attributes2.Mandatory …
Run Code Online (Sandbox Code Playgroud)

parameters powershell powershell-4.0 positional-parameter dynamicparameters

1
推荐指数
1
解决办法
1153
查看次数