Link to home
Start Free TrialLog in
Avatar of Leon Taljaard
Leon TaljaardFlag for South Africa

asked on

Invoke-WebRequest with basic authentication

Hi Guys,

I have a web service i need to get the XML returned from but it requires basic authentication, so I am needing some help on formatting the PowerShell script.

The web service has a specific method I need to call as well as to variables that I need to pass with specific values, on variable is "Datsource" and the other is "CustomerID"

Some help on getting the basic authentication added would be a great help please, i have the script working but it fails with an error 400 Bad Request.

This is what I have so far

$Datasource = 'Value'
$CustomerID ='Value'

Invoke-WebRequest "http://ServerName/CustomerCareServiceV1.3.1/ProductService.svc/Basic/GetAvailableProducts?DataSource=$Datasource&CustomerID=$CustomerID&CustomerIDSpecified=True"

Open in new window

Avatar of Nadav Solomon
Nadav Solomon

Avatar of Leon Taljaard

ASKER

Hi Nadav,

We are at it again :)

I have tried that, I still get error 400 bad request, the problem is that I need to use "Basic" authentication
Try this one, worked for me: (using basic iis authentication)
$url = "http://localhost/temp/default.htm"
$user = "userName"
$pass = "pass..."
$secPass = convertTo-secureString $pass -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($user,$secPass)

$data = Invoke-RestMethod $url -Credential $cred
$data

Open in new window

This is what i have just tried

$Datasource = 'DTH'
$CustomerID ='36526867'

$url = "http://23RNB-W4INT01/CustomerCareServiceV1.3.1/ProductService.svc/GetAvailableProducts?DataSource=$Datasource&CustomerID=$CustomerID&CustomerIDSpecified=True"
$user = "za\btgaccount"
$pass = "BTG@ccount2"
$secPass = convertTo-secureString $pass -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($user,$secPass)

$data = Invoke-RestMethod $url -Credential $cred
$data

Open in new window

I still get 400 bad request.

The method is GetAvailableProductsw

This is the XML Payload Request required

<Datasource>String</Datasource>
<CustomerID>0</CustomerID>
<CustomerIDSpecified>True</CustomerIDSpecified>

Open in new window

First try without the domain (just for the test) second if you browse to that url using internet explorer do you get a prompt for user and password?  if you type this user and password does it work?
By saying without the domain I mean just the username not domain\username
Getavailableproducts is a file or virtual directory?
Hi Nadav,

Sorry for the delayed response.

You cannot open that URL normally by browsing to it. The "Getavailableproducts" is a method name which requires datasource, customerid and customeridspecified values to be passed to it.
ASKER CERTIFIED SOLUTION
Avatar of Leon Taljaard
Leon Taljaard
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I managed to get this worked out by changing the method i was calling and using the full method name with the endpoint