PowerShell 飞溅不起作用

标签 powershell

我相信我遗漏了一些明显的东西,或者误解了 PowerShell 的 splatting 功能。

我正在使用哈希表将参数传递给自定义函数,但即使是一个简单的示例,它似乎也没有采用参数。

文件:Test-Splat.ps1

function Test-Splat
{
    param(
        [Parameter(Mandatory=$true)][string]$Name,
        [Parameter(Mandatory=$true)][string]$Greeting
    )
    $s = "$Greeting, $Name"
    Write-Host $s
}

然后尝试使用 splatting 执行此操作,要求为第二个参数提供一个值。
. .\Test-Splat.ps1
$Params = @{
    Name = "Frank"
    Greeting = "Hello"
}
Test-Splat $Params

产生以下结果
cmdlet Test-Splat at command pipeline position 1
Supply values for the following parameters:
Greeting: 

如果我直接使用它而不产生飞溅,它会起作用
Greeting: [PS] C:\>Test-Splat -Name "Frank" -Greeting "Hello"
Hello, Frank

如果相关,我将在 PowerShell 3.0 下的 Exchange 命令行管理程序中执行此操作
[PS] C:\>$PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
3      0      -1     -1

最佳答案

您确实遗漏了一些东西,那就是当您想将变量作为函数或 cmdlet 的参数时,您可以使用 @符号而不是 $象征。在您的示例中,您放置变量的行将如下所示:

Test-Splat @Params

关于PowerShell 飞溅不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42492642/

相关文章:

powershell - 如何在使用导入的 PowerShell session 时向 Live@EDU (outlook.com) 进行身份验证?

powershell - 使用 Powershell 提取文件的前 50 个字节

unix - powershell中等效的unix test -f命令是什么?

windows - 使用 SC 安装 Windows 服务

Powershell,如何检查 AD 用户的组成员身份

azure - 使用 Powershell 将范围添加到 Azure AD 应用程序(公开 API)

O365 GUI 的 Powershell : permissions

PowerShell-向数组添加列

powershell - 将名称匹配的文件夹复制到匹配目录的其他子文件夹

c# - Powershell运行空间写入 protected 变量(不可替换)