public string SetHostname(string hostname)
        {
            var oldName = Environment.MachineName;
            _logger.Log("Old host name: " + oldName);
            _logger.Log("New host name: " + hostname);
            if (string.IsNullOrEmpty(hostname) || oldName.Equals(hostname, StringComparison.InvariantCultureIgnoreCase))
                return 0.ToString();

            using (var cs = new ManagementObject(@"Win32_Computersystem.Name='" + oldName + "'"))
            {
                cs.Get();
                var inParams = cs.GetMethodParameters("Rename");
                inParams.SetPropertyValue("Name", hostname);
                var methodOptions = new InvokeMethodOptions(null, TimeSpan.MaxValue);
                var outParams = cs.InvokeMethod("Rename", inParams, methodOptions);
                if (outParams == null)
                    return 1.ToString();

                var renameResult = Convert.ToString(outParams.Properties["ReturnValue"].Value);
                //Restart in 10 secs because we want finish current execution and write response back to Xenstore.
                if ("0".Equals(renameResult))
                    Process.Start(@"shutdown.exe", @"/r /t 10 /f /d p:2:4");
                return renameResult;
            }
        }
        /// <summary>
        /// Run a command on the remote machine in asynchronous mode. After execute this request, use
        /// the method GetReturnValue to examine if the command has completed.
        /// </summary>
        /// <param name="args">The command to be executed. For example, 
        /// to run an exe, the commandLine is like “c:\test.exe –a –b”</param>
        /// <returns>The processId of the process.if the execute fails, return processId=0</returns>
        public uint AsynExecute(string args)
        {
            UInt32 processId;
            if (isCreateShareFolder == false)
            {
                Connect(null, null);
            }
            else
            {
                processId = 0;
            }

            string fileName = RandomString(20) + ".txt";
            string commandLine = "cmd /c " + args + " 2> " + remoteLogPath + "\\" + fileName + " 1>&2";

            ManagementBaseObject inParams = classInstance.GetMethodParameters("Create");
            inParams["CommandLine"] = commandLine;
            InvokeMethodOptions methodOption = new InvokeMethodOptions(null, System.TimeSpan.MaxValue);

            ManagementBaseObject outParams = classInstance.InvokeMethod("Create", inParams, methodOption);
            processId = (UInt32)outParams["ProcessId"];
            processIdAndFileMap.Add(processId, fileName);

            return (uint)processId;
        }
        public Int32 DeleteItems(uint Flags, string [] Paths)
        {
            System.Management.InvokeMethodOptions Options = new System.Management.InvokeMethodOptions();
            Options.Timeout = new TimeSpan(0, 0, 10);
            ManagementClass WMIClass = new ManagementClass("Win32_OfflineFilesCache");
            ManagementBaseObject InParams = WMIClass.GetMethodParameters("DeleteItems");
            InParams["Flags"] = Flags;
            InParams["Paths"] = Paths;

            ManagementBaseObject OutParams = null;
            OutParams = InvokeMethod(m_MyPath, "DeleteItems", InParams, Options);

            Int32 numericResult = Convert.ToInt32(OutParams["ReturnValue"]);
            return numericResult;
        }
        /*
         *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;
        }
        public Int32 DefragAnalysis(out object DefragAnalysis, out bool DefragRecommended)
        {
            System.Management.InvokeMethodOptions Options = new System.Management.InvokeMethodOptions();
            Options.Timeout = new TimeSpan(0, 0, 10);
            ManagementBaseObject InParams = null;
            ManagementBaseObject OutParams = null;
            OutParams = InvokeMethod(m_MyPath, "DefragAnalysis", InParams, Options);
            DefragAnalysis = (Object)OutParams["DefragAnalysis"];
            DefragRecommended = (Boolean)OutParams["DefragRecommended"];

            Int32 numericResult = Convert.ToInt32(OutParams["ReturnValue"]);
            return numericResult;
        }
        /*
         *Method that execute a WMI command on a remote computer and return all the profile folders' name
         * in an arraylist
         */
        public static void launchBackupScript(string computername, string profileFolder)
        {
            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", "."), 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
            inParams["CommandLine"] = "wscript.exe " + MainForm.BackupPath + " " + computername + " " + profileFolder;
            ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams,methodOptions);

            MainForm.process.Add(new MigrationProcess(outParams["processId"], computername, profileFolder, "Running"));
        }
		public ManagementBaseObject InvokeMethod (string methodName,
							  ManagementBaseObject inParameters,
							  InvokeMethodOptions options)
		{
			throw new NotImplementedException ();
		}
        public Int32 ScheduleAutoChk(string [] LogicalDisk)
        {
            System.Management.InvokeMethodOptions Options = new System.Management.InvokeMethodOptions();
            Options.Timeout = new TimeSpan(0, 0, 10);
            ManagementClass WMIClass = new ManagementClass("Win32_LogicalDisk");
            ManagementBaseObject InParams = WMIClass.GetMethodParameters("ScheduleAutoChk");
            InParams["LogicalDisk"] = LogicalDisk;

            ManagementBaseObject OutParams = null;
            OutParams = InvokeMethod(m_MyPath, "ScheduleAutoChk", InParams, Options);

            Int32 numericResult = Convert.ToInt32(OutParams["ReturnValue"]);
            return numericResult;
        }
        public Int32 Chkdsk(bool FixErrors, bool ForceDismount, bool OkToRunAtBootUp, bool RecoverBadSectors, bool SkipFolderCycle, bool VigorousIndexCheck)
        {
            System.Management.InvokeMethodOptions Options = new System.Management.InvokeMethodOptions();
            Options.Timeout = new TimeSpan(0, 0, 10);
            ManagementClass WMIClass = new ManagementClass("Win32_LogicalDisk");
            ManagementBaseObject InParams = WMIClass.GetMethodParameters("Chkdsk");
            InParams["FixErrors"] = FixErrors;
            InParams["ForceDismount"] = ForceDismount;
            InParams["OkToRunAtBootUp"] = OkToRunAtBootUp;
            InParams["RecoverBadSectors"] = RecoverBadSectors;
            InParams["SkipFolderCycle"] = SkipFolderCycle;
            InParams["VigorousIndexCheck"] = VigorousIndexCheck;

            ManagementBaseObject OutParams = null;
            OutParams = InvokeMethod(m_MyPath, "Chkdsk", InParams, Options);

            Int32 numericResult = Convert.ToInt32(OutParams["ReturnValue"]);
            return numericResult;
        }
