0

I want to login/communicate to a remote system using RESTAPI and I want to use the same AD credentials of the user firing the script from a windows machine. Example:- I am logged into a Windows server with the credentials "Domain\User" with password "password" and want to login using the same credentials to login to a remote system without entering the username and password.

2 Answers 2

1

Try using WebClient class:

$webClient = new-object System.Net.WebClient
$webClient.UseDefaultCredentials = $true
$reply = $webClient.DownloadString("http://google.com")
$reply

The main thing here is to set UseDefaultCredentials property of WebClient to true. Method DownloadString is just an example of how to use WebClient. There are plenty of other methods which you can use depending on your REST endpoint. You can find full list here.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks ,Let me check and get back to you.
So how do we use this to pass the credentials.I was previously using this to manually pass the credentials. Invoke-RestMethod -uri $uri -Credential $credentials -method Get
You do not need to - by specifying UseDefaultCredentials you tell WebClient to use current user's credentials.
0

I would recommend using Invoke-RestMethod with -UseDefaultCredentials switch.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.