本文整理汇总了C#中System.Management.ManagementClass.InvokeMethod方法的典型用法代码示例。如果您正苦于以下问题:C# ManagementClass.InvokeMethod方法的具体用法?C# ManagementClass.InvokeMethod怎么用?C# ManagementClass.InvokeMethod使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Management.ManagementClass
的用法示例。
在下文中一共展示了ManagementClass.InvokeMethod方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Run
//private char NULL_VALUE = char(0);
public static ProcessReturnCode Run(string machineName, string commandLine, string args, string currentDirectory)
{
var connOptions = new ConnectionOptions
{
EnablePrivileges = true
};
var scope = new ManagementScope(@"\\{0}\root\cimv2".FormatWith(machineName), connOptions);
scope.Connect();
var managementPath = new ManagementPath(CLASSNAME);
using (var processClass = new ManagementClass(scope, managementPath, new ObjectGetOptions()))
{
var inParams = processClass.GetMethodParameters("Create");
commandLine = System.IO.Path.Combine(currentDirectory, commandLine);
inParams["CommandLine"] = "{0} {1}".FormatWith(commandLine, args);
var outParams = processClass.InvokeMethod("Create", inParams, null);
var rtn = Convert.ToUInt32(outParams["returnValue"]);
var pid = Convert.ToUInt32(outParams["processId"]);
if (pid != 0)
{
WaitForPidToDie(machineName, pid);
}
return (ProcessReturnCode)rtn;
}
}
示例2: Bob
public void Bob()
{
var connOptions = new ConnectionOptions
{
EnablePrivileges = true,
//Username = "username",
//Password = "password"
};
string server = "SrvTestWeb01";
ManagementScope manScope = new ManagementScope(String.Format(@"\\{0}\ROOT\CIMV2", server), connOptions);
manScope.Connect();
var managementPath = new ManagementPath("Win32_Process");
var processClass = new ManagementClass(manScope, managementPath, new ObjectGetOptions());
var inParams = processClass.GetMethodParameters("Create");
inParams["CommandLine"] = @"C:\Temp\dropkick.remote\dropkick.remote.exe create_queue msmq://localhost/dk_test";
var outParams = processClass.InvokeMethod("Create", inParams, null);
var rtn = System.Convert.ToUInt32(outParams["returnValue"]);
var processID = System.Convert.ToUInt32(outParams["processId"]);
}
示例3: AddARecord
public void AddARecord(string hostName, string zone, string iPAddress, string dnsServerName)
{
/*ConnectionOptions connOptions = new ConnectionOptions();
connOptions.Impersonation = ImpersonationLevel.Impersonate;
connOptions.EnablePrivileges = true;
connOptions.Username = "nwtraders\administrator";
connOptions.Password = "Password01";
*/
ManagementScope scope =
new ManagementScope(@"\\" + dnsServerName + "\\root\\MicrosoftDNS"); //,connOptions);
scope.Connect();
ManagementClass wmiClass =
new ManagementClass(scope,
new ManagementPath("MicrosoftDNS_AType"),
null);
ManagementBaseObject inParams =
wmiClass.GetMethodParameters("CreateInstanceFromPropertyData");
inParams["DnsServerName"] = dnsServerName;
inParams["ContainerName"] = zone;
inParams["OwnerName"] = hostName + "." + zone;
inParams["IPAddress"] = iPAddress;
wmiClass.InvokeMethod("CreateInstanceFromPropertyData", inParams, null);
}
示例4: CreateShare
/// <summary>
/// Creates the share.
/// </summary>
/// <param name="shareName">Name of the share.</param>
/// <param name="folderPath">The folder path.</param>
/// <returns>WindwsShare instance.</returns>
public static WindowsShare CreateShare(string shareName, string folderPath)
{
ManagementClass shareClass = null;
ManagementClass sd = null;
ManagementBaseObject inParams = null;
ManagementBaseObject outParams = null;
try
{
sd = new ManagementClass(new ManagementPath("Win32_SecurityDescriptor"), null);
sd["ControlFlags"] = 0x4;
sd["DACL"] = new ManagementBaseObject[] { };
shareClass = new ManagementClass("Win32_Share");
inParams = shareClass.GetMethodParameters("Create");
inParams["Name"] = shareName;
inParams["Path"] = new DirectoryInfo(folderPath).FullName;
//// inParams["Description"] = description;
inParams["Type"] = 0x0; // Type of Disk Drive
inParams["Access"] = sd;
outParams = shareClass.InvokeMethod("Create", inParams, null);
if ((uint)outParams["ReturnValue"] != 0)
{
throw new WindowsShareException("Unable to create share. Win32_Share.Create Error Code: " + outParams["ReturnValue"]);
}
}
catch (Exception ex)
{
throw new WindowsShareException("Unable to create share", ex);
}
finally
{
if (shareClass != null)
{
shareClass.Dispose();
}
if (inParams != null)
{
inParams.Dispose();
}
if (outParams != null)
{
outParams.Dispose();
}
if (sd != null)
{
sd.Dispose();
}
}
return new WindowsShare(shareName);
}
示例5: ExportReg
public static void ExportReg(string RegKey, string SavePath, string ServerName)
{
string path = "\"" + SavePath + "\"";
string key = "\"" + RegKey + "\"";
object[] theProcessToRun = { "regedit.exe /e " + path + " " + key + "" };
ManagementClass mClass = new ManagementClass(@"\\" + ServerName + @"\root\cimv2:Win32_Process");
mClass.InvokeMethod("Create", theProcessToRun);
}
示例6: StartProcess
public static string StartProcess(string machineName, string processPath)
{
ManagementClass processTask = new ManagementClass(@"\\" + machineName + @"\root\CIMV2",
"Win32_Process", null);
ManagementBaseObject methodParams = processTask.GetMethodParameters("Create");
methodParams["CommandLine"] = processPath;
ManagementBaseObject exitCode = processTask.InvokeMethod("Create", methodParams, null);
return ProcessMethod.TranslateProcessStartExitCode(exitCode["ReturnValue"].ToString());
}
示例7: Execute
public override void Execute(object context = null)
{
ActionsHomeModel pc = context as ActionsHomeModel;
//Can only be processed if the machine is online...
if (pc.Status == ComputerStates.Online || pc.Status == ComputerStates.LoggedOn)
{
ManagementScope oMs = ConnectToClient(pc.Name);
if (oMs != null)
{
this.DeleteQueryData(oMs, @"root\ccm\Policy", "SELECT * FROM CCM_SoftwareDistribution");
this.DeleteQueryData(oMs, @"root\ccm\Policy", "SELECT * FROM CCM_Scheduler_ScheduledMessage");
this.DeleteQueryData(oMs, @"root\ccm\Scheduler", "SELECT * FROM CCM_Scheduler_History");
//oMs.Path.NamespacePath = @"ROOT\CCM";
ManagementClass oClass = new ManagementClass(oMs, new ManagementPath("SMS_Client"), null);
oClass.Get();
ManagementBaseObject inParams = oClass.GetMethodParameters("ResetPolicy");
inParams["uFlags"] = 1;
oClass.InvokeMethod("ResetPolicy", inParams, new InvokeMethodOptions());
ManagementBaseObject newParams = oClass.GetMethodParameters("RequestMachinePolicy");
oClass.InvokeMethod("RequestMachinePolicy", newParams, new InvokeMethodOptions());
App.Current.Dispatcher.BeginInvoke(new Action(() =>
{
this.State = RemoteActionState.Completed;
}), null);
}
else
{
App.Current.Dispatcher.BeginInvoke(new Action(() =>
{
this.State = RemoteActionState.Error;
}), null);
}
}
else
{
App.Current.Dispatcher.BeginInvoke(new Action(() =>
{
this.State = RemoteActionState.Pending;
}), null);
}
}
示例8: RemoteExec
private void RemoteExec()
{
var processToRun = new[] { "notepad.exe" };
var connection = new ConnectionOptions();
connection.Username = "celine";
connection.Password = "nothing";
connection.Authentication = AuthenticationLevel.Connect;
var wmiScope = new ManagementScope(String.Format("\\\\{0}\\root\\cimv2", "192.168.9.101"), connection);
var wmiProcess = new ManagementClass(wmiScope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
wmiProcess.InvokeMethod("Create", processToRun);
}
示例9: RemoteExecute
public object RemoteExecute(string command)
{
object[] theProcessToRun = { command };
ConnectionOptions theConnection = new ConnectionOptions();
theConnection.Timeout = TimeSpan.FromSeconds(Timeout_Second);
theConnection.Username = CommandExecutionContext.Instance.CurrentMachine.DomainUsername;
theConnection.Password = CommandExecutionContext.Instance.CurrentMachine.Password;
ManagementScope theScope = new ManagementScope("\\\\" + CommandExecutionContext.Instance.CurrentMachine.Address + "\\root\\cimv2", theConnection);
ManagementClass theClass = new ManagementClass(theScope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
object rlt = theClass.InvokeMethod("Create", theProcessToRun);
return rlt;
}
示例10: CreateKey
public static void CreateKey(ManagementScope connectionScope,
baseKey BaseKey,
string key)
{
ManagementClass registryTask = new ManagementClass(connectionScope,
new ManagementPath("DEFAULT:StdRegProv"), new ObjectGetOptions());
ManagementBaseObject methodParams = registryTask.GetMethodParameters("CreateKey");
methodParams["hDefKey"] = BaseKey;
methodParams["sSubKeyName"] = key;
ManagementBaseObject exitCode = registryTask.InvokeMethod("CreateKey",
methodParams, null);
}
示例11: RunCommand
public ManagementBaseObject RunCommand(Dictionary<string, string> inArgs)
{
// WMI: Use Win32_Process in root\cimv2 namespace.
var processClass = new ManagementClass(
new ManagementScope(String.Format(@"\\{0}\root\cimv2", this.hostName), this.connectionOptions),
new ManagementPath("Win32_Process"),
new ObjectGetOptions());
ManagementBaseObject parameters = processClass.GetMethodParameters("Create");
foreach (var item in inArgs)
{
parameters[item.Key] = item.Value;
}
return processClass.InvokeMethod("Create", parameters, null);
}
示例12: InvokeRemoteCommand
/// <summary>
/// Invoca un comando en un servidor remotamente.
/// </summary>
/// <param name="ipServer"></param>
/// <param name="user"></param>
/// <param name="password"></param>
/// <param name="command"></param>
/// <returns></returns>
public static string InvokeRemoteCommand(string ipServer, string user, string password, string command)
{
try
{
var processToRun = new[] { command };
var connection = new ConnectionOptions { Username = user, Password = password };
var wmiScope = new ManagementScope(String.Format("\\\\{0}\\root\\cimv2", ipServer), connection);
var wmiProcess = new ManagementClass(wmiScope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
var result = wmiProcess.InvokeMethod("Create", processToRun);
return result.ToString();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
示例13: EnumerateKeys
public static string[] EnumerateKeys(ManagementScope connectionScope,
baseKey BaseKey,
string key)
{
ManagementClass registryTask = new ManagementClass(connectionScope,
new ManagementPath("DEFAULT:StdRegProv"), new ObjectGetOptions());
ManagementBaseObject methodParams = registryTask.GetMethodParameters("EnumKey");
methodParams["hDefKey"] = BaseKey;
methodParams["sSubKeyName"] = key;
ManagementBaseObject exitCode = registryTask.InvokeMethod("EnumKey",
methodParams, null);
System.String[] subKeys;
subKeys = (string[])exitCode["sNames"];
return subKeys;
}
示例14: getProfile
/*
*Method that execute a WMI command on a remote computer and return all the profile folders' name
* in an arraylist
*/
public static ArrayList getProfile(string computername)
{
//List to store the user profiles
ArrayList profileList = new ArrayList();
//Line of the file written on the client
string line;
ConnectionOptions connOptions = new ConnectionOptions();
ObjectGetOptions objectGetOptions = new ObjectGetOptions();
ManagementPath managementPath = new ManagementPath("Win32_Process");
//To use caller credential
connOptions.Impersonation = ImpersonationLevel.Impersonate;
connOptions.EnablePrivileges = true;
ManagementScope manScope = new ManagementScope(String.Format(@"\\{0}\ROOT\CIMV2", computername), connOptions);
manScope.Connect();
InvokeMethodOptions methodOptions = new InvokeMethodOptions(null, System.TimeSpan.MaxValue);
ManagementClass processClass = new ManagementClass(manScope, managementPath, objectGetOptions);
ManagementBaseObject inParams = processClass.GetMethodParameters("Create");
//The command to execute to get the profile folder and write the result to C:\\tmp.txt
inParams["CommandLine"] = "cmd /c " + "dir C:\\Users /B" + " > c:\\tmp.txt";
ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams,methodOptions);
//Arbitratry delay to make sure that the command is done
Thread.Sleep(2000);
//Reading the result file
StreamReader sr = new StreamReader("\\\\" + computername + "\\c$\\tmp.txt");
line = sr.ReadLine();
//While there are profile
while (line != null)
{
//Add the profile to the arraylist
profileList.Add(line);
line = sr.ReadLine();
}
sr.Close();
return profileList;
}
示例15: ShareCreate
internal int ShareCreate(string ShareName, string FolderPath, string Description)
{
ManagementClass mgmtClass = new ManagementClass("Win32_Share");
ManagementBaseObject inParams = mgmtClass.GetMethodParameters("Create");
ManagementBaseObject outParams;
inParams["Description"] = Description;
inParams["Name"] = ShareName;
inParams["Path"] = FolderPath;
inParams["Type"] = 0x0; //disk drive
inParams["Access"] = SecurityDescriptor(); //for Everyone full perms
outParams = mgmtClass.InvokeMethod("Create", inParams, null);
if ((uint)(outParams.Properties["ReturnValue"].Value) != 0)
{
string errCode = Enum.GetName(typeof(shareCreateErrorCodes), outParams.Properties["ReturnValue"].Value);
MessageBox.Show(String.Format("Unable to create a network share. The error message was {0}\n\nShareName = {1}\nFolderPath = {2}", errCode, ShareName, FolderPath) );
return 1;
}
else return 0;
}