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