【问题标题】:passing values to a function in a powershell script将值传递给powershell脚本中的函数
【发布时间】:2020-06-05 07:44:22
【问题描述】:

我很好奇是否可以使用对象表示法将参数传递给 powershell 模块。看下面的函数定义:

function Get-Something
{
    Param
    (
         [Parameter(Mandatory=$true, Position=0)]
         [string] $Name,
         [Parameter(Mandatory=$true, Position=1)]
         [int] $Id
    )
}

我可以用以下符号调用这个函数吗:

Get-Something(Name "Vance Refrigeration", Id "9")

而不是做:

Get-Something -Name "Vance Refrigeration" -Id 9

【问题讨论】:

    标签: powershell powershell-module


    【解决方案1】:

    据我所知,除了使用'-Name "Vance Refrigeration" -Id 9' 键入每个变量之外,唯一的其他方法是执行以下操作:

    function Get-Something
    {
        Param
        (
             [Parameter(Mandatory=$true, Position=0)]
             [string] $Name,
             [Parameter(Mandatory=$true, Position=1)]
             [int] $Id
        )
        Write-Host $name en $id
    }
    $object = @{Name = "Vance Refrigeration"; Id = "9"}
    Get-Something @object
    

    或者使用更漂亮的对象样式:

    $object = @{
        Name = "Vance Refrigeration"
        Id = "9"
    }
    Get-Something @object
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-27
      • 1970-01-01
      • 1970-01-01
      • 2011-08-01
      • 2015-06-07
      • 2017-05-08
      相关资源
      最近更新 更多