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
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.
3 Comments
user2525672
Thanks ,Let me check and get back to you.
user2525672
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
Yevgeniy.Chernobrivets
You do not need to - by specifying UseDefaultCredentials you tell WebClient to use current user's credentials.