【问题标题】:How to use keywords parameters in powershell scripts?如何在powershell脚本中使用关键字参数?
【发布时间】:2020-03-16 16:11:15
【问题描述】:

我有一个简单的脚本:

function getops {
[CmdletBinding()]
  param
  (
    [ValidateNotNullOrEmpty()]
    [string]$server,
    [string]$name,
    [string]$user,
    [string]$password,
    [string]$url,
    [string]$acl

  )
    echo $server
    echo $name
    echo $user
    echo $password
    echo $url
    echo $acl
}

getops

但是当我试图用参数调用这个脚本时。

.\Untitled2.ps1 -server my\sqlexpress -name my -user my_user -password my_password -url 192.168.0.1 -acl 192.168.0.1:5000

我看到空结果。

当我在脚本中添加函数时需要参数

function getops {
[CmdletBinding()]
  param
  (
    [ValidateNotNullOrEmpty()]
    [string]$server,
    [string]$name,
    [string]$user,
    [string]$password,
    [string]$url,
    [string]$acl

  )
    echo $server
    echo $name
    echo $user
    echo $password
    echo $url
    echo $acl
}

getops -server my\sqlexpress -name my -user my_user -password my_password -url 192.168.0.1 -acl 192.168.0.1:5000

我看到了我需要的结果:

my\sqlexpress
my
my_user
my_password
192.168.0.1
192.168.0.1:5000

问题,如何在powershell中接收相同的结果,通过调用带有关键字参数的脚本,如下所示:

.\Untitled2.ps1 -server my\sqlexpress -name my -user my_user -password my_password -url 192.168.0.1 -acl 192.168.0.1:5000

主要任务是将这些参数接收到变量中,并将这些变量放入不同的函数中。

【问题讨论】:

    标签: powershell arguments parameter-passing command-line-arguments keyword


    【解决方案1】:

    在函数外部定义另一个参数块,以便可以将参数传递给您的脚本。在您的情况下,您只需调用不带参数的函数。

    # Untitled2.ps1
    
    [CmdletBinding()]
    param
    (
        [ValidateNotNullOrEmpty()]
        [string]$server,
        [string]$name,
        [string]$user,
        [string]$password,
        [string]$url,
        [string]$acl
    )
    
    function getops {
      param
      (
        [ValidateNotNullOrEmpty()]
        [string]$server,
        [string]$name,
        [string]$user,
        [string]$password,
        [string]$url,
        [string]$acl
      )
      echo $server
      echo $name
      echo $user
      echo $password
      echo $url
      echo $acl
    }
    
    getops -server $server -name $name -user $user -password $password -url $url -acl $acl
    

    然后你可以通过

    调用你的脚本
    .\Untitled2.ps1 -server my\sqlexpress -name my -user my_user -password my_password -url 192.168.0.1 -acl 192.168.0.1:5000
    

    【讨论】:

      【解决方案2】:

      您可以像在函数中一样在脚本中使用参数
      使用参数块编写脚本

      param
      (
        [ValidateNotNullOrEmpty()]
        [string]$server,
        [string]$name,
        [string]$user,
        [string]$password,
        [string]$url,
        [string]$acl
      
      )
      echo $server
      echo $name
      echo $user
      echo $password
      echo $url
      echo $acl
      

      然后运行脚本

      .\Untitled2.ps1 -server my\sqlexpress -name my -user my_user -password my_password -url 192.168.0.1 -acl 192.168.0.1:5000
      

      【讨论】:

        猜你喜欢
        • 2022-11-02
        • 2013-03-23
        • 2019-11-28
        • 1970-01-01
        • 2021-10-11
        • 2022-01-02
        • 2020-12-04
        • 1970-01-01
        • 2015-04-04
        相关资源
        最近更新 更多