Invoke PowerShell remote command with parameters with spaces via TeamCity
One of the coolest things to do with TeamCity is to run some external applications via PowerShell. This will allow us to invoke remote commands without having to install an additional agent on the remote target and it will allow us to centralise those commands from our build agent environment.
The idea behind it is as follows:
I have a centralised Build Agent environment and I need to run a command line application en two additional machines. I don't want to install any TeamCity agent on those machines as these are just deployment machines and should be independent of the bulding process. I just need to remote deploy some binaries there via Powershell and then execute the application.
These two additional machines are in the same network and WinRM is configured in every instance with the correct permissions so TeamCity can run the commands without problems.
There are loads of guides over the internet regarding WinRM configuration. Here is the screenshot from my local configuration on my Win10 machine. I run the same on the TeamCity agent, a Win2012 machine:
I had to tweak first with the wifi connection as it was set to public by default and it needs to be private. Then just enable PSRemoting and add the trusteshosts as all (*). Then to test it out, run the Test-WSMan myRemoteMachine and you should see something similar to the image above.
In TeamCity, there are two additional steps, one to copy some binaries to a remote machine and the second one to run the binaries remotely. Both steps running powershell:
Here is the command for each step:
And the remote execution from Powershell:
In TeamCity, there are two additional steps, one to copy some binaries to a remote machine and the second one to run the binaries remotely. Both steps running powershell:
Here is the command for each step:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#The MIT License (MIT) | |
#Copyright (c) 2015 Jordi Corbilla | |
#Permission is hereby granted, free of charge, to any person obtaining a copy | |
#of this software and associated documentation files (the "Software"), to deal | |
#in the Software without restriction, including without limitation the rights | |
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
#copies of the Software, and to permit persons to whom the Software is | |
#furnished to do so, subject to the following conditions: | |
#The above copyright notice and this permission notice shall be included in | |
#all copies or substantial portions of the Software. | |
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
#THE SOFTWARE. | |
#Copy files from Build agent to a remote machine | |
Copy-Item -Path C:\build\MapReduce\* -Destination \\MACHINE1\c$\remote\MapReduce\ -Recurse -force | |
#Run command in second machine via typed credentials | |
#Notice that the command line argument doesn't contain any quotes | |
$pw = convertto-securestring -AsPlainText -Force -String yourpassword | |
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "domain\user", $pw | |
Invoke-Command -ComputerName MACHINE1 -ScriptBlock { C:\remote\MapReduce\MapReduce.exe 'C:\remote\MapReduce\test.txt' } -credential $cred | |
#If the path contains spaces, then you need to use call operator & with the double quotes | |
Invoke-Command -ComputerName MACHINE1 -ScriptBlock { &"C:\remote\Map Reduce\MapReduce.exe" 'C:\remote\Map Reduce\test.txt' } -credential $cred |
And the remote execution from Powershell:
Comments
Post a Comment