In powershell I want to use the username and password which are stored in $uname, $pass. These are actually read from a file and is stored in $uname and $pass, each time the script executes. I have tried like
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $uname,$pass
But the error is
new-object : Cannot find an overload for "PSCredential" and the argument count: "2".
I need to use these credentials for a New-PsSession later on in the script. Can someone please help at the earliest?
The PSCredential object represents a set of security credentials such as a user name and password. The object can be passed as a parameter to a function that runs as the user account in that credential object. There are a few ways that you can create a credential object.
Typically, to create a PSCredential object, you'd use the Get-Credential cmdlet. The Get-Credential cmdlet is the most common way that PowerShell receives input to create the PSCredential object like the username and password. The Get-Credential cmdlet works fine and all but it's interactive.
You will have to convert your plaintext password to a secure string first:
$password=$pass|ConvertTo-SecureString -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PsCredential("[email protected]",$password)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With