【问题标题】:c# run command on windows 2008 64bit remotelyc#在windows 2008 64bit远程运行命令
【发布时间】:2012-05-09 20:11:41
【问题描述】:

我是 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 .. 运行它吗?如果是.. 谁能帮我解决这些代码或方法..?

谢谢大家..!

【问题讨论】:

    标签: windows 64-bit command


    【解决方案1】:

    您查看过 ProcessStartInfo 吗?

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

    这是 CodeProject.com 上的一篇文章,它使用 WMI 创建远程进程,就像您所做的那样。完整的源代码可用。

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

    这可能是一个愚蠢的问题,但是您的超时设置为多少?如果它太小,这可能是原因。

    【讨论】:

    • 是的,我已经尝试过相同的代码,这是查看此问题的另一种方法。如果我执行其中包含本地路径的批处理文件,它工作正常,但如果我有远程文件位置供它复制,它会失败:-(,也没有设置 TimeOut,我使用的是默认值。做我必须明确设置更多的超时长度..?
    • 我的意思是,如果我在批处理文件中执行此操作,它可以正常工作:COPY "C:\temp\abc.txt" D:\ab\
    • 所以从远程位置获取文件超时。抱歉,我没有使用远程命令遇到这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-11
    • 2020-08-30
    • 2012-11-17
    • 2020-09-03
    • 2014-10-07
    • 1970-01-01
    • 2014-01-07
    相关资源
    最近更新 更多