gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:13:01 25 4
gpt4 key购买 nike

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

    $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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com