0

I am using below code to execute a batch file through 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);
            }

This code works fine if I run console application from same machine with different user. But it fails to execute batch file, if I call it from remote machine using powershell with delegation on. Below is the powershell command to call exe from remote machine.

$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

Note: Please note that delegation is setup between machines.

Please advice. Thanks

0

1 Answer 1

1

Have you tried to ask the cmd run with "/c"?

processInfo = new ProcessStartInfo("cmd.exe", "/c exec.bat");
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Marcius, thanks for quick answer. But i have already tried this option and its not working.
did you supply the full path name to exec.bat ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.