我使用下面的代码通过c#console application
执行批处理文件ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "Batch File Path";
psi.WorkingDirectory = "Folder path where batch file is located";
psi.UseShellExecute = false;
//User under which batch file has to be executed
using (new Impersonator("UserName", "DomainName", "Password"))
{
Process.Start(psi);
}
如果我从不同用户的同一台机器运行控制台应用程序,此代码可以正常工作。 但它无法执行批处理文件,如果我使用带有委托的powershell从远程机器调用它。 下面是从远程机器调用exe的powershell命令。
$pw = convertto-securestring -AsPlainText -Force -String Password
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "UserName",$pw
$opt = new-pssessionoption -OperationTimeOut 7200000 -OutputBufferingMode Drop
$sessions = New-PSSession -ComputerName ServerName -sessionOption $opt -credential $cred -Authentication CredSSP
Invoke-Command -session $sessions -ScriptBlock {"Path of exe on remote machine" | out-null}
Remove-PSSession -ComputerName ServerName
注意:请注意,委派是在计算机之间设置的。
请指教。 感谢
答案 0 :(得分:1)
您是否尝试过使用“/ c”运行cmd?
processInfo = new ProcessStartInfo("cmd.exe", "/c exec.bat");