wmi遠程啓動exe程序


                                   一、vbs:WMI遠程控制機器時報0x80070005拒絕訪問錯誤的解決方法
昨天晚上學習一個WMI遠程連接機器的方法,可是始終報錯,錯誤代碼爲0x80070005;拒絕訪問,今天上午看到別人的博客裏寫到遠程連接機器不成功的方法,試了一下果然OK了,以免以後忘掉,所以記在這裏;
wmic /node:[機器IP] /user:[管理員賬戶名] /password:[密碼]  process where (name='qq.exe') call terminate輸入後報錯,錯誤代碼爲:0x80070005
解決方法:
1.開始---運行DCOMCNFG在"組件服務"對話框中,依次展開"組件服務","計算機""我的電腦",在"我的電腦"右鍵屬性對話框中單擊"COM安全"選項卡,在"啓動和激活權限"下,單擊"編輯限制",在"啓動權限"對話框中,將你要訪問的用戶或組添加到"組或用戶名稱"列表中。在"啓動權限"對話框中,在"組或用戶名稱"框架內選擇您的用戶和組。在"用戶權限"下的"允許"欄中,選擇"遠程啓動",然後單擊“確定”
2.開始---運行,到達dos命令行狀態,輸入netsh firewall set Service RemoteAdmin enable(disable爲不可用)
輸入完以後會提示“確定”代表執行動作生效
3.打開“控制面板”---“管理工具”,點擊“本地安全策略”,選擇“本地策略”---“安全選項”,選擇中間的“網絡訪問:本地賬戶的共享和安全模式”把它的值設爲“經典--本地用戶以自己的身份驗證”,然後再回到dos命令行下,輸入以下命令:
wmic /node:[機器IP] /user:[管理員賬戶名] /password:[密碼]  process where (name='qq.exe') call terminate
以上這句話可以把遠程控制的機器上的qq進行關閉
其中name='qq.exe'也可以寫成別的進程名,看自己有什麼樣的需求; 如:name='winword'等等

二、0x80070003 無權限

問題 9:如何設置 WMI 命名空間的安全性?

使用 WMI 控件設置命名空間的安全性

WMI 控件提供了一種管理命名空間安全性的方法。可以在命令提示行運行以下命令來啓動 WMI 控件:

wmimgmt

在安裝了 WMI 的 Windows 9x 或 Windows NT4 計算機上,輸入以下命令:

wbemcntl.exe

或者您也可以通過以下方式訪問 WMI Control 和“安全性”選項卡:

1. 右鍵單擊“我的電腦”,然後單擊 管理。

2.雙擊 服務和應用程序 ,然後雙擊 WMI 控件。

3. 右鍵單擊 WMI 控件 ,然後單擊 屬性。

4. 在 WMI 控件屬性 對話框中單擊 安全 選項卡。

5. 一個名爲 Root ,前面帶加號 (+) 的文件夾將會出現。如果必要,展開這個樹狀結構,定位到想要設置權限的命名空間。

6. 單擊 安全設置 按鈕。一組用戶和權限顯示出來。如果用戶在這個列表中,請按照需要修改權限。如果用戶不再這個列表中,請單擊 添加 按鈕,然後從賬戶所在的位置(本地計算機、域等等)添加用戶。

注意:

• 爲了查看和設置 namespace 安全性,用戶必需擁有 讀取安全設置 和 編輯安全設置 權限。系統管理員默認具備這些權限,並可以按照需要將權限賦予其他用戶。

• 如果一個用戶需要遠程訪問命名空間,必須爲其選中 遠程啓用 權限。

• 默認情況下,針對一個命名空間設置的用戶權限只對該命名空間有效。如果希望用戶可以訪問該命名空間和其下所有子命名空間,或者只能訪問子命名空間,請單擊 高級 按鈕。單擊 編輯 並在出現的對話框中指定允許訪問的範圍。

三、創建任務:
     打開“控制面板”---“管理工具”,任務計劃程序    創建基本任務,觸發爲一次性任務,操作爲啓動程序