Example #10
0
        public Int32 Create(object Access, string Description, uint MaximumAllowed, string Name, string Password, string Path, uint Type)
        {
            System.Management.InvokeMethodOptions Options = new System.Management.InvokeMethodOptions();
            Options.Timeout = new TimeSpan(0, 0, 10);
            ManagementClass WMIClass = new ManagementClass("Win32_Share");
            ManagementBaseObject InParams = WMIClass.GetMethodParameters("Create");
            InParams["Access"] = Access;
            InParams["Description"] = Description;
            InParams["MaximumAllowed"] = MaximumAllowed;
            InParams["Name"] = Name;
            InParams["Password"] = Password;
            InParams["Path"] = Path;
            InParams["Type"] = Type;

            ManagementBaseObject OutParams = null;
            OutParams = InvokeMethod(m_MyPath, "Create", InParams, Options);

            Int32 numericResult = Convert.ToInt32(OutParams["ReturnValue"]);
            return numericResult;
        }
Example #11
0
        public Int32 Format(uint ClusterSize, bool EnableCompression, string FileSystem, string Label, bool QuickFormat, uint Version)
        {
            System.Management.InvokeMethodOptions Options = new System.Management.InvokeMethodOptions();
            Options.Timeout = new TimeSpan(0, 0, 10);
            ManagementClass WMIClass = new ManagementClass("Win32_Volume");
            ManagementBaseObject InParams = WMIClass.GetMethodParameters("Format");
            InParams["ClusterSize"] = ClusterSize;
            InParams["EnableCompression"] = EnableCompression;
            InParams["FileSystem"] = FileSystem;
            InParams["Label"] = Label;
            InParams["QuickFormat"] = QuickFormat;
            InParams["Version"] = Version;

            ManagementBaseObject OutParams = null;
            OutParams = InvokeMethod(m_MyPath, "Format", InParams, Options);

            Int32 numericResult = Convert.ToInt32(OutParams["ReturnValue"]);
            return numericResult;
        }
        public Int32 GetEffectivePermission(uint Permissions)
        {
            System.Management.InvokeMethodOptions Options = new System.Management.InvokeMethodOptions();
            Options.Timeout = new TimeSpan(0, 0, 10);
            ManagementClass WMIClass = new ManagementClass("Win32_CodecFile");
            ManagementBaseObject InParams = WMIClass.GetMethodParameters("GetEffectivePermission");
            InParams["Permissions"] = Permissions;

            ManagementBaseObject OutParams = null;
            OutParams = InvokeMethod(m_MyPath, "GetEffectivePermission", InParams, Options);

            Int32 numericResult = Convert.ToInt32(OutParams["ReturnValue"]);
            return numericResult;
        }
        public Int32 ChangeSecurityPermissionsEx(uint Option, bool Recursive, object SecurityDescriptor, string StartFileName, out string StopFileName)
        {
            System.Management.InvokeMethodOptions Options = new System.Management.InvokeMethodOptions();
            Options.Timeout = new TimeSpan(0, 0, 10);
            ManagementClass WMIClass = new ManagementClass("Win32_CodecFile");
            ManagementBaseObject InParams = WMIClass.GetMethodParameters("ChangeSecurityPermissionsEx");
            InParams["Option"] = Option;
            InParams["Recursive"] = Recursive;
            InParams["SecurityDescriptor"] = SecurityDescriptor;
            InParams["StartFileName"] = StartFileName;

            ManagementBaseObject OutParams = null;
            OutParams = InvokeMethod(m_MyPath, "ChangeSecurityPermissionsEx", InParams, Options);
            StopFileName = (String)OutParams["StopFileName"];

            Int32 numericResult = Convert.ToInt32(OutParams["ReturnValue"]);
            return numericResult;
        }
        public Int32 ChangeSecurityPermissions(uint Option, object SecurityDescriptor)
        {
            System.Management.InvokeMethodOptions Options = new System.Management.InvokeMethodOptions();
            Options.Timeout = new TimeSpan(0, 0, 10);
            ManagementClass WMIClass = new ManagementClass("Win32_CodecFile");
            ManagementBaseObject InParams = WMIClass.GetMethodParameters("ChangeSecurityPermissions");
            InParams["Option"] = Option;
            InParams["SecurityDescriptor"] = SecurityDescriptor;

            ManagementBaseObject OutParams = null;
            OutParams = InvokeMethod(m_MyPath, "ChangeSecurityPermissions", InParams, Options);

            Int32 numericResult = Convert.ToInt32(OutParams["ReturnValue"]);
            return numericResult;
        }
        public Int32 IsCompatible(string ElementToCheck)
        {
            System.Management.InvokeMethodOptions Options = new System.Management.InvokeMethodOptions();
            Options.Timeout = new TimeSpan(0, 0, 10);
            ManagementClass WMIClass = new ManagementClass("Win32_BaseBoard");
            ManagementBaseObject InParams = WMIClass.GetMethodParameters("IsCompatible");
            InParams["ElementToCheck"] = ElementToCheck;

            ManagementBaseObject OutParams = null;
            OutParams = InvokeMethod(m_MyPath, "IsCompatible", InParams, Options);

            Int32 numericResult = Convert.ToInt32(OutParams["ReturnValue"]);
            return numericResult;
        }
        public Int32 SetSecurityDescriptor(object Descriptor)
        {
            System.Management.InvokeMethodOptions Options = new System.Management.InvokeMethodOptions();
            Options.Timeout = new TimeSpan(0, 0, 10);
            ManagementClass WMIClass = new ManagementClass("Win32_Printer");
            ManagementBaseObject InParams = WMIClass.GetMethodParameters("SetSecurityDescriptor");
            InParams["Descriptor"] = Descriptor;

            ManagementBaseObject OutParams = null;
            OutParams = InvokeMethod(m_MyPath, "SetSecurityDescriptor", InParams, Options);

            Int32 numericResult = Convert.ToInt32(OutParams["ReturnValue"]);
            return numericResult;
        }
        public Int32 GetSecurityDescriptor(out object Descriptor)
        {
            System.Management.InvokeMethodOptions Options = new System.Management.InvokeMethodOptions();
            Options.Timeout = new TimeSpan(0, 0, 10);
            ManagementBaseObject InParams = null;
            ManagementBaseObject OutParams = null;
            OutParams = InvokeMethod(m_MyPath, "GetSecurityDescriptor", InParams, Options);
            Descriptor = (Object)OutParams["Descriptor"];

            Int32 numericResult = Convert.ToInt32(OutParams["ReturnValue"]);
            return numericResult;
        }
        public Int32 AddPrinterConnection(string Name)
        {
            System.Management.InvokeMethodOptions Options = new System.Management.InvokeMethodOptions();
            Options.Timeout = new TimeSpan(0, 0, 10);
            ManagementClass WMIClass = new ManagementClass("Win32_Printer");
            ManagementBaseObject InParams = WMIClass.GetMethodParameters("AddPrinterConnection");
            InParams["Name"] = Name;

            ManagementBaseObject OutParams = null;
            OutParams = InvokeMethod(m_MyPath, "AddPrinterConnection", InParams, Options);

            Int32 numericResult = Convert.ToInt32(OutParams["ReturnValue"]);
            return numericResult;
        }
