Powershell: System.Net.WebRequest

sakuraime 2,331 Reputation points
2021-02-08T15:05:49.65+00:00

I have the following code

$Req = [System.Net.WebRequest]::Create($URL);
$Req.Credentials = new-object System.Net.NetworkCredential($Username, $Password, $Domain)
$Req.Timeout = 30000;

is it possible to use single sign on using domain account instead of hardcode the password in the script ?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,576 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ian Xue 38,551 Reputation points Microsoft Vendor
    2021-02-09T03:08:38.043+00:00

    Hi,

    You can use the Get-Credential cmdlet

    $Req.Credentials = (Get-Credential).GetNetworkCredential()  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.