使用" runas"运行cmd命令使用C#中的凭据

时间:2014-09-25 10:15:14

标签: c# command-line remote-access

类似的问题在SO上被问到至少十二次,看起来现在我已经用尽了大部分建议的解决方案,但仍然无法成功完成任务。

所以我拥有以下命令,我想在cmd中运行:

xcopy /q C:\fileName.txt \\VMNAME\C$\destFolder /Y /E

但是我需要用某些凭据来执行它。所以我手动做的是先输入以下命令:

runas /user:<domainName>\<userName> cmd

那是打开一个单独的cmd窗口,我在该窗口中运行了第一个(xcopy)命令。

我现在所拥有的:

string strCmdText = string.Format(@"xcopy /q {0} {1} /Y /E", source, destination);

ProcessStartInfo procStartInfo = new ProcessStartInfo();

procStartInfo.RedirectStandardError = true;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.FileName = "runas";
procStartInfo.Arguments = String.Format(@"/user:<domainName>\<userName> cmd " + strCmdText);

Process.Start(procStartInfo);

sourcedestination属于以下结构:

 source = "C:\\somePath\\fileName.txt"
 destination = "\\\\<VMName>\\C$\\somePath\\"

我还尝试使用以下内容定义procStartInfo

procStartInfo.Verb = "runas";

而不是:

procStartInfo.FileName = "runas";

有类似的结果。

目前,当我运行上面的代码时,它不会返回任何错误,但也不会执行预期的操作。我错过了什么或这种方法是错的吗?

0 个答案:

没有答案