码迷,mamicode.com
首页 > 其他好文 > 详细

wmi远程启动exe程序

时间:2018-08-23 19:16:58      阅读:436      评论:0      收藏:0      [点我收藏+]

标签:object   命令行   注意   data-   安全性   执行   blog   public   star   

版权声明:join share https://blog.csdn.net/qq_16072507/article/details/54603954

 
                                   一、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

wmi远程启动exe程序

标签:object   命令行   注意   data-   安全性   执行   blog   public   star   

原文地址:https://www.cnblogs.com/hu16683845/p/9525141.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!