我正在尝试编写一个脚本,该脚本将发送随机 SOAP 请求以测试另一个进程。但是我似乎无法创建变量。每次我将代码从记事本复制/粘贴到 PS 控制台时,脚本都会以 >> 结尾(即使在多次按 Enter 后也是如此)。即使我只复制/粘贴$SOAPRequest
脚本的一部分,也会发生同样的事情。如果我注释掉整个 here-string,脚本就会运行(尽管由于缺少 SOAP 内容而出现错误)。
我尝试了以下各种组合:
- 用反斜杠转义英镑符号
- 删除包含磅符号的行
- 在 Powershell v1 和 Powershell v2 中使用 SOAP 请求创建变量
- 删除所有带有 http 地址的行
- 使用@''@(不仅没有用,而且我需要变量扩展)
问题:如何让 Powershell 将 here-string 中的内容设置为$SOAPRequest
变量?换句话说,我怎样才能停止>>?通常这意味着我错过了括号、括号或双引号,但我似乎找不到类似的东西。我不知道为什么这不起作用。
我寻求帮助的页面:
- 带参数的 Powershell SOAP 请求
- http://blogs.technet.com/b/heyscriptingguy/archive/2011/02/17/use-powershell-to-simplify-working-with-xml-data.aspx
$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