Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 将SOAP请求放入here字符串中_Xml_Powershell_Soap - Fatal编程技术网

Xml 将SOAP请求放入here字符串中

Xml 将SOAP请求放入here字符串中,xml,powershell,soap,Xml,Powershell,Soap,我正在尝试编写一个脚本,该脚本将发送随机SOAP请求以测试另一个进程。然而,我似乎无法创建变量。每次我将代码从记事本复制/粘贴到PS控制台时,脚本都以>>结尾(即使在多次按enter键之后)。即使我只复制/粘贴脚本的$SOAPRequest部分,也会发生同样的情况。如果我注释掉整个here字符串,脚本将运行(尽管由于缺少SOAP内容而出现错误) 我尝试了以下各种组合: 用反斜杠转义英镑符号 删除包含英镑符号的行 在Powershell v1和Powershell v2中使用SOAP请求创建变量

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

我尝试了以下各种组合:

  • 用反斜杠转义英镑符号
  • 删除包含英镑符号的行
  • 在Powershell v1和Powershell v2中使用SOAP请求创建变量
  • 删除具有http地址的所有行
  • 使用@“”@(不仅不起作用,而且我需要变量扩展)
问题:如何让Powershell将here字符串中的内容设置为
$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>
"@
$SOAPRequest=[xml]@”
使用者
#PASSWD#
网格
软件分发
软件请求
源名称
创造
真的
标准
$pkgID
$pkgName
\\网络\Share\With\Content
爆炸了的
批处理文件
3600
真的
"@
尽管此问题似乎与here字符串有关,但以下是上下文脚本的其余部分:

#----------------------------------------------------------------------
# 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
}
#----------------------------------------------------------------------
#变数
#----------------------------------------------------------------------
$pkgID=346
$pkgNameList=@(“包ABC”、“包定义”、“包XYZ”、“包123”)
#设置起止日期戳
$now=获取日期
$end=获取日期“04/03/2013 08:00 AM”
$testDirectory=“C:\Users\UserName\Desktop\AutomatedSOAPTest”
if(!(测试路径$testDirectory)){
新项$testDirectory-itemType目录| Out Null
}
#----------------------------------------------------------------------
#功能
#----------------------------------------------------------------------
#用于写入SOAP请求日志的函数
函数写入日志($message)
{
$logDate=获取日期-格式为“MM/dd/yy HH:MM:ss”
$logPath=“$testDirectory\progress.log”
写入输出“$logDate$message”|输出文件-文件路径$logPath-追加
}#结束写入日志函数
#函数写入SOAP返回日志
函数写入返回($xml,$item)
{
$logDate=获取日期-格式为“MM/dd/yy HH:MM:ss”
$logPath=“$testDirectory\SOAP\u return-$item.log”
$success=$xml.status.success
$message=$xml.status.message
写入输出“在$logDate上为$item返回的请求”|输出文件-文件路径$logPath-追加
写入输出“Success:$Success”| Out File-FilePath$logPath-Append
写入输出$message | Out文件-文件路径$logPath-追加
}#结束写入日志函数
#函数调用SOAP请求
函数执行SOAPRequest(
[Int]$pkgID,
[字符串]$pkgName
)
{
$SOAPRequest=[xml]@”
"@
$SOAPurl=“http://”
写入日志“向服务器发送$pkgName的SOAP请求:$SOAPurl”
$soapWebRequest=[System.Net.WebRequest]::创建($SOAPurl)
$soapWebRequest.Headers.Add(“SOAPAction”,“``”)
$soapWebRequest.ContentType=“text/xml;字符集=`“utf-8`”
$soapWebRequest.Accept=“text/xml”
$soapWebRequest.Method=“POST”
写入日志“启动发送”
$requestStream=$soapWebRequest.GetRequestStream()
$SOAPRequest.Save($requestStream)
$requestStream.Close()
写入日志“发送完成,等待响应。”
$resp=$soapWebRequest.GetResponse()
$responseStream=$resp.GetResponseStream()
$soapReader=[System.IO.StreamReader]($responseStream)
$ReturnXml=[Xml]$soapReader.ReadToEnd()
$responseStream.Close()
写入日志“收到响应”
写入返回$ReturnXml.status.success
写入返回$ReturnXml.status.message
}#结束执行SOAPRequest函数
#----------------------------------------------------------------------
#代码
#----------------------------------------------------------------------
而($now-lt$end)
{
$pkgList=Get Random-输入$pkgNameList-计数4
foreach($pkgName in$pkgList)
{
#运行函数以执行SOAP请求
执行SOAPRequest$pkgID$pkgName
$pkgID++
}
开始睡眠-s 3600
$now=获取日期
}
Execute SOAPRequest函数的代码来自这里:

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

当您将其粘贴到代码中时,它会出现缩进,缩进中的前导空格会导致解析器继续读取以下代码作为here字符串的一部分。由于该字符串从未正确终止,因此>>会提示输入here字符串的更多数据,或终止“@”

编辑:如果不想弄乱流程块中的缩进,可以将可展开的here字符串作为scriptblock移动到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仍将按开始、进程、结束的顺序运行它们,无论它们在脚本中的顺序是什么:

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

很好,我没有意识到here字符串的打开和关闭必须从第1列开始。谢谢不必打开,但必须关闭。