public static void CreateDirectory(string machineName, string userName, string password, string directory) { ManagementScope scope = WmiHelper.Connect(machineName, userName, password); string workingFolder = null; string startupConfig = null; IntPtr processID = IntPtr.Zero; WmiHelper.InvokeStaticMethod(machineName, userName, password, "Win32_Process", "Create", new object[] { "cmd.exe /c mkdir \"" + directory + "\"", workingFolder, startupConfig, processID }); }
public static bool DoesDirectoryExist(string machineName, string userName, string password, string directory) { ManagementScope scope = WmiHelper.Connect(machineName, userName, password); SelectQuery selectQuery = new SelectQuery(string.Format(@"SELECT * FROM Win32_Directory WHERE Name = '{0}'", directory.Replace("\\", "\\\\"))); ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, selectQuery); if (searcher.Get().Count > 0) { return(true); } else { return(false); } }
public static List <WmiService> GetAllServices(string machineName, string userName, string password, string filterString) { List <WmiService> allServices = new List <WmiService>(); ManagementScope scope = WmiHelper.Connect(machineName, userName, password); // Get all app pools. SelectQuery selectQuery; //if(filterString == null || filterString == String.Empty) selectQuery = new SelectQuery(@"SELECT * FROM Win32_Service"); //else // selectQuery = new SelectQuery(string.Format(@"SELECT * FROM Win32_Service WHERE {0}", filterString)); ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, selectQuery); foreach (ManagementObject managementObject in searcher.Get()) { WmiService newService = new WmiService(); newService.WmiServiceObject = managementObject; foreach (PropertyData propertyData in managementObject.Properties) { if (propertyData.Value == null) { continue; } switch (propertyData.Name) { case "DesktopInteract": bool.TryParse(propertyData.Value.ToString(), out newService.DesktopInteract); break; case "DisplayName": newService.DisplayName = propertyData.Value.ToString(); break; case "ErrorControl": newService.ErrorControl = (ErrorControl)Enum.Parse(typeof(ErrorControl), propertyData.Value.ToString(), true); break; case "LoadOrderGroup": newService.LoadOrderGroup = propertyData.Value.ToString(); break; case "LoadOrderGroupDependencies": newService.LoadOrderGroupDependencies = propertyData.Value.ToString(); break; case "PathName": newService.PathName = propertyData.Value.ToString(); break; case "ServiceDependencies": newService.ServiceDependencies = propertyData.Value.ToString(); break; case "ServiceType": newService.ServiceType = (ServiceType)Enum.Parse(typeof(ServiceType), propertyData.Value.ToString().Replace(" ", ""), true); break; case "StartMode": newService.StartMode = (StartMode)Enum.Parse(typeof(StartMode), propertyData.Value.ToString(), true); break; case "StartName": newService.StartName = propertyData.Value.ToString(); break; case "StartPassword": newService.StartPassword = propertyData.Value.ToString(); break; case "AcceptPause": bool.TryParse(propertyData.Value.ToString(), out newService.AcceptPause); break; case "AcceptStop": bool.TryParse(propertyData.Value.ToString(), out newService.AcceptStop); break; case "CheckPoint": int.TryParse(propertyData.Value.ToString(), out newService.CheckPoint); break; case "CreationClassName": newService.CreationClassName = propertyData.Value.ToString(); break; case "Description": newService.Description = propertyData.Value.ToString(); break; case "ExitCode": int.TryParse(propertyData.Value.ToString(), out newService.ExitCode); break; case "InstallDate": DateTime.TryParse(propertyData.Value.ToString(), out newService.InstallDate); break; case "Name": newService.Name = propertyData.Value.ToString(); break; case "ProcessId": int.TryParse(propertyData.Value.ToString(), out newService.ProcessId); break; case "ServiceSpecificExitCode": int.TryParse(propertyData.Value.ToString(), out newService.ServiceSpecificExitCode); break; case "Started": bool.TryParse(propertyData.Value.ToString(), out newService.Started); break; case "State": newService.State = (State)Enum.Parse(typeof(State), propertyData.Value.ToString().Replace(" ", ""), true); break; case "Status": newService.Status = propertyData.Value.ToString(); break; case "SystemCreationClassName": newService.SystemCreationClassName = propertyData.Value.ToString(); break; case "SystemName": newService.SystemName = propertyData.Value.ToString(); break; case "TagId": int.TryParse(propertyData.Value.ToString(), out newService.TagId); break; case "WaitHint": int.TryParse(propertyData.Value.ToString(), out newService.WaitHint); break; } } // ensure newService is a valid instance -CT if (!string.IsNullOrEmpty(newService.DisplayName)) { allServices.Add(newService); } } List <WmiService> filteredServices = new List <WmiService>(); if (filterString != null && filterString != String.Empty) { DataTable allServicesDataTable = new Business.DataTableConverter.DataTableConverter <WmiService>().GetDataTable(allServices); DataView allServicesView = new DataView(allServicesDataTable); allServicesView.RowFilter = filterString; foreach (DataRowView allServicesRow in allServicesView) { filteredServices.Add((WmiService)allServicesRow["OriginalSourceObject"]); } } else { filteredServices = allServices; } return(filteredServices); }