簡體   English   中英

C#WMI遠程程序無法正確執行

[英]C# WMI remote program doesn't execute correctly

我正在嘗試使用WMI在遠程服務器上執行程序。

當我執行我的代碼時,我可以看到可執行文件出現在遠程服務器上的任務管理器的“進程”選項卡中,但它實際上從未執行過它的功能。 正確運行時,特定文件夾中應該有一些文件,但是當我從程序中運行它時沒有。

另外,我檢查從WMI報告的結果,我得到一個0的代碼,我相信這表明沒有錯誤。

我已經發布了以下代碼。 如果我的代碼中的某些內容沒有明顯錯誤,那么我將非常感謝有關如何解決此問題的建議,因為我是WMI流程的新手。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.IO;
using System.Configuration;
using System.Diagnostics;
using System.Management;

namespace ExecuteNetworkResources
{
    class Program
    {
        static void Main(string[] args)
        {
            var processToRun = new[] { "C:\\Program Files\\TestApplication\\mscombine.exe -c -cwa" };
            var connection = new ConnectionOptions();
            connection.Username = "domain\\user1;
            connection.Password = "password";
            var wmiScope = new ManagementScope(String.Format(@"\\{0}\root\cimv2", "Server1"), connection);
            var wmiProcess = new ManagementClass(wmiScope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
            object result = wmiProcess.InvokeMethod("Create", processToRun);
            Console.WriteLine("Returned: " + result);
            Console.ReadLine();

        }
    }
}

您可以在遠程計算機的任務管理器中看到進程意味着WMI執行成功。 現在,未創建文件的問題是因為遠程進程create方法在會話0中運行進程。 這是一個非交互式桌面會話。

這可能會限制進程訪問文件系統的訪問/權限。 這就是為什么你沒有收到任何錯誤,但是過程中的操作被忽略了。 您需要在連接選項中明確指定它以啟用previleges:

        ConnectionOptions connection = new ConnectionOptions() {
            EnablePrivileges = true
        };

如果用於連接的用戶具有足夠的權限,這將提供對文件系統的訪問。

我正在回答這個問題,所以我可以將它標記為MethodMan的答案。 他是對的,我們的服務器團隊表示已關閉。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM