Exec Task in MSBuild for execution of command on remote machine - msbuild

I am using following command to install a service via MSBuild file. This works great
<Exec Command= 'c:\test\myService.Appservices.exe install' ContinueOnError='false' />
But the above command install the service on local machine. I want to install the service on a remote machine. How can I specify the machine name using this command?

As per Mike Vine's comment, MSBuild doesn't include tools for remote execution. You could however use something like psexec. e.g.
<Exec Command='psexec -accepteula -s \\RemoteServer "C:\Path To EXE on Remote Machine\my.EXE"' IgnoreExitCode="false" ContinueOnError="false" Timeout="600000" >
<Output TaskParameter="ExitCode" PropertyName="exitCode1"/>
</Exec>

Related

Running sqlplus inside cmd.exe using sshexec

I have cygwin installed on a remote windows server running oracle. I am trying to run sqlplus through an ANT file in Jenkins. I use sshexec to run the command below, but it doesn't work.
I am able to make it work when I login into SSH then first type in 'cmd' to switch to the windows command prompt, then entering the sqlplus statement below is successfull.
How can I replicate the above using scripts by running the sqlplus command below in cmd.exe or run cmd.exe first then execute the sqlplus command so it works like when I perform it directly in SSH.
<target name="compile.data">
<sshexec host="${ssh.hostname}"
verbose="true"
trust="true"
username="${ssh.username}"
password="${ssh.username}"
command="sqlplus ${db.username}/${db.password}#${db.hostname}:${db.port}:${db.sid} #${script.path}testscript.sql"/>
</target>
Thanks.
I have found the answer after to my question for refernce:
<sshexec host="${ssh.hostname}"
verbose="true"
trust="true"
username="${ssh.username}"
password="${ssh.username}"
command='cmd /C "sqlplus ${db.username}/${db.password}#${db.hostname}:${db.port}:${db.sid} #${script.path}testscript.sql"'/>

SSH into server, sudo later with Phing

We have a server that is set up so you can't log in with root directly. You first log in with a user, then run su and enter the password.
I need to SSH into a server, using phing, and sudo then run a command. I thought if I can get it working just via ssh, I can use that command in an exec task in phing, but can't even get the plain SSH right.
Is this possible?
I've tried the following:
ssh user#server 'su && cd /var/www/clients'
ssh user#server 'su && {{password}} && cd /var/www/clients'
You can use the SshTask and how-to-pass-the-password-to-su-sudo-ssh-without-overriding-the-tty.
<project name="ssh-with-later-sudo" default="run-cmd" basedir=".">
<target name="run-cmd">
<ssh username="user" password="password" host="server" command="echo password | sudo -S cd /var/www/clients" />
</target>
</project>

dotnet publish -o ./dist does not set $OutDir or $OutPath in msbuild

I Have a file move event which I want to trigger after a publish
<Target Name="CopyEmailTemplates" AfterTargets="AfterPublish">
<ItemGroup>
<TemplatesFolder Include="Views\EmailTemplates\*.cshtml" />
</ItemGroup>
<Copy SourceFiles="#(TemplatesFolder)" DestinationFolder="$(OutDir)Views\EmailTemplates\" />
</Target>
I've confirmed that the command does not return the publish directory with this target:
<Target Name="OutputTest" AfterTargets="AfterPublish">
<Exec Command="echo OutPath: $(OutputPath)" />
<Exec Command="echo OutDir: $(OutDir)" />
</Target>
Expected:
OutDir is set to dist/
Actual behavior:
OutDir is set to bin/Release/netcoreapp2.0/
I am using: .NET Command Line Tools (2.1.4) on osx.10.12-x64
Publish is a two-step process. The project is built using normal build settings and then published to $(PublishDir). Use this property wherever you need to know the path of the publish output.
Self answering in hopes to prevent future headaches for people.
The dotnet publish -o ./dist command will set the $(PublishDir) variable in msbuild.
dotnet build -o ./dist does however set $(OutDir)
To be more explicit with our build I now use the msbuild command
dotnet publish -o ./dist -c Release
Becomes:
dotnet msbuild /t:publish /p:PublishDir=dist/ /p:Configuration=Release

