Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I hide the command I'm using in an MSBuild Exec task from console output?

I've got a task in my MSBuild file like so:

<Exec command="net use $(DeploymentServerName) /user:username passwd" ContinueOnError="false" />

But in the console output it will dump the command:

...
net use $(DeploymentServerName) /user:username passwd
...

But I'd like to hide the credentials, if possible. I don't care about where the output of the command goes, I just care that the command itself is not echo'd to the console. Any ideas?

like image 899
peterw Avatar asked Aug 07 '14 18:08

peterw


2 Answers

Starting with .NET 4.0 Exec MSBuild task got property EchoOFF which allows to achieve exactly that - suppress echoing of the command itself (not the command output). Full documentation is here. Just add EchoOff="true" to the list of Exec properties.

like image 84
Konstantin Erman Avatar answered Oct 08 '22 18:10

Konstantin Erman


There are a couple of possible approaches, here is one

<Target Name="DoHideCommand">
  <Exec Command="MSBuild $(MsBuildThisFile) /t:SpecialCommand /nologo /noconsolelogger"/>
</Target>

<PropertyGroup>
  <MyCommand>dir c:</MyCommand>
</PropertyGroup>  

<Target Name="SpecialCommand">
  <Exec Command="dir"/>
</Target>

This invokes a seperate msbuild process to invoke the actual target, and hides all output resulting in

...
DoHideCommand:
  MSBuild test.targets /t:SpecialCommand /nologo /noconsolelogger
...

And here is another one

<Target Name="SpecialCommandViaFile">
  <PropertyGroup>
    <TmpFile>tmp.bat</TmpFile>
  </PropertyGroup>
  <WriteLinesToFile File="$(TmpFile)" Lines="$(MyCommand)"/>
  <Exec Command="$(TmpFile) > NUL 2>&amp;1" WorkingDirectory="$(MsBuildThisFileDirectory)"/>
  <Delete Files="$(TmpFile)"/>
</Target> 

This creates a batch file to run the actual command, then redirects all output to NUL so only this is shown:

...
SpecialCommandViaFile:
tmp.bat > NUL 2>&1
Deleting file "tmp.bat".
...

Note though that the one executing your msbuild file can always simply open your file to look at the credentials, even if they are hidden from the output when running it.

like image 40
stijn Avatar answered Oct 08 '22 20:10

stijn