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