四、寫代碼前,需要在解決方案下的工程中References右鍵添加引用System.Management,System.Management.Instrumentation
 public static void StartGame() {
            Console.WriteLine("remote start game");
            //StartRemoteExe("192.168.1.3","UPC","imr360","xfs");
            StartRemoteExe2("192.168.1.3", "UPC", "imr360", "xfs");
            //ShowWinInfo();
        }
private static void StartRemoteExe2(string ip, string username, string password, string taskname)
        {
            ManagementClass classInstance = new ManagementClass();
            ManagementBaseObject inParams = classInstance.GetMethodParameters("Create");
            inParams["CommandLine"] = "schtasks /run /s \"" + ip + "\" /u \"" + username + "\" /p \"" + password+"\" /tn \"" + taskname + "\"";
            classInstance.InvokeMethod("Create",inParams,new InvokeMethodOptions(null, System.TimeSpan.MaxValue))
        }
        private static void StartRemoteExe(string ip, string username, string password, string taskname) {
            ConnectionOptions connOption = new ConnectionOptions();
            connOption.Username = username;
            connOption.Password = password;
            ManagementPath mngPath = new ManagementPath(@"\\" + ip + @"\root\cimv2:Win32_Process");
            ManagementScope scope = new ManagementScope(mngPath, connOption);
            scope.Connect();
            ManagementClass classInstance = new ManagementClass(scope, mngPath, new ObjectGetOptions());
            ManagementBaseObject inParams = classInstance.GetMethodParameters("Create");
            inParams["CommandLine"] = "schtasks /run /tn \""+taskname+"\"";
            ManagementBaseObject outParams = classInstance.InvokeMethod("Create", inParams, new InvokeMethodOptions(null, System.TimeSpan.MaxValue));
            Console.WriteLine("Creation of calculator process returned: " + outParams["returnValue"]);
            Console.WriteLine("Process ID: " + outParams["processId"]);
        }
        /// <summary>
        /// Start run the exe at the remote computor
        /// </summary>
        private static void StartRemoteExe() {
            string name = "UPC";
            string password = "imr360";
            ConnectionOptions connOption = new ConnectionOptions();
            connOption.Username = name;
            connOption.Password = password;
            ManagementPath mngPath = new ManagementPath(@"\\" + "192.168.1.3" + @"\root\cimv2:Win32_Process");//\\192.168.1.3\root\cimv2:Win32_Process
            Console.WriteLine(ManagementPath.DefaultPath);
            ManagementScope scope = new ManagementScope(mngPath, connOption);
            scope.Connect();
            ObjectGetOptions objOption = new ObjectGetOptions();
            ManagementClass classInstance = new ManagementClass(scope, mngPath, objOption);
            MethodDataCollection.MethodDataEnumerator enumera=classInstance.Methods.GetEnumerator();
            Console.WriteLine("methods out="+classInstance.Methods.Count);
            while (enumera.MoveNext())
            {
                Console.WriteLine("method name :" + enumera.Current.Name);
            }
            ManagementBaseObject inParams = classInstance.GetMethodParameters("Create");
            // Fill in input parameter values
            //inParams["CommandLine"] = @"D:\Program\UserAnts20120530\UserAnts20120530\UserAnts3\TaskWorker.exe";//只能啓動進程
            //inParams["CommandLine"] = @"C:\share\xuefengshan\xuefengshan.exe";//只能啓動進程
            inParams["CommandLine"] = "schtasks /run /tn \"xfs\""; //其中Start03是任務計劃的名稱,需要建立啓動exe的計劃任務
            // Method Options
            InvokeMethodOptions methodOptions = new InvokeMethodOptions(null, System.TimeSpan.MaxValue);
            // Execute the method
            ManagementBaseObject outParams = classInstance.InvokeMethod("Create", inParams, methodOptions);
            Console.WriteLine("Creation of calculator process returned: " + outParams["returnValue"]);
            Console.WriteLine("Process ID: " + outParams["processId"]);
        }
命令記錄:
wmic /TRACE:ON /node:192.168.1.3 /user:UPC /password:imr360 process call create cmd.exe



發佈了60 篇原創文章 · 獲贊 31 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章