c#run命令在Windows 2008 64bit远程

时间:2012-05-09 20:11:42

标签: windows 64-bit command

我是C#编码的新手,请跟我帮忙,谢谢你提前.. !!!

我正在尝试关注:

我正在开发小型C#app以在远程服务器上执行批处理文件,以下是我的代码,我的大多数服务器都是Windows 2008 64位,如果我进入服务器RDP,我可以执行批处理文件而没有任何错误,但是当我尝试通过以下代码来完成它,它不起作用,没有抛出异常,但没有结果。

我的批处理文件包含以下命令:

@ECHO off 
echo Running Remote Commands 
date/t 
time /t 
COPY "\\xt0022\I$\abc\RemoteProcess\testcopy.bat" D:\ab\
date/t 
time /t 

-

try
{
    string remotemachine = "Server1";

    object[] theProcessToRun = { "D:\\ab\\test2.bat" };
    ConnectionOptions theConnection = new ConnectionOptions();
    theConnection.Impersonation = ImpersonationLevel.Impersonate;
    theConnection.EnablePrivileges = true;
    ManagementScope theScope = new ManagementScope("\\\\" + remoteMachine + "\\root\\cimv2", theConnection);
    ManagementClass theClass = new ManagementClass(theScope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
    theClass.InvokeMethod("Create", theProcessToRun);

}
catch (Exception ex)
{

}

如果我调试代码,它会显示“Derivation = Function evaluation out out out。”

我是否必须使用RUNAS进行此操作..?如果是..任何人都可以帮助我使用这些代码或方法..?

谢谢大家..!

1 个答案:

答案 0 :(得分:1)

您是否查看了ProcessStartInfo?

ProcessStartInfo startInfo = new ProcessStartInfo("PsExec.exe");
startInfo.Arguments = @"\\<computername -u <username> -p <password> -i c:\\test.bat";
Process.Start(startInfo);

这是CodeProject.com上的一个artice,它使用WMI创建一个远程进程,就像你做的那样。完整的来源可用。

http://www.codeproject.com/Articles/31113/Create-a-Remote-Process-using-WMI-in-C

这可能是一个愚蠢的问题,但你的超时设置是什么?如果它太小,那可能是原因。

相关问题