gpt4 book ai didi

powershell - 我可以将 invoke-restmethod 的结果保存为变量而不是 powershell 中的 -outfile 吗?

转载 作者:行者123 更新时间:2023-12-05 06:43:59 24 4
gpt4 key购买 nike

我有一个 Invoke-Restmethod API 脚本,它创建一个新的邮件列表并返回以下内容

  Code Message                              Result
---- ------- ------
1 Mailing list successfully created 12345678

我需要下一个操作的结果(新邮件列表的 ID)。

我知道我可以使用 -outfile 将结果保存到 txt\xml\json 并使用 get-contents 将其导入,但让我印象深刻的是将结果保存在变量中而无需保存outfile 并将其重新导入。有没有办法做到这一点?

我的完整代码:

Invoke-RestMethod $url -Method $method -Headers $headers -Body (ConvertTo-json $body) -ContentType "$outform"

最佳答案

当然可以。将 -PassThru 开关添加到您的 Invoke-RestMethod 调用,并像往常一样将输出分配给变量。

$result = Invoke-RestMethod @args -PassThru # when there's "outfile" in args

事实上,如果您使用-outfile,您的结果应该已经可以保存为变量。

$result = Invoke-RestMethod @args # when there's no "outfile" in args

编辑:如果您只需要总结果中的一个字段(因此您不需要 codemessage 等),您可以只保存结果的 result 字段如下:

$result = (Invoke-RestMethod @args).result

关于powershell - 我可以将 invoke-restmethod 的结果保存为变量而不是 powershell 中的 -outfile 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31264257/

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