Execute npm with ant

I want to automate the build of an application with frontend and backend. To do this I want to use maven with ant for platform independent copy and cli tasks. With some cli's like docker ... this works. But this does not work for cli's provided by npm or npm itself.
<exec executable="docker">
<arg line="version"/>
</exec>
<!--Works-->
<exec executable="C:\Program Files\nodejs\npm.cmd">
<arg line="version"/>
</exec>
<!--Doesn't work-->
<exec executable="npm">
<arg line="version"/>
</exec>
As seen in the second example, the script works if I specify the full path to the npm.cmd. But this should work at least on windows and unix. So specifying the full path is not an option.
Is there any way to run npm and its modules from ant?
Late edit:
The real problem was, that the windows node installer also placed a file named npm into the bin folder which is a bash script meant for cygwin. The npm bin folder was added to the 'global' PATH env var and the windows cmd did pick up the correct binary because it uses the PATHEXT env var to determine what is executable and what not. The ant exec plugin does not use PATHEXT and just executes the first file named npm which fails. The Solution was to rename the plain npm file in the path. This way ant sees the npm.cmd file first and everything runs smoothly.
I know this is old, but this is what I have, for others in the future. Works in our mac, unix, and windows boxes.
<macrodef name="exec-node">
<attribute name="module" description="The name of the NodeJS module to execute" />
<attribute name="failonerror" default="true" description="Fail if the exit code is not 0" />
<attribute name="dir" description="Directory to execute task" />
<element name="args" implicit="yes" description="Argument to pass to the exec task" />
<sequential>
<exec executable="cmd.exe" dir="#{dir}" failonerror="#{failonerror}" osfamily="winnt">
<arg line="/c #{module}" />
<args />
</exec>
<exec executable="#{module}" dir="#{dir}" failonerror="#{failonerror}" osfamily="unix" logError="true">
<args />
</exec>
</sequential>
</macrodef>
Then things like this can work
<exec-node dir="${node.project.dir}" module="npm" failonerror="true" >
<arg value="run" />
<arg value="lint" />
</exec-node>
You can of course hardcode module as npm, or default it, but I use this with npm and npx.
From what I can gather, its not possible to call npm directly through the antrun plugin.
I did manage to get it tu run by calling cmd (on windows) with the /c argument.
Example:
<exec executable="cmd">
<arg line="/c npm run babel -- src/main/webapp/js/es6/ --presets babel-preset-es2015 --out-dir src/main/webapp/js/"/>
</exec>
This works for me on Windows:
<exec executable="npm.cmd">
<arg value="version"/>
</exec>
If you like to use npm you should take a look at the frontend-maven-plugin.

Running commands on Remote machine using PsExec from Msbuild invoked via Jenkins

I am using psexec(with MSbuild Script) to run an xCopy on a server from Jenkins(Hudson). The command runs ok when run through Command Prompt(as System user), but gives the following error when tried using Jenkins. This is the command I am using from Msbuild script:
psexec \\<RemoteMachine> /accepteula -i -u <Domian\User> -p <Pass> -s cmd /c xcopy <Path1> <Path2> /e /i
PsExec v1.98 - Execute processes remotely
Copyright (C) 2001-2010 Mark Russinovich
Sysinternals - www.sysinternals.com
The handle is invalid.
Connecting to ..
Couldn't access :
Connecting to ...
**error MSB3073: The command "psexec \\<RemoteMachine> /accepteula -i -u <Domian\User> -p <Pass> -s cmd /c xcopy <Path1> <Path2> /e /i " exited with code 6.**
I have tried too many options but no luck so far.
Does anyone tried the same using Jenkins? Help will be really appreciated.
Are there any other ways to achieve this using some other tools without any problem. Please share.
Just wanted to post the solution here, as I figured it after some time.
The Remote Machine should have this user added in the Administrators Group.
This will resolve the connection issue. :)