Credential in Invoke-sqlcmd command

Welcome Forums General PowerShell Q&A Credential in Invoke-sqlcmd command

  • This topic has 5 replies, 3 voices, and was last updated 3 months ago by
    Moderator
    .
Viewing 5 reply threads
  • Author
    Posts
    • #205410
      Participant
      Topics: 12
      Replies: 21
      Points: 135
      Rank: Participant

      I am not able to pass credential or says login failed(when it is correct)

      $cred = $host.ui.PromptForCredential("Need credentials for the Server", "Please enter your user name and password.", "", "NetBiosUserName")
      Invoke-Sqlcmd -Credential $cred -Query (SELECT * from TESTDB" -ServerInstance "test4"
      

      Request the expert help to get this corrected. it looks like the -credential doesnt work for invoke-sqlcmd. I see few answer as run as powershell with the required accounts, but I am combining many other script and want to work on credential popup..any help?

      • This topic was modified 3 months ago by kvprasoon. Reason: code formatting
    • #205437
      Senior Moderator
      Topics: 8
      Replies: 1215
      Points: 4,334
      Helping Hand
      Rank: Community Hero

      Invoke-Sqlcmd has a -User and -Password parameters. Try using those instead of credentials.

    • #205473
      Moderator
      Topics: 3
      Replies: 216
      Points: 1,176
      Helping Hand
      Rank: Community Hero

      Are running it from SQLServer drive or from the other path? You haven’t mentioned instance details.

      Can you please provide the complete code and error details?

      Thank you.

    • #205521
      Participant
      Topics: 12
      Replies: 21
      Points: 135
      Rank: Participant

      Invoke-Sqlcmd has a -User and -Password parameters. Try using those instead of credentials.

      I tried and get the same error “Login failed for user <Username>

       

    • #205524
      Participant
      Topics: 12
      Replies: 21
      Points: 135
      Rank: Participant

      Are running it from SQLServer drive or from the other path? You haven’t mentioned instance details.

      Can you please provide the complete code and error details?

      Thank you.

      Not from SQL Drive, I had to install module and connecting remotely. I get the error as login failed for user <username>. It using the default Instance and therefore just the servername.

    • #205584
      Moderator
      Topics: 3
      Replies: 216
      Points: 1,176
      Helping Hand
      Rank: Community Hero

      Not from SQL Drive, I had to install module and connecting remotely. I get the error as login failed for user . It using the default Instance and therefore just the servername.

      Use -HostName instead of -ServerInstance it should work.

      Invoke-Sqlcmd -Credential $cred -Query “SELECT * from TESTDB” -HostName “test4”

      Thank you.

      • This reply was modified 3 months ago by Kiran.
Viewing 5 reply threads
  • The topic ‘Credential in Invoke-sqlcmd command’ is closed to new replies.