从 Azure PowerShell 任务调用 PowerShell 函数时未获得预期输出
Not getting expected output when calling PowerShell function from Azure PowerShell Task
鉴于:
- PowerShell 5.1
- Azure 开发运营服务器 2019
我正在尝试直接从我的 Azure PowerShell 任务参数调用我的函数,这可能吗?我没有得到任何预期的输出。
param([String] $Name, [Int] $Age, [String] $Path)
Function Foo
{
Param(
[String]
$Name
,
[Int]
$Age
,
[string]
$Path
)
Process
{
write-host "Hello World"
If ("Tom","Dick","Jane" -NotContains $Name)
{
Throw "$($Name) is not a valid name! Please use Tom, Dick, Jane"
}
If ($age -lt 21 -OR $age -gt 65)
{
Throw "$($age) is not a between 21-65"
}
IF (-NOT (Test-Path $Path -PathType ‘Container’))
{
Throw "$($Path) is not a valid folder"
}
# All parameters are valid so New-stuff"
write-host "New-Foo"
}
}
更新 3
更新 2
更新 1
如果您直接执行脚本,它只会定义 Foo
函数,但绝不会调用 它。
将以下 放在脚本中 函数定义之后,以便使用脚本本身收到的参数调用它,使用 automatic $args
variable via splatting,这允许您通过包含参数值数组或哈希表的变量传递参数,需要用 @
而不是 $
:
引用
Foo @args
另一种方法是不调用 脚本文件,而是调用 一段 PowerShell 代码(在 PowerShell CLI 术语中,这意味着使用
-Command
参数而不是 -File
),这将允许您使用 .
,dot-sourcing operator 首先将函数定义加载到调用者的范围,然后允许它被调用:
. "$(System.DefaultWorkingDirectory)/_RodneyConsole1Repo/FunctionExample.ps1"
Foo -Name Rodney -Age 21 -Path ""
鉴于:
- PowerShell 5.1
- Azure 开发运营服务器 2019
我正在尝试直接从我的 Azure PowerShell 任务参数调用我的函数,这可能吗?我没有得到任何预期的输出。
param([String] $Name, [Int] $Age, [String] $Path)
Function Foo
{
Param(
[String]
$Name
,
[Int]
$Age
,
[string]
$Path
)
Process
{
write-host "Hello World"
If ("Tom","Dick","Jane" -NotContains $Name)
{
Throw "$($Name) is not a valid name! Please use Tom, Dick, Jane"
}
If ($age -lt 21 -OR $age -gt 65)
{
Throw "$($age) is not a between 21-65"
}
IF (-NOT (Test-Path $Path -PathType ‘Container’))
{
Throw "$($Path) is not a valid folder"
}
# All parameters are valid so New-stuff"
write-host "New-Foo"
}
}
更新 3
更新 2
更新 1
如果您直接执行脚本,它只会定义 Foo
函数,但绝不会调用 它。
将以下 放在脚本中 函数定义之后,以便使用脚本本身收到的参数调用它,使用 automatic $args
variable via splatting,这允许您通过包含参数值数组或哈希表的变量传递参数,需要用 @
而不是 $
:
Foo @args
另一种方法是不调用 脚本文件,而是调用 一段 PowerShell 代码(在 PowerShell CLI 术语中,这意味着使用 -Command
参数而不是 -File
),这将允许您使用 .
,dot-sourcing operator 首先将函数定义加载到调用者的范围,然后允许它被调用:
. "$(System.DefaultWorkingDirectory)/_RodneyConsole1Repo/FunctionExample.ps1"
Foo -Name Rodney -Age 21 -Path ""