I need to be able to run a PowerShell script remotely (via Jenkins) as a different user. Since it will be executed as a Jenkins job, Get-Credential
is not an option for me. Below is the script I have created but it simply does not work.
$uname='domain\username'
$pwd='password'
$passw=Convertto-SecureString -String $pwd -AsPlainText -force
$mycred=New-object -TypeName System.Management.Automation.PSCredential -ArgumentList $uname, $passw
Invoke-Command -FilePath "C:\test_scripts\fetchquery.ps1" -Authentication default -Credential $mycred -computername localhost