-1

I am using the below command,

$SQLServerPSCred = Get-Credential 

to get the credential object.

How can I get the Creds object without the Prompt?

I have tried below code, but it is failing.

$SQLServerPSCred = New-Object System.Management.Automation.PSCredential -ArgumentList $UserName, $Password
Suhas Parameshwara
  • 1,344
  • 7
  • 15
Akshay
  • 107
  • 2
  • 11
  • 1
    "but it is failing" - _how_ is it failing? Error messages? Access Denied? – Mathias R. Jessen Nov 06 '19 at 12:07
  • The password should be a secure string - see https://blog.kloud.com.au/2016/04/21/using-saved-credentials-securely-in-powershell-scripts/ for details – Scepticalist Nov 06 '19 at 12:22
  • Does this answer your question? [Using PowerShell credentials without being prompted for a password](https://stackoverflow.com/questions/6239647/using-powershell-credentials-without-being-prompted-for-a-password) – Scepticalist Nov 06 '19 at 12:22
  • @MathiasR.Jessen error message is : Invoke-Sqlcmd2 : Exception calling "Open" with "0" argument(s): "Login failed for user 'abcd'." – Akshay Nov 06 '19 at 12:34
  • @Scepticalist I tried that approach it is allowing me to login using Get-Credential, but it fails when I tried with $SQLServerPSCred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName, $Password – Akshay Nov 06 '19 at 12:37
  • You didn't read the article - see below. You need to convert the password into a secure string. – Scepticalist Nov 06 '19 at 12:39
  • I am ok if the password is hard-coded...the only thing is prompt should not come. – Akshay Nov 06 '19 at 12:40
  • @Scepticalist I did convert the password into secure string – Akshay Nov 06 '19 at 12:41

1 Answers1

1

You haven't included much information about what your actual problem with the code is. it's correct that you're prompted with your first example. As you explicitly tell it to ask you.

With your second example it's incomplete.

PowerShell – How to create a PSCredential object by koteshblog has an example on how to do it:

$secpasswd = ConvertTo-SecureString "PlainTextPassword" -AsPlainText -Force 
$mycreds = New-Object System.Management.Automation.PSCredential ("username", $secpasswd)

As you can see you first need to convert the password into a SecureString which you can pass to the credential object. It also shows a slightly different syntax but that shouldn't matter.

Depending on what you're doing and where that script resides it can also be worth it to look into some form of "more secure" storage for your secrets. As it is everyone who would have access to the script could extract the username and password from it.

One option is to store the secure string. But keep in mind a secure string is somewhat tied to the users. So You won't be able to easily just pass along a text file that contains the password.

For an example on how to do that check Powershell Tip – Storing and Using Password Credentials by robcost. It's a basic solutions there might be better ones available depending on your needs.

Seth
  • 1,215
  • 15
  • 35
  • I have tried : $UserName = "abc" $Password = ConvertTo-SecureString "abcdefgh" -AsPlainText -Force $SQLServerPSCred = New-Object System.Management.Automation.PSCredential ($UserName, $Password) The error I am gettting is : Invoke-Sqlcmd2 : Exception calling "Open" with "0" argument(s): "Login failed for user 'abc'." But when I am using : $SQLServerPSCred = Get-Credential and then pass the credentials ...I am able to login. – Akshay Nov 06 '19 at 12:54
  • 1
    Did you try to use single-quote characters around the password instead of the double-quotes you show here? The plaintext password may contain an `$` character and it should be taken literally. See [here](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-6#single-and-double-quoted-strings) – Theo Nov 06 '19 at 13:10
  • Also make sure to include a possible domain name. – Seth Nov 06 '19 at 13:14
  • yes..That password should be in a single quote...that works for me. – Akshay Nov 06 '19 at 13:28