6

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?

peterw
  • 1,332
  • 2
  • 12
  • 23

2 Answers2

17

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.

Konstantin Erman
  • 551
  • 6
  • 14
1

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.

stijn
  • 34,664
  • 13
  • 111
  • 163
  • @IlyaKozhevnikov without a bat, it will only suppress command *output*, not the command itself. The `>` etc are optional, `>` works just as fine. – stijn Aug 08 '14 at 09:17
  • I used the top idea, by executing another target. I made the target name something informative so I will still know in the output what step is happening, but just not the actual details. Thanks! – peterw Aug 08 '14 at 16:47