将SOAP请求放在here-string中

时间:2013-04-02 17:33:21

标签: xml powershell soap

我正在尝试编写一个脚本,该脚本将发送随机SOAP请求以测试另一个进程。但是我似乎无法创建变量。每次我将代码从记事本复制/粘贴到PS控制台时,脚本都以>>结尾。 (即使多次击中后也是如此)。即使我只复制/粘贴脚本的$SOAPRequest部分,也会发生同样的事情。如果我注释掉整个here-string,脚本就会运行(尽管由于缺少SOAP内容而导致错误)。

我尝试了以下各种组合:

  • 使用反斜杠转义英镑符号
  • 删除包含井号的
  • 在Powershell v1和Powershell v2中使用SOAP请求创建变量
  • 删除所有包含http地址的行
  • 使用@''@(不仅没有工作,我需要变量扩展)

问题:如何让Powershell将here-string中的内容设置为$SOAPRequest变量?换句话说,我怎么能停止>>?通常这意味着我错过了括号,括号或双引号,但我似乎找不到那样的东西。我不知道为什么这不起作用。

我寻求帮助的页面:

$ SOAPRequest变量:

$SOAPRequest = [xml] @"
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soa="http://bmc.com/ao/xsd/2008/09/soa">
<soapenv:Header>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" soapenv:mustUnderstand="1">
<wsse:UsernameToken>
<wsse:Username>USER</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">#PASSWD#</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<soa:executeProcess>
<soa:gridName>GRID</soa:gridName>
<soa:processName>Software_Distribution</soa:processName>
<soa:parameters>
<soa:Input>
<soa:Parameter>
<soa:Name required="true">Software Request</soa:Name>
<soa:Value soa:type="xs:anyType">
<soa:XmlDoc>
<request>
<Source>SourceName</Source>
<Command>create</Command>
<Debug>true</Debug>
<DeployType>standard</DeployType>
<PkgId>$pkgID</PkgId>
<PackageName>$pkgName</PackageName>
<PackageShareLocation>\\Network\Share\With\Content</PackageShareLocation>
<PackageFormat>exploded</PackageFormat>
<InstallScript>install.bat</InstallScript>
<InstallTimeout>3600</InstallTimeout>
<SilentInstall>True</SilentInstall>
<Emails />
</request>
</soa:XmlDoc>
</soa:Value>
</soa:Parameter>
</soa:Input>
</soa:parameters>
</soa:executeProcess>
</soapenv:Body>
</soapenv:Envelope>
"@

尽管此问题似乎与here-string具体相关,但这是上下文脚本的其余部分:

#----------------------------------------------------------------------
# Variables
#----------------------------------------------------------------------
$pkgID = 346
$pkgNameList = @("Package_ABC", "Package_DEF", "Package_XYZ", "Package_123" )

#Set start/end datestamps
$now= Get-Date
$end = Get-Date "04/03/2013 08:00 AM"


$testDirectory = "C:\Users\UserName\Desktop\AutomatedSOAPTest"
if (!(Test-Path $testDirectory)){
    New-Item $testDirectory -itemType directory | Out-Null
}

#----------------------------------------------------------------------
# Functions
#----------------------------------------------------------------------
#Function to write to SOAP request log
function Write-Log($message)
{
    $logDate = Get-Date -format "MM/dd/yy HH:mm:ss"
    $logPath = "$testDirectory\progress.log"

    Write-Output "$logDate $message" | Out-File -FilePath $logPath -Append
}#end Write-Log function

#Function to write to SOAP return log
function Write-Return($xml, $item)
{
    $logDate = Get-Date -format "MM/dd/yy HH:mm:ss"
    $logPath = "$testDirectory\SOAP_return-$item.log"

    $success = $xml.status.success
    $message = $xml.status.message

    Write-Output "Request returned for $item on $logDate" | Out-File -FilePath $logPath -Append
    Write-Output "Success: $success" | Out-File -FilePath $logPath -Append
    Write-Output $message | Out-File -FilePath $logPath -Append
}#end Write-Log function

#Function to call SOAP request
function Execute-SOAPRequest(
    [Int]    $pkgID,
    [String] $pkgName
)
{
    $SOAPRequest = [xml] @"
        <SOAP request content here>
    "@

    $SOAPurl = "http://<site where requests are sent>"

    Write-Log "Sending SOAP Request for $pkgName To Server: $SOAPurl"
    $soapWebRequest = [System.Net.WebRequest]::Create($SOAPurl)
    $soapWebRequest.Headers.Add("SOAPAction","`"`"")

    $soapWebRequest.ContentType = "text/xml;charset=`"utf-8`""
    $soapWebRequest.Accept      = "text/xml"
    $soapWebRequest.Method      = "POST"

    Write-Log "Initiating Send."
    $requestStream = $soapWebRequest.GetRequestStream()
    $SOAPRequest.Save($requestStream)
    $requestStream.Close()

    Write-Log "Send Complete, Waiting For Response."
    $resp = $soapWebRequest.GetResponse()
    $responseStream = $resp.GetResponseStream()
    $soapReader = [System.IO.StreamReader]($responseStream)
    $ReturnXml = [Xml] $soapReader.ReadToEnd()
    $responseStream.Close()

    Write-Log "Response Received."

    Write-Return $ReturnXml.status.success
    Write-Return $ReturnXml.status.message
}#End Execute-SOAPRequest function

#----------------------------------------------------------------------
# Code
#----------------------------------------------------------------------

while($now -lt $end)
{
    $pkgList = Get-Random -input $pkgNameList -count 4

    foreach($pkgName in $pkgList)
    {
        #Run function to execute SOAP request
        Execute-SOAPRequest $pkgID $pkgName

        $pkgID++
    }

    Start-Sleep -s 3600
    $now = Get-Date
}

Execute-SOAPRequest函数的代码来自:http://www.iislogs.com/steveschofield/execute-a-soap-request-from-powershell

1 个答案:

答案 0 :(得分:2)

此处字符串中的@终止必须从第1列开始。

当您将其粘贴到代码中时,它会缩进,并且来自缩进的前导空格会导致解析器继续读取以下代码作为here-string的一部分。由于字符串从未正确终止,因此&gt;&gt;提示输入here-string或终止“@。

的更多数据

编辑:如果你不想弄乱进程块中的缩进,你可以将一个可扩展的here-string作为一个脚本块移动到Begin块中,然后再调用它:

Begin{
$SoapString = {
@"
This is my here-string containing $variable
"@
}
}


Process{
$variable = "MY VARIABLE"

  foreach ($x in 1)
   {
     &$SoapString
    }
}


This is my here-string containing MY VARIABLE

如果你不喜欢它会破坏脚本的头部,你可以根据需要将Being块放在底部。 Powershell仍会按照Begin,Process,End的顺序运行它们,无论它们在脚本中的顺序如何:

Process{
$variable = "MY VARIABLE"

  foreach ($x in 1)
   {
     &$SoapString
   }
}

Begin{
$SoapString = {
@"
This is my here-string containing $variable
"@
}
}

This is my here-string containing MY VARIABLE