Invoke-Webrequest Powershell 2.0

This topic contains 5 replies, has 2 voices, and was last updated by  Oliver Adams 3 years, 7 months ago.

  • Author
    Posts
  • #12969

    Oliver Adams
    Participant

    I have a script I need to deploy to a high number of servers. Most of which are pshell 3.0 but with some version 2.0. Could anyone give me some advice on how to recreate the following code to work in version 2.0.

    $res = Invoke-WebRequest `
    -Method Post `
    -ContentType 'text/xml; charset=utf-8' `
    -Uri "https://services.isee.hp.com/$($service)/$($service)Service.asmx" `
    -Headers @{'SOAPAction' = "http://www.hp.com/isee/webservices/$($action)"} `
    -UserAgent 'RemoteSupport/A.05.05 – gSOAP/2.7' `
    -Body $body.OuterXml

    Thanks in advance for any ideas.

  • #12984

    Oliver Adams
    Participant

    Thanks for the fast reply Dave.

  • #13350

    Oliver Adams
    Participant

    Hi Dave,

    I have still been unable to get this to work. My complete code is below if you can assist.

    Param(
    [string]$serial,
    [string]$product,
    [string]$country = 'NO'
    )

    $product = Get-WmiObject -Namespace root\wmi -class ms_systeminformation | Select -ExpandProperty SystemSKU
    $Serial = Get-WmiObject win32_bios | Select-Object -ExpandProperty SerialNumber

    $reqRegNs = @{
    'SOAP-ENV' = 'http://schemas.xmlsoap.org/soap/envelope/'
    'iseeReg' = 'http://www.hp.com/isee/webservices/'
    'isee' = 'http://www.hp.com/schemas/isee/5.00/event'
    }

    $reqWarNs = @{
    'SOAP-ENV' = 'http://schemas.xmlsoap.org/soap/envelope/'
    'iseeReg' = 'http://www.hp.com/isee/webservices/'
    'isee' = 'http://www.hp.com/isee/webservices/'
    }

    $reqWarEntNs = @{
    'SOAP-ENV' = 'http://schemas.xmlsoap.org/soap/envelope/'
    'isee' = 'http://www.hp.com/schemas/isee/5.00/entitlement'
    }

    $resNs = @{
    'soap' = 'http://schemas.xmlsoap.org/soap/envelope/'
    'isee' = 'http://www.hp.com/isee/webservices/'
    }

    function IseeRequest([string]$service, [string]$action, [xml]$body)
    {
    $res = Invoke-WebRequest `
    -UseBasicParsing `
    -Method Post `
    -ContentType 'text/xml; charset=utf-8' `
    -Uri "https://services.isee.hp.com/$($service)/$($service)Service.asmx" `
    -Headers @{'SOAPAction' = "http://www.hp.com/isee/webservices/$($action)"} `
    -UserAgent 'RemoteSupport/A.05.05 - gSOAP/2.7' `
    -Body $body.OuterXml

    [xml]$doc = $res.Content

    $isSuccess = ($doc | Select-Xml -Namespace $resNs "//soap:Envelope/soap:Body/isee:$($action)Response/isee:$($action)Result/isee:IsSuccess").Node.InnerText -as [bool]

    if (!$isSuccess) {
    $doc.OuterXml
    $err = ($doc | Select-Xml -Namespace $resNs "//soap:Envelope/soap:Body/isee:$($action)Response/isee:$($action)Result/isee:Error").Node.InnerText
    Write-Error "ISEE request failed: $($err)"
    Exit
    }

    return $doc
    }

    #
    # Register session
    #

    $curTime = Get-Date -Date (Get-Date).ToUniversalTime() -UFormat '%Y/%m/%d %H:%M:%S GMT'

    #[xml]$regEnv = Get-Content 'templates\register_soapenv.xml'
    [xml]$regEnv = '




    '

    #[xml]$regReq = Get-Content 'templates\register_payload.xml'
    [xml]$regReq = '




    0







    255









    '

    #[xml]$regEnv = Get-Content 'templates\register_soapenv.xml'
    #[xml]$regReq = Get-Content 'templates\register_payload.xml'

    # Payload parameters
    ($regReq | Select-Xml -Namespace $reqRegNs '//isee:ISEE-Registration/RegistrationSource/HP_OOSIdentifiers/OSID/Section/Property[@name="TimestampGenerated"]').Node.SetAttribute('value', $curTime)
    ($regReq | Select-Xml -Namespace $reqRegNs '//isee:ISEE-Registration/RegistrationSource/HP_OOSIdentifiers/CSID/Section/Property[@name="TimestampGenerated"]').Node.SetAttribute('value', $curTime)

    # Envelope payload
    ($regEnv | Select-Xml -Namespace $reqRegNs '//SOAP-ENV:Envelope/SOAP-ENV:Body/iseeReg:RegisterClient2/iseeReg:request').Node.InnerXml = $regEnv.CreateCDataSection($regReq.OuterXml).OuterXml

    Write-Host 'Registrating session...'
    [xml]$regRes = IseeRequest 'ClientRegistration' 'RegisterClient2' $regEnv

    $gdid = ($regRes | Select-Xml -Namespace $resNs '//soap:Envelope/soap:Body/isee:RegisterClient2Response/isee:RegisterClient2Result/isee:Gdid').Node.InnerText
    $token = ($regRes | Select-Xml -Namespace $resNs '//soap:Envelope/soap:Body/isee:RegisterClient2Response/isee:RegisterClient2Result/isee:RegistrationToken').Node.InnerText

    Write-Host "Got session id $($gdid)"

    #
    # Warranty lookup
    #

    [xml]$warEnv = '












    '

    [xml]$warReq = '







    '

    #[xml]$warEnv = Get-Content 'templates\warranty_soapenv.xml'
    #[xml]$warReq = Get-Content 'templates\warranty_payload.xml'

    # Payload parameters
    ($warReq | Select-Xml -Namespace $reqWarEntNs '//isee:ISEE-GetOOSEntitlementInfoRequest/HP_ISEEEntitlementParameters/CountryCode').Node.InnerText = $country
    ($warReq | Select-Xml -Namespace $reqWarEntNs '//isee:ISEE-GetOOSEntitlementInfoRequest/HP_ISEEEntitlementParameters/SerialNumber').Node.InnerText = $serial
    ($warReq | Select-Xml -Namespace $reqWarEntNs '//isee:ISEE-GetOOSEntitlementInfoRequest/HP_ISEEEntitlementParameters/ProductNumber').Node.InnerText = $product

    # Envelope headers and payload
    ($warEnv | Select-Xml -Namespace $reqWarNs '//SOAP-ENV:Envelope/SOAP-ENV:Header/isee:IseeWebServicesHeader/isee:GDID').Node.InnerText = $gdid
    ($warEnv | Select-Xml -Namespace $reqWarNs '//SOAP-ENV:Envelope/SOAP-ENV:Header/isee:IseeWebServicesHeader/isee:registrationToken').Node.InnerText = $token
    ($warEnv | Select-Xml -Namespace $reqWarNs '//SOAP-ENV:Envelope/SOAP-ENV:Body/isee:GetOOSEntitlementList2/isee:request').Node.InnerXml = $warEnv.CreateCDataSection($warReq.OuterXml).OuterXml

    Write-Host 'Requesting warranty...'
    [xml]$warRes = IseeRequest 'EntitlementCheck' 'GetOOSEntitlementList2' $warEnv

    Write-Host 'Dumping warranty payload:'
    $warxml = ($warRes | Select-Xml -Namespace $resNs '//soap:Envelope/soap:Body/isee:GetOOSEntitlementList2Response/isee:GetOOSEntitlementList2Result/isee:Response').Node.InnerText

    $warxml = (($warxml.Substring($warxml.IndexOf(''),19)))

    $warxml = $Warxml.Trim('')

    $props = @{
    'Warranty Expires:' = $warxml;
    'Serial No:' = $Serial;
    'Product Code' = $Product;
    'Country Code' = $Country
    }

    New-Object -TypeName psobject -Property $props

  • #13353

    Dave Wyatt
    Moderator

    That's a lot of code. What part isn't working the way you expect?

  • #13354

    Oliver Adams
    Participant

    I get

    PS C:\Scripts> .\HPWarrantyCheck-ps2.ps1 CZ23450QH9 646676-421
    Registrating session...
    Exception calling "GetRequestStream" with "0" argument(s): "The operation has timed out"
    At C:\Scripts\HPWarrantyCheck-ps2.ps1:40 char:1
    + $requestStream = $request.GetRequestStream()
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException

    If I switch back to the invoke-webrequest code it works fine so I know the service is still available.

  • #12976

    Dave Wyatt
    Moderator

    I've only used Invoke-WebRequest, personally, but I've seen examples of the underlying .NET classes as well. It'll take some testing (and probably fixing), but I think it'll wind up looking something like this:

    $request = [System.Net.WebRequest]::Create("https://services.isee.hp.com/$($service)/$($service)Service.asmx")
    $request.Headers.Add('SOAPAction', "http://www.hp.com/isee/webservices/$($action)")
    $request.UserAgent = 'RemoteSupport/A.05.05/gSOAP/2.7'
    $request.ContentType = 'text/xml; charset=utf-8'
    $request.Method = 'Post'
    
    try
    {
        $requestStream = $request.GetRequestStream()
        $streamWriter = New-Object System.IO.StreamWriter($requestStream)
        $streamWriter.Write($body.OuterXml)
    }
    finally
    {
        if ($null -ne $streamWriter) { $streamWriter.Dispose() }
        if ($null -ne $requestStream) { $requestStream.Dispose() }
    }
    
    $res = $request.GetResponse()
    

    Note: I had to take the "-" character out of your UserAgent string. It's apparently not a legal character in most HTTP text fields, and .NET was complaining about it when I tested most of this code (right up to the call to GetResponse(), since I don't know what your $action and $service variables contain.)

You must be logged in to reply to this topic.