Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I copy files as a different user from msbuild?

Tags:

c#

.net

msbuild

I am trying to copy files to a remote server, the account I am running my build server doesn't have permissions though. How can I do it using different credentials?

like image 600
Grzenio Avatar asked Dec 05 '22 05:12

Grzenio


2 Answers

It's best to create a quick extension of CallTarget task that uses Impersonator by Uwe Keim, like this:

public class Impersonate : CallTarget
{
    public string Domain { get; set; }

    [Required]
    public string UserName { get; set; }

    [Required]
    public string Password { get; set; }

    public override bool Execute()
    {
        using (new Impersonator(this.UserName, this.Domain, this.Password))
        {
            return base.Execute();
        }
    }
}

Then call will look like this:

<Target Name="DoSms">
    <....>
</Target>

<Target Name="Impersonate">
    <Impersonate Targets="DoSms" UserName="username" Password="password" Domain="domain"/>
</Target>
like image 125
Andriy K Avatar answered Dec 07 '22 17:12

Andriy K


Try Exec task to execute RunAs.exe which would run the xcopy.exe

like image 26
Stop Putin Stop War Avatar answered Dec 07 '22 18:12

Stop Putin Stop War