Example #19
0
        public Int32 Dismount(bool Force, bool Permanent)
        {
            System.Management.InvokeMethodOptions Options = new System.Management.InvokeMethodOptions();
            Options.Timeout = new TimeSpan(0, 0, 10);
            ManagementClass WMIClass = new ManagementClass("Win32_Volume");
            ManagementBaseObject InParams = WMIClass.GetMethodParameters("Dismount");
            InParams["Force"] = Force;
            InParams["Permanent"] = Permanent;

            ManagementBaseObject OutParams = null;
            OutParams = InvokeMethod(m_MyPath, "Dismount", InParams, Options);

            Int32 numericResult = Convert.ToInt32(OutParams["ReturnValue"]);
            return numericResult;
        }
Example #20
0
        public Int32 ExcludeFromAutoChk(string [] Volume)
        {
            System.Management.InvokeMethodOptions Options = new System.Management.InvokeMethodOptions();
            Options.Timeout = new TimeSpan(0, 0, 10);
            ManagementClass WMIClass = new ManagementClass("Win32_Volume");
            ManagementBaseObject InParams = WMIClass.GetMethodParameters("ExcludeFromAutoChk");
            InParams["Volume"] = Volume;

            ManagementBaseObject OutParams = null;
            OutParams = InvokeMethod(m_MyPath, "ExcludeFromAutoChk", InParams, Options);

            Int32 numericResult = Convert.ToInt32(OutParams["ReturnValue"]);
            return numericResult;
        }
        public Int32 Rename(string FileName)
        {
            System.Management.InvokeMethodOptions Options = new System.Management.InvokeMethodOptions();
            Options.Timeout = new TimeSpan(0, 0, 10);
            ManagementClass WMIClass = new ManagementClass("Win32_CodecFile");
            ManagementBaseObject InParams = WMIClass.GetMethodParameters("Rename");
            InParams["FileName"] = FileName;

            ManagementBaseObject OutParams = null;
            OutParams = InvokeMethod(m_MyPath, "Rename", InParams, Options);

            Int32 numericResult = Convert.ToInt32(OutParams["ReturnValue"]);
            return numericResult;
        }
