Logon failure: unknown username or bad password error on Windows Server 2012 R2 for Remote PS script execution

991 Views Asked by At

I am trying to run a powershell script from a TeamCity Windows Slave to another server for deploying my application.

This is BuildConfig:

username = "<username>"
$password = "<password>"
$secstr = New-Object -TypeName System.Security.SecureString; 
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr;
Invoke-Command -ComputerName "<computer_name>" -Credential $cred -FilePath "deploy.ps1"

I am getting the following error.

following error message : Logon failure: unknown user name or bad password. 
[13:57:52]  [Step 1/1] For more information, see the about_Remote_Troubleshooting Help topic.

I have used the correct User name and password only. I have also checked the Local User security policies. Am I missing something?

1

There are 1 best solutions below

3
Gabriel McColl On

Is it just a paste error that missed the $ from username ? Does the user name and password work fine outside the script ? Are the variables being substituted in properly. Do the logs show ? I’ve never used that construct to make a pscredential before. Is this one mentioned here not working.

https://pscustomobject.github.io/powershell/howto/PowerShell-Create-Credential-Object/