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

LVL 8
Leon TaljaardSystem Center Specialist - ArchitectureAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nadav SolomonCommented:
0
Leon TaljaardSystem Center Specialist - ArchitectureAuthor Commented:
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
0
Nadav SolomonCommented:
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

1
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

Leon TaljaardSystem Center Specialist - ArchitectureAuthor Commented:
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

0
Nadav SolomonCommented:
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?
0
Nadav SolomonCommented:
By saying without the domain I mean just the username not domain\username
0
Nadav SolomonCommented:
Getavailableproducts is a file or virtual directory?
0
Leon TaljaardSystem Center Specialist - ArchitectureAuthor Commented:
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.
0
Leon TaljaardSystem Center Specialist - ArchitectureAuthor Commented:
Hi Nadav,

Seems like i got it sorted out, I tried the Invoke Web Services Activity within Orchestrator again but this time i used the endpoint@method name as the full method when I do my request and it worked perfectly.

Thank you so much for all your help
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Leon TaljaardSystem Center Specialist - ArchitectureAuthor Commented:
I managed to get this worked out by changing the method i was calling and using the full method name with the endpoint
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.