2013-04-02 57 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

相關問題