Copy files on the target server back to the Azure DevOps Agent


I was asked yesterday about copying files from a remote computer back to the computer running the Azure DevOps Agent. I’ve never had a requirement for something like this but hey, I love a challenge and it turned out to be simpler than I originally thought due to the fact that Copy-Item supports it out of the box.

The PowerShell Script (with variables)

$pso = New-PsSessionOption -SkipCACheck -SkipCNCheck
$username = "$(AdminUser)"
$password = ConvertTo-SecureString $(AdminPassword) -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList ($username, $password)

$session = New-PSSession -ComputerName $(CognosServer) -Credential $cred -UseSSL -SessionOption $pso 

Copy-Item $(DestinationFolder)\deploy-vis.ps1 -Destination $(System.DefaultWorkingDirectory)\deploy-vis-new.ps1 -FromSession $session

NOTE: The variables I used above are defined in my pipeline

This post of course assumes you have WinRM configured on the remote computer. If you do not have WinRM configured, do not fear Microsoft has a script to perform the configuration for you.

Configure WinRM on the Target machine(s)

Download from GitHub this PowerShell script for Windows 10 and Windows Server 2016 or higher, or this PowerShell script for previous versions of Windows. Copy it to your target machine, and decide whether you plan to use HTTP or HTTPS to communicate to the target machine from the Agent.

If you choose HTTP, you must use a fully qualified domain name (FQDN). To use a FQDN to access the target machine(s), execute the following in a Windows PowerShell command prompt As Administrator:

.\ConfigureWinRM.ps1 {FQDN} http

If you choose HTTPS, you can use either a fully qualified domain name (FQDN) or an IP address to access the target machine(s). To use a FQDN to access the target machine(s), execute the following in a Windows PowerShell command prompt As Administrator:

.\ConfigureWinRM.ps1 {FQDN} https

To use an IP address to access the target machine(s), execute the following in a Windows PowerShell command prompt As Administrator:

.\ConfigureWinRM.ps1 {ipaddress} https

Azure DevOps – Azure Pipelines

The PowerShell Task should look something like this in a Classic Build or Release pipeline

PowerShell Task

Don’t forget YAML

Here is the equivalent task using YAML – watch out for your indenting 🙂

steps:
- powershell: |
   # This example copies a file from the remote PC
   # The file happens to be one I copied to the server, renamed it on the way back to the agent
   
   $pso = New-PsSessionOption -SkipCACheck -SkipCNCheck
   $username = "$(AdminUser)"
   $password = ConvertTo-SecureString $(AdminPassword) -AsPlainText -Force
   $cred = New-Object System.Management.Automation.PSCredential -ArgumentList ($username, $password)
   $session = New-PSSession -ComputerName $(WindowsServer) -Credential $cred -UseSSL -SessionOption $pso 
   Copy-Item $(DestinationFolder)\deploy-vis.ps1 -Destination $(System_DefaultWorkingDirectory)\deploy-vis-new.ps1 -FromSession $session
   
  displayName: 'PowerShell Script - Copy file from the $(WindowsServer)'

If you wanted to use environment variables in your PowerShell step that are not defined prior to the step you can add the following env: to your PowerShell YAML task so they are present in the PowerShell environment when the task runs.

steps:
- powershell: |
   $pso = New-PsSessionOption -SkipCACheck -SkipCNCheck
   $username = "$env:User"
   $password = ConvertTo-SecureString $Env:Password -AsPlainText -Force
   $cred = New-Object System.Management.Automation.PSCredential -ArgumentList ($username, $password)
   $session = New-PSSession -ComputerName $Env:WindowsServer -Credential $cred -UseSSL -SessionOption $pso 
   Copy-Item $env:DestDir\deploy-vis.ps1 -Destination $env:WorkDir\deploy-vis-new.ps1 -FromSession $session
   
  displayName: 'PowerShell Script - Copy file from the $(WindowsServer)'
  env:
    User: $(AdminUser)
    Password: $(AdminPassword)
    WorkDir: $(System.DefaultWorkingDirectory)
    DestDir: $(DestinationFolder)
    WindowsServer: $(WindowsServer)

Happy Azure Pipelining!!

About Wes MacDonald

Wes MacDonald is a DevOps Consultant for LIKE 10 INC., a DevOps consulting firm providing premium support, guidance and services for Azure, Microsoft 365 and Azure DevOps.

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: