Process.Start() 没有通过 c# 代码执行批处理文件
本文关键字:代码 执行 批处理文件 Start Process | 更新日期: 2023-09-27 18:36:48
我使用以下代码通过 c# 控制台应用程序执行批处理文件
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
注意:请注意,委派是在计算机之间设置的。
请指教。谢谢
您是否尝试过使用"/c"询问cmd运行?
processInfo = new ProcessStartInfo("cmd.exe", "/c exec.bat");