开发者

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 networ开发者_Python百科k 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>


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.


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"


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 /?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