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:

Comments

Popular Posts