我可以交互地运行这个函数就好了。当我把它放在计划任务中时它失败了。它通常返回“OK”,我从那里运行逻辑。$RequestMessage 包含 2 个用于用户名和密码的变量,它们在脚本顶部声明。
Function Invoke-PingSG
{
PARAM
(
[parameter(Mandatory=$true)]
[string]$Uri,
[parameter(Mandatory=$true)]
[string]$SOAPAction,
[parameter(Mandatory=$true)]
[xml]$RequestMessage
)
$Request = [System.Net.WebRequest]::Create($Uri)
$Request.Headers.Add("SOAPAction","`"REDACTED$SOAPAction`"")
$Request.ContentType = 'text/xml;charset="utf-8"'
$Request.Method = "POST"
$Request.KeepAlive = $true
$RequestStream = $Request.GetRequestStream()
$RequestMessage.Save($RequestStream)
$RequestStream.Close()
try
{
$Response = $Request.GetResponse()
$ResponseStream = $Response.GetResponseStream()
$Reader = [System.IO.StreamReader]($ResponseStream)
$XmlReturned = [xml]$Reader.ReadToEnd()
$ResponseStream.Close()
}
catch [System.Net.WebException]
{
$Response = $_.Exception
}
RETURN @($Response,$XmlReturned)
}
$RequestMessage = [xml]"<SOAP-ENV:Envelope xmlns:SOAP-ENV=`"http://schemas.xmlsoap.org/soap/envelope/`"
xmlns:xsd=`"http://www.w3.org/2001/XMLSchema`" xmlns:xsi=`"http://www.w3.org/2001XMLSchema-instance`">
<SOAP-ENV:Header>
<wsse:Security xmlns:wsse=`"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd`"
xmlns:wsu=`"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd`"><wsse:UsernameToken>
<wsse:Username>$AU</wsse:Username>
<wsse:Password Type=`"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText`">
$AUP</wsse:Password></wsse:UsernameToken></wsse:Security></SOAP-ENV:Header><SOAP-ENV:Body>REDACTEDSTUFF</SOAP-ENV:Body></SOAP-ENV:Envelope>"
$SendTest = Invoke-PingSG -Uri http://some.url -SOAPAction "Ping" -RequestMessage $RequestMessage -ErrorAction SilentlyContinue
$($SendTest[0].StatusCode 将导致 OK。
当我添加一些日志记录时,我得到
TerminatingError(Invoke-WebRequest): ..... ID3242: 无法对安全令牌进行身份验证或授权......
有任何想法吗?我也试过
$SendTest = Invoke-WebRequest http://some.url -Method post -ContentType 'text/xml' -Body $RequestMessage -Headers $headers -ErrorAction SilentlyContinue
它以交互方式工作,但不在计划任务中。
任务动作:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
和论点:
-ExecutionPolicy 绕过 -File C:\Scripts\myscript.ps1