Invoke-Webrequest Powershell 2.0

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.

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.)

Thanks for the fast reply Dave.

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 = '<SOAP-ENV:Envelope
xmlns:SOAP-ENV=“http://schemas.xmlsoap.org/soap/envelope/
xmlns:iseeReg=“http://www.hp.com/isee/webservices/”>
<SOAP-ENV:Body>
<iseeReg:RegisterClient2>
<iseeReg:request/>
</iseeReg:RegisterClient2>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

#[xml]$regReq = Get-Content ‘templates\register_payload.xml’
[xml]$regReq = '<isee:ISEE-Registration schemaVersion=“5.00”
xmlns:isee=“http://www.hp.com/schemas/isee/5.00/event”>
<RegistrationSource>
<HP_OOSIdentifiers>
<OSID>
<Section name=“SYSTEM_IDENTIFIERS”>
<Property name=“TimestampGenerated”/>
</Section>
</OSID>
<CSID>
<Section name=“SYSTEM_IDENTIFIERS”>
<Property name=“CollectorType” value=“MC3”/>
<Property name=“CollectorVersion” value=“T05.80.1 build 1”/>
<Property name=“AutoDetectedSystemSerialNumber” value=“10”/>
<Property name=“SystemModel” value=“HP ProLiant”/>
<Property name=“TimestampGenerated”/>
</Section>
</CSID>
</HP_OOSIdentifiers>
<PRS_Address>
<AddressType>0</AddressType>
<Address1/>
<Address2/>
<Address3/>
<Address4/>
<City/>
<Region/>
<PostalCode/>
<TimeZone/>
<Country/>
</PRS_Address>
</RegistrationSource>
<HP_ISEECustomer>
<Business/>
<Name/>
</HP_ISEECustomer>
<HP_ISEEPerson>
<CommunicationMode>255</CommunicationMode>
<ContactType/>
<FirstName/>
<LastName/>
<Salutation/>
<Title/>
<EmailAddress/>
<TelephoneNumber/>
<PreferredLanguage/>
<Availability/>
</HP_ISEEPerson>
</isee:ISEE-Registration>

#[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 = '<SOAP-ENV:Envelope
xmlns:SOAP-ENV=“http://schemas.xmlsoap.org/soap/envelope/
xmlns:isee=“http://www.hp.com/isee/webservices/”>
<SOAP-ENV:Header>
<isee:IseeWebServicesHeader>
<isee:GDID/>
<isee:registrationToken/>
<isee:OSID/>
<isee:CSID/>
</isee:IseeWebServicesHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<isee:GetOOSEntitlementList2>
<isee:request/>
</isee:GetOOSEntitlementList2>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

[xml]$warReq = '<isee:ISEE-GetOOSEntitlementInfoRequest
xmlns:isee=“http://www.hp.com/schemas/isee/5.00/entitlement
schemaVersion=“5.00”>
<HP_ISEEEntitlementParameters>
<CountryCode/>
<SerialNumber/>
<ProductNumber/>
<EntitlementType/>
<EntitlementId/>
<ObligationId/>
</HP_ISEEEntitlementParameters>
</isee:ISEE-GetOOSEntitlementInfoRequest>

#[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(’<EndDate>’),19)))

$warxml = $Warxml.Trim(’<EndDate>’)

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

New-Object -TypeName psobject -Property $props

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

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.