2013-04-02 3 views
0

다른 프로세스를 테스트하기 위해 임의의 SOAP 요청을 보내는 스크립트를 작성하려고합니다. 그러나 나는 변수를 생성 할 수 없다. 메모장에서 PS 콘솔로 코드를 복사/붙여 넣을 때마다 스크립트가 >>로 끝납니다 (여러 번 입력 한 후에도). 스크립트의 $SOAPRequest 부분 만 복사/붙여 넣기해도 똑같은 일이 발생합니다. 전체 here-string을 주석 처리하면 스크립트가 실행됩니다 (SOAP 컨텐츠 누락으로 인한 오류 임에도 불구하고).here-string에 SOAP 요청하기

  • 파워 쉘 v1 및 PowerShell에서 SOAP 요청과 변수를 만들기 파운드 기호
  • 을 포함하는 행을 삭제 백 슬래시
  • 와 파운드 기호를 이스케이프 :

    나는 다음의 다양한 조합을 시도했습니다 v2

  • http 주소가있는 모든 줄 삭제
  • @ ''@ (작동하지 않았지만 변수 확장이 필요함)

질문 : here-string의 내용을 Powershell에서 $SOAPRequest 변수로 설정할 수 있습니까? 다른 말로하면 >> 어떻게 멈출 수 있습니까? 보통은 괄호 나 괄호 또는 이중 인용 부호가 빠진 것을 의미하지만 그와 같은 것을 찾을 수는 없습니다. 나는 이것이 왜 효과가 없는지에 관해서는 손해보고있다.

$ SOAPRequest 변수 :

페이지는 내가 도움을 보았다

이 문제는 여기 -에 specificaly 관련 보인다하더라도
$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> 
"@ 

string, 이것은 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

하여 여기에 문자열의 종료 "@ COL에서 시작해야합니다 1.

이 나타납니다 코드에 들여 쓰게되었고, 들여 쓰기에서 나오는 공백으로 인해 파서는 here-string의 일부로 다음 코드를 계속 읽게됩니다. 문자열이 제대로 종료 된 적이 없기 때문에 >>는 here-string 또는 종료하는 "@"에 대한 더 많은 데이터를 요구합니다.

편집 : 프로세스 블록에서 들여 쓰기를 원하지 않는 경우 당신이 존재를 넣을 수 있습니다,

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은 스크립트의 순서에 상관없이 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 
+0

우수, 여기서는 시작 및 종료를 알지 못했습니다. 감사합니다! – gorideyourbike

+0

개통은 할 필요는 없지만 개점은 필수입니다. – mjolinor