Search code examples
powershelltfspowershell-remotingvnext

How to copy a file from local work space to remote server (not a network shared path) with powershell


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


Solution

  • 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