【问题标题】:parse variable in XML POST request with PowerShell使用 PowerShell 解析 XML POST 请求中的变量
【发布时间】:2018-03-06 06:49:33
【问题描述】:

我正在尝试将变量(日期)放入 XML 帖子中,但我无法让它工作。 在 XML 帖子中,我需要指定 2 个日期(vndg=today 和 morg=today+1)。

这是我的 PowerShell 脚本,它可以在没有日期变量的情况下工作。

$vndg = (Get-Date).ToString("dd-MM-yyyy")
$morg = (Get-Date).AddDays(+1).ToString("dd-MM-yyyy")

[XML]$SOAP = @'
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <Clocking_GetByDateRangeUtc xmlns="http://www.geodynamics.be/webservices">
      <caller>
        <CompanyName>companyname</CompanyName>
        <LoginName>username</LoginName>
        <Password>password</Password>
      </caller>
      <fromDateUtc>$vndg</fromDateUtc>
      <toDateUtc>$morg</toDateUtc>
    </Clocking_GetByDateRangeUtc>
  </soap:Body>
</soap:Envelope>
'@

$headers = @{"SOAPAction" = "http://www.geodynamics.be/webservices/Clocking_GetByDateRangeUtc"}
$destination = 'C:\Temp\GeoDynamics\Downloads\GeoPers.xml'

$URI = "https://secure.geodynamics.be/webservices/intellitracer/1.0/IntegratorWebservice.asmx?WSDL"
$out = Invoke-WebRequest $uri -Method Post -ContentType 'text/xml' -Body $SOAP -Headers $headers -OutFile $destination

【问题讨论】:

  • @'...'@ -> @"..."@
  • 如果我这样做了,我会得到一个错误 ==> 字符串 '25-09-2017' 不是有效的 AllXsd 值。
  • 我通过更改日期变量的格式使其工作 ==> $vndg = (get-date).ToString("yyyy-MM-ddT00:00:00") $morg = ( get-date).AddDays(+1).ToString("yyyy-MM-ddT00:00:00")
  • (Get-Date).Date.ToString('s')

标签: xml powershell variables soap


【解决方案1】:

我通过将脚本更改为以下内容使其工作:

[XML]$SOAP = @"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <Clocking_GetByDateRangeUtc xmlns="http://www.geodynamics.be/webservices">
      <caller>
        <CompanyName>name</CompanyName>
        <LoginName>login</LoginName>
        <Password>password</Password>
      </caller>
      <fromDateUtc>$vndg</fromDateUtc>
      <toDateUtc>$morg</toDateUtc>
    </Clocking_GetByDateRangeUtc>
  </soap:Body>
</soap:Envelope>
"@

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    • 2017-03-04
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    • 2013-08-04
    • 2020-12-30
    相关资源
    最近更新 更多