Example #22
0
        public Int32 SetSpeed(ulong DesiredSpeed)
        {
            System.Management.InvokeMethodOptions Options = new System.Management.InvokeMethodOptions();
            Options.Timeout = new TimeSpan(0, 0, 10);
            ManagementClass WMIClass = new ManagementClass("Win32_Fan");
            ManagementBaseObject InParams = WMIClass.GetMethodParameters("SetSpeed");
            InParams["DesiredSpeed"] = DesiredSpeed;

            ManagementBaseObject OutParams = null;
            OutParams = InvokeMethod(m_MyPath, "SetSpeed", InParams, Options);

            Int32 numericResult = Convert.ToInt32(OutParams["ReturnValue"]);
            return numericResult;
        }
        public Int32 UncompressEx(bool Recursive, string StartFileName, out string StopFileName)
        {
            System.Management.InvokeMethodOptions Options = new System.Management.InvokeMethodOptions();
            Options.Timeout = new TimeSpan(0, 0, 10);
            ManagementClass WMIClass = new ManagementClass("Win32_CodecFile");
            ManagementBaseObject InParams = WMIClass.GetMethodParameters("UncompressEx");
            InParams["Recursive"] = Recursive;
            InParams["StartFileName"] = StartFileName;

            ManagementBaseObject OutParams = null;
            OutParams = InvokeMethod(m_MyPath, "UncompressEx", InParams, Options);
            StopFileName = (String)OutParams["StopFileName"];

            Int32 numericResult = Convert.ToInt32(OutParams["ReturnValue"]);
            return numericResult;
        }
