0

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

我正在尝试以下操作:

我正在开发小型 C# 应用程序以在远程服务器上执行批处理文件,以下是我的代码,我的大多数服务器都是 windows 2008 64bit,如果我 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)
{

}

如果我调试代码,它会显示“推导 = 函数评估超时”。

我必须用 RUNAS 运行它吗?如果是..任何人都可以帮助我使用这些代码或方法..?

谢谢你们..!

4

1 回答 1

1

Have you looked into ProcessStartInfo?

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

Here's an artice on CodeProject.com that creates a remote process using WMI, like your doing. Complete source is available.

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

This might be a silly question, but what is your timeout set to? If it's too small, that may be the cause.

于 2012-05-09T20:20:00.420 回答