Credential in Invoke-sqlcmd command

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?

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

[quote quote=205410][/quote]
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.

[quote quote=205437]Invoke-Sqlcmd has a -User and -Password parameters. Try using those instead of credentials.

[/quote]
I tried and get the same error "Login failed for user <Username>

 

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.

Use -HostName instead of -ServerInstance it should work.

[pre]Invoke-Sqlcmd -Credential $cred -Query “SELECT * from TESTDB” -HostName “test4”[/pre]

Thank you.