将脚本参数传递给Powershell中的函数

时间:2022-12-05 19:16:43

My Script calls a function that needs the parameters from the calling of the scripts:

我的脚本调用一个需要脚本调用参数的函数:

function new( $args )
{
  if( $args.length -lt 8 )
  {
    Write-Host "Parameter Missing, requires 8 Parameters. Aborting."!
    Write-Host $args.length
    break
  }
}

switch ($args[0]) {
  '--test' { }
  '--new' { new $args }
  default  { }
}

When I call it, the args array is not handed over to the new function:

当我调用它时,args数组并没有交给新函数:

PS Q:\mles\etl-i_test> .\iprog.ps1 --new 1 2 3 4 5 6 7
Parameter Missing, requires 8 Parameters. Aborting. !
0

How do I pass an array to a function in powershell? Or specifically, the $args array? Shouldn't the scope for the $args array be global?

如何将数组传递给powershell中的函数?或者说,$args数组?$args数组的范围不应该是全局的吗?

3 个解决方案

#1


2  

Modify your function as below:

修改你的功能如下:

function new { param([string[]] $paramargs)

  if( $paramargs.length -lt 8 )
  {
    Write-Host "Parameter Missing, requires 8 Parameters. Aborting."!
    Write-Host $paramargs.length
    break
  }
}

switch ($args[0]) {
  '--test' { }
  '--new' { new $args }
  default  { }
}

This will avoid the ambiguity of the variable $arg from command line and parameter $arg (changed as $paramarg).

这将避免来自命令行的变量$arg和参数$arg(更改为$paramarg)的模糊性。

#2


2  

The $args is always part of any function. When you call a function with $args as the name of an argument things get pretty confusing. In your function change the name to something other than $args and it should work.

$args始终是任何函数的一部分。当你调用一个带有$args的函数作为参数的名称时,事情会变得非常混乱。在您的函数中,将名称更改为$args以外的其他名称,它应该可以工作。

function new( $arguments )
{

$arguments.Length
  if( $arguments.length -lt 8 )
  {
    Write-Host "Parameter Missing, requires 8 Parameters. Aborting."!
    Write-Host $arguments.length
    break
  }
}

switch ($args[0]) {
  '--test' { }
  '--new' { new $args }
  default  { }
}

#3


1  

You must pass $args from a parent scope to a child scope explicitly. $args in the parent scope is never visible in a chlid scope, because the scope is initialized with it's own $args. Do this:

您必须显式地将$args从父范围传递到子范围。父作用域中的$args在chlid作用域中是不可见的,因为作用域是用它自己的$args初始化的。这样做:

&{get-variable -scope 0}

and note the results. Every variable you see there is created locally in the new scope when the scope is initialized, so those variables in the parent scope aren't visible in the new scope. $args is one of those variables.

并注意结果。在初始化作用域时,您看到的每个变量都在新作用域中本地创建,因此父作用域中的这些变量在新作用域中不可见。$args就是其中一个变量。

#1


2  

Modify your function as below:

修改你的功能如下:

function new { param([string[]] $paramargs)

  if( $paramargs.length -lt 8 )
  {
    Write-Host "Parameter Missing, requires 8 Parameters. Aborting."!
    Write-Host $paramargs.length
    break
  }
}

switch ($args[0]) {
  '--test' { }
  '--new' { new $args }
  default  { }
}

This will avoid the ambiguity of the variable $arg from command line and parameter $arg (changed as $paramarg).

这将避免来自命令行的变量$arg和参数$arg(更改为$paramarg)的模糊性。

#2


2  

The $args is always part of any function. When you call a function with $args as the name of an argument things get pretty confusing. In your function change the name to something other than $args and it should work.

$args始终是任何函数的一部分。当你调用一个带有$args的函数作为参数的名称时,事情会变得非常混乱。在您的函数中,将名称更改为$args以外的其他名称,它应该可以工作。

function new( $arguments )
{

$arguments.Length
  if( $arguments.length -lt 8 )
  {
    Write-Host "Parameter Missing, requires 8 Parameters. Aborting."!
    Write-Host $arguments.length
    break
  }
}

switch ($args[0]) {
  '--test' { }
  '--new' { new $args }
  default  { }
}

#3


1  

You must pass $args from a parent scope to a child scope explicitly. $args in the parent scope is never visible in a chlid scope, because the scope is initialized with it's own $args. Do this:

您必须显式地将$args从父范围传递到子范围。父作用域中的$args在chlid作用域中是不可见的,因为作用域是用它自己的$args初始化的。这样做:

&{get-variable -scope 0}

and note the results. Every variable you see there is created locally in the new scope when the scope is initialized, so those variables in the parent scope aren't visible in the new scope. $args is one of those variables.

并注意结果。在初始化作用域时,您看到的每个变量都在新作用域中本地创建,因此父作用域中的这些变量在新作用域中不可见。$args就是其中一个变量。