powershell - 使用参数对象调用 Powershell 函数

标签 powershell

是否可以像这样创建一个对象

    $Data = new-object PSObject
    $Data | Add-member NoteProperty -Name "SiteName" -Value "Web Title"
    $Data | Add-member NoteProperty -Name "SiteURL" -Value "https://www.test.url"

然后以某种方式调用类似

的函数
Do-CustomFunction $Data

这将解压对象并将其属性用作命名参数,以模拟以下行为:

Do-CustomFunction -SiteName "Web Title" -SiteURL "https://www.test.url"

最佳答案

您正在寻找about_splatting:link

例子:

function mytest{
    param($path1,$path2)
    write-host $path1 $path2
}

$commands = @{
    path1 = "C:\temp\test.txt"
    path2 = "C:\temp\test2.txt"
}

mytest @commands

注意在调用函数时使用 @ 引用哈希表。

关于powershell - 使用参数对象调用 Powershell 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59434762/

相关文章:

powershell - 比较两个 PsCustomObjects 数组

powershell - 如何在 PowerShell 中创建全局常量变量

json - 哈希表 ConvertTo-Json PowerShell 格式问题

c# - 在 VS2010 中执行 Powershell Invoke 时出错

powershell - 如何将解析路径与另一个目录连接

.net - 如果 View 发生更改,则使用 TeamCity 打开 MvcBuildViews

powershell - 单击按钮后如何最小化powershell表单?

windows - 反引号放置为何重要?

json - 在Powershell中合并JSON对象

powershell - FileSystemWatcher-任务期间的新文件