0

I am trying to copy a file from my local workspace to a remote server (not a network shared path) by using the powershell command through Inline Powershell" task in TFS vNext build definition. FYI, destination path is not a network shared path

I tried with below commands

$Session = New-PSSession -ComputerName "remote server name" -Credential "domain\username" 
Copy-Item "$(Build.SourcesDirectory)\Test.htm" -Destination "C:\inetpub\wwwroot\aspnet_client\" -ToSession $Session

But it's promoting for the password every time and I tried with entering the password manually and the result looks good.

How can we achieve this step without prompting password or credentials

AdminOfThings
  • 23,946
  • 4
  • 17
  • 27
Sam
  • 141
  • 1
  • 7
  • 1
    You should put your credentials into a PSCredential object before passing it to `New-PSSession`. See [Static Password](https://stackoverflow.com/questions/55065136/invoke-command-with-static-password/55065520#55065520) for how to create a pscredential object (`$credential`). Then just pass that variable to your command `-Credential $Credential`. – AdminOfThings Jun 06 '19 at 17:56
  • Do you have to enter creds to create a remote session on the target server? – TheMadTechnician Jun 06 '19 at 18:18
  • Thank you @AdminOfThings for your quick response and issue got resolved. – Sam Jun 06 '19 at 18:32
  • Why are you not using the Windows Machine File Copy task? – Daniel Mann Jun 06 '19 at 18:51

1 Answers1

2

Are you sure it's not on a network share? :)

Powershell only takes password as a secure string. You can use $credential = Get-Credential to render a really cool box to store those credentials for you, or if you want to store your login programmatically (not recommended for obvious security reasons) use this:

$passwd = ConvertTo-SecureString "<password>" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential("<username>",$passwd)

There might be a way to inherit your current domain credentials, but that's way beyond me, and a quick google search turns up nothing.

EDIT: Sorry I forgot to post the whole thing:

$passwd = ConvertTo-SecureString "<password>" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential("<username>",$passwd)
$Session = New-PSSession -ComputerName "remote server name" -Credential $credential
Copy-Item "$(Build.SourcesDirectory)\Test.htm" -Destination "C:\inetpub\wwwroot\aspnet_client\" -ToSession $Session
ncfx1099
  • 367
  • 1
  • 11