2013-04-02 65 views
0

我想编写一个脚本来发送测试另一个进程的随机SOAP请求。不过,我似乎无法创建变量。每次我将记事本中的代码复制/粘贴到PS控制台时,脚本都以>>结束(甚至在敲击输入多次后)。即使我复制/粘贴脚本的$SOAPRequest部分也会发生同样的情况。如果我注释掉整个这个字符串,脚本就会运行(尽管由于缺少SOAP内容导致错误)。把一个SOAP请求放在一个字符串中

我已经尝试了以下的各种组合:

  • 用反斜杠
  • 删除包含磅符号
  • 创建具有在Powershell的v1和Powershell的SOAP请求的变量线逃离磅符号v2
  • 删除所有带有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> 
"@ 

即使这个问题似乎specificaly相关这里 - 字符串,这是contex脚本的其余部分T:

#---------------------------------------------------------------------- 
# 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 
} 

的执行,SOAPRequest函数的代码来自这里:http://www.iislogs.com/steveschofield/execute-a-soap-request-from-powershell

回答

1

在这里串的终止“@必须在山坳开始1

看来,当你粘贴它到代码中,它是缩进的,并且缩进的前导空格导致解析器继续读取以下代码作为here字符串的一部分。由于字符串从未正确终止,因此>>是提示输入字符串的更多数据,或终止“@。

编辑:如果您不想在进程块中搞乱缩进,您可以将可扩展下面的字符串到开始块作为脚本块,后来调用它:

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 

如果你不喜欢它搞乱了脚本的头,你可以把被如果你愿意,可以在底部封锁。PowerShell的仍然会在开始,过程,最后,不管他们是在脚本什么顺序的顺序运行它们:

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 
+0

优秀的,我并没有意识到这里串的开启和关闭不得不开始在第1列。谢谢! – gorideyourbike

+0

开场不一定要,但必须结束。 – mjolinor

相关问题