Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 从PS脚本获取输出并传递给curl命令_Powershell_Curl - Fatal编程技术网

Powershell 从PS脚本获取输出并传递给curl命令

Powershell 从PS脚本获取输出并传递给curl命令,powershell,curl,Powershell,Curl,如何传递powershell脚本的输出以更新curl命令中的参数 curl -k -H "Cache-Control:no-cache" -H "Content-Type:application/json" -u "user:pswd" -X POST http://example.com -d "{"""abc""":["""def"""],"""ghi""":["""DATA""","""HISTORY""","""CONFIGURATION"""],"""backupId""":"""xy

如何传递powershell脚本的输出以更新curl命令中的参数

curl -k -H "Cache-Control:no-cache" -H "Content-Type:application/json" -u "user:pswd" -X POST http://example.com -d "{"""abc""":["""def"""],"""ghi""":["""DATA""","""HISTORY""","""CONFIGURATION"""],"""backupId""":"""xyz"""}" 
例如:

gci D:\temp | ? { $_.BaseName } | sort CreationTime -desc | select -f 1 
如果上述命令的输出是文件夹名ie xyz,它对应于以下curl命令中的“备份id”

curl -k -H "Cache-Control:no-cache" -H "Content-Type:application/json" -u "user:pswd" -X POST http://example.com -d "{"""abc""":["""def"""],"""ghi""":["""DATA""","""HISTORY""","""CONFIGURATION"""],"""backupId""":"""xyz"""}" 

我们如何使用powershell获取文件夹名称并更新上述curl命令中的backupid参数,并使用新更新的备份id执行curl。换句话说,如果powershell的输出为1234,则应将运行上述curl的.bat文件的backupid更新为1234,并通过任务计划程序执行

听起来您在询问如何捕获powershell命令或脚本的输出,然后将其传递给从批处理文件调用的
curl
。因此,您实际上是在询问如何与批处理文件通信

这取决于如何调用powershell脚本和批处理文件。powershell脚本是否调用批处理文件?批处理文件是否运行powershell脚本?是否有其他程序或shell同时调用powershell和batch

根据这一切的工作方式,您可以尝试:

  • 将powershell输出另存为环境变量。修改批处理文件以在调用
    curl

  • 将PowerShell输出写入文件。修改批处理文件以读取此文件,并将其内容合并到
    curl
    调用中


为什么不使用
调用RestMethod
?“如果上述命令的输出是文件夹名称”-它不是,它是
[system.io.directoryinfo]
对象。您需要
选择-f 1-expandproperty name
(或fullname)使其成为文件夹名称。我尝试了以下方法。第一部分为备份id获取文件夹名称。第二部分在$opt下保存json,最后一部分执行curl命令$backupID=Get ChildItem D:\temp |%{$.BaseName}排序对象创建时间-降序|选择对象-第一个1$opt=“{”“dgcRestoreOptions”“”:[“自定义设置”],“reposerectoptions”“:[“数据”,“历史记录”,“配置”],“backupID”:“$($backupID)”,}调用命令{&curl-k-H“缓存控制:无缓存”-H“内容类型:application/json“-u”user:passwd“-X POST-d$opt}表达式或语句中意外的标记“dgcRestoreOptions”“”:[“CUSTOMIZATIONS”“”],“repoRestoreOptions”“:[“DATA”“,“HISTORY”“,“CONFIGURATION”“”],“backupId”“:“$($backupId)”,“}”。+CategoryInfo:ParserError:(:)[],ParseException+FullyQualifiedErrorId:unexpectedtokenh如何使用Invoke Webrequest来避免curl而不是下面的命令:Invoke命令{&curl-k-H“缓存控制:无缓存”-H“内容类型:application/json”-u“用户:passwd”-X POST-d$opt}