Search code examples
msbuildmsbuild-task

How do I copy files to a network share that requires a password using MSBuild?


I've created an MSBuild.xml file to automate our ASP.NET project builds. It builds the project, publishes it, zips it and copies the zip to a network share. This all works perfectly, provided that I have already logged in to the network folder previously. After I have accessed the network share the username and password are remembered for until I log out of my machine. If I haven't logged in previously I get a "failure: unknown user name or bad password" error when I run the build file. I would like the build to work all the time regardless of whether I have previously accessed the network share.

Currently I'm using the Copy task to copy the zip file to the network share. I've checked the Copy task documentation and I can't see any way to include credentials. So, how can I copy files to a network share passing the required username and password?

Here is an example of the Copy task I'm using. All the properties are defined at the top of the MSBuild.xml file:

<Target Name="CopyToServer">
    <Copy SourceFiles="$(ReleaseFolder)\$(ZipFileName).zip" DestinationFolder="$(WebServerRoot)" />
</Target>

Solution

  • I've found one solution. I'm not entirely happy with it, but it'll do until I find a better solution.

    If you run the MSBuild command from a batch file it's possible to use the runas command to run MSBuild as the user that has the required permissions to access the network share.

    E.g.

    runas /user:someWindowsUser "C:\PathToBatchFile"