Reputation: 3491
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>
Upvotes: 2
Views: 2818
Reputation: 473
I had the same problem. But I did not want to enter password each time I deploy app using RunAs
.
Instead, I use Copy
task wrapped by net use
via Exec
:
<Target Name="CopyOutput">
<ItemGroup>
<PackagedFiles Include="$(ProjectName)\obj\$(Configuration)\Package\PackageTmp\**\*.*"/>
</ItemGroup>
<Exec Command="net use $(DestPath) /user:$(DestLogin) $(DestPass)" ContinueOnError="false"/>
<Copy SourceFiles="@(PackagedFiles)"
DestinationFiles="@(PackagedFiles->'$(DestPath)\%(RecursiveDir)%(Filename)%(Extension)')"
ContinueOnError="true"/>
<Exec Command="net use $(DestPath) /delete" />
</Target>
DestPath, DestLogin and DestPass I set through environment variables.
Thanks to Phil's answer and this one.
Upvotes: 5
Reputation: 3491
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"
Upvotes: 1
Reputation: 4286
I would reccoment to create special account, let's say builder, and give read/write access for that account share on remote computer.
Or you can call script on computer start up to open session. See net use /?
Upvotes: 0