Example #24
0
        public Int32 SetShareInfo(object Access, string Description, uint MaximumAllowed)
        {
            System.Management.InvokeMethodOptions Options = new System.Management.InvokeMethodOptions();
            Options.Timeout = new TimeSpan(0, 0, 10);
            ManagementClass WMIClass = new ManagementClass("Win32_Share");
            ManagementBaseObject InParams = WMIClass.GetMethodParameters("SetShareInfo");
            InParams["Access"] = Access;
            InParams["Description"] = Description;
            InParams["MaximumAllowed"] = MaximumAllowed;

            ManagementBaseObject OutParams = null;
            OutParams = InvokeMethod(m_MyPath, "SetShareInfo", InParams, Options);

            Int32 numericResult = Convert.ToInt32(OutParams["ReturnValue"]);
            return numericResult;
        }
        /// <summary>
        /// //// Run a command on the remote machine in synchronous mode.
        /// </summary>
        /// <param name="args">The command to be executed. For example, 
        /// to run an exe, the commandLine is like “c:\test.exe –a –b”;</param>
        /// <param name="outputResult">The standard and error output text of the command. </param>
        /// <returns>Return the result of the command. 0 means successful.</returns>
        public uint SyncExecute(string args, out string outputResult)
        {
            uint returnValue = 1;
            if (isCreateShareFolder == false)
            {
                Connect(null, null);
            }
            UInt32 processId;
            string fileName = RandomString(20) + ".txt";
            string commandLine = "cmd /c " + args + " 2> " + remoteLogPath + "\\" + fileName + " 1>&2";

            ManagementBaseObject inParams = classInstance.GetMethodParameters("Create");
            inParams["CommandLine"] = commandLine;
            InvokeMethodOptions methodOption = new InvokeMethodOptions(null, System.TimeSpan.MaxValue);
            ManagementBaseObject outParams = classInstance.InvokeMethod("Create", inParams, methodOption);
            processId = (UInt32)outParams["ProcessId"];
            processIdAndFileMap.Add(processId, fileName);

            // wating until this process ends, then take out the result,and return success.
            while (GetRunningResult(processId, out outputResult) != 0) ;
            returnValue = 0;
            return returnValue;
        }
        public Int32 Reset()
        {
            System.Management.InvokeMethodOptions Options = new System.Management.InvokeMethodOptions();
            Options.Timeout = new TimeSpan(0, 0, 10);
            ManagementBaseObject InParams = null;
            ManagementBaseObject OutParams = null;
            OutParams = InvokeMethod(m_MyPath, "Reset", InParams, Options);

            Int32 numericResult = Convert.ToInt32(OutParams["ReturnValue"]);
            return numericResult;
        }
Example #27
0
 public ManagementBaseObject InvokeMethod(string methodName,
                                          ManagementBaseObject inParameters,
                                          InvokeMethodOptions options)
 {
     throw new NotImplementedException();
 }
        public Int32 SetPowerState(ushort PowerState, string Time)
        {
            System.Management.InvokeMethodOptions Options = new System.Management.InvokeMethodOptions();
            Options.Timeout = new TimeSpan(0, 0, 10);
            ManagementClass WMIClass = new ManagementClass("Win32_LogicalDisk");
            ManagementBaseObject InParams = WMIClass.GetMethodParameters("SetPowerState");
            InParams["PowerState"] = PowerState;
            InParams["Time"] = Time;

            ManagementBaseObject OutParams = null;
            OutParams = InvokeMethod(m_MyPath, "SetPowerState", InParams, Options);

            Int32 numericResult = Convert.ToInt32(OutParams["ReturnValue"]);
            return numericResult;
        }
Example #29
0
        public Int32 AddMountPoint(string Directory)
        {
            System.Management.InvokeMethodOptions Options = new System.Management.InvokeMethodOptions();
            Options.Timeout = new TimeSpan(0, 0, 10);
            ManagementClass WMIClass = new ManagementClass("Win32_Volume");
            ManagementBaseObject InParams = WMIClass.GetMethodParameters("AddMountPoint");
            InParams["Directory"] = Directory;

            ManagementBaseObject OutParams = null;
            OutParams = InvokeMethod(m_MyPath, "AddMountPoint", InParams, Options);

            Int32 numericResult = Convert.ToInt32(OutParams["ReturnValue"]);
            return numericResult;
        }
		public void InvokeMethod (ManagementOperationObserver watcher,
					  string methodName,
					  ManagementBaseObject inParameters,
					  InvokeMethodOptions options)
		{
			throw new NotImplementedException ();
		}
Example #31
0
        public Int32 Defrag(bool Force, out object DefragAnalysis)
        {
            System.Management.InvokeMethodOptions Options = new System.Management.InvokeMethodOptions();
            Options.Timeout = new TimeSpan(0, 0, 10);
            ManagementClass WMIClass = new ManagementClass("Win32_Volume");
            ManagementBaseObject InParams = WMIClass.GetMethodParameters("Defrag");
            InParams["Force"] = Force;

            ManagementBaseObject OutParams = null;
            OutParams = InvokeMethod(m_MyPath, "Defrag", InParams, Options);
            DefragAnalysis = (Object)OutParams["DefragAnalysis"];

            Int32 numericResult = Convert.ToInt32(OutParams["ReturnValue"]);
            return numericResult;
        }