Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

wmi help 1

Status
Not open for further replies.

bouwob

Programmer
Apr 12, 2006
171
0
0
US
I am completely new to wmi so I figured I would come here for the best help.

I need a script that checks to see is sms is installed and if installed to see if it is running. Does anybody have a script that does this in C#? Any help pertaining to this topic will be greatly appreciated.

tia
 
Code:
   class WakeUpSMS
    {
        private ManagementObject mo;
        private ManagementBaseObject outParams;
        private ManagementBaseObject inParams;
        public bool blSuccess = false;
        private string strComputerName;

        public WakeUpSMS(string strComputerName)
        {
           Console.WriteLine(doWork(strComputerName));
        }
        
        
        // required: string strComputerName: String varible letting function know what computer to act on
        // modifies: if on
        //              nothing
        //           if off
        //              turns CcmExec (SMS) service on
        // effects :If sms is on the chosen computer this function turns it on or relays an error message.
        private string doWork(string strComputerName)
        {
            try
            {
                this.strComputerName = strComputerName;
                mo = new ManagementObject(@"\\" + strComputerName + @"\root\CIMV2", "Win32_Service.Name='CcmExec'", null);

                // Execute the method and obtain the return values.
                outParams = mo.InvokeMethod("StartService", null, null);

                // List outParams
                mo.Dispose();
                return HandleReturn();
            }
            catch (ManagementException err)
            {
                MessageBox.Show("An error occurred while trying to execute the WMI method: " + err.Message);
                return "false";
            }
        }

        // required: string strReturnCode: the code given by WMI after trying to start the process
        // modifies: nothing
        // effects : returns message
        private string HandleReturn()
        {
            string strReturnMessage = "";
            switch (this.outParams["ReturnValue"].ToString())
            {
                //Success
                case "0": strReturnMessage = "Success";
                    blSuccess = true;
                    break;
                //Not Supported
                case "1": strReturnMessage = "Not Supported";
                    break;
                //Access Denied
                case "2": strReturnMessage = "Access Denied";
                    break;
                //Dependent Services Running
                case "3": strReturnMessage = "Dependent Services Running";
                    break;
                //Invalid Service Control
                case "4": strReturnMessage = "Invalid Service Control";
                    break;
                //Service Cannot Accept Control
                case "5": strReturnMessage = "Service Cannot Accept Control";
                    break;
                //Service Not Active
                case "6": strReturnMessage = "Service Not Active";
                    break;
                //Service Request Timeout
                case "7": strReturnMessage = "Service Request Timeout";
                    break;
                //Unknown Failure
                case "8": strReturnMessage = "Unknown Failure";
                    break;
                //Path Not Found
                case "9": strReturnMessage = "Path Not Found";
                    break;
                //Service Already Running
                case "10": strReturnMessage = "Service Already Running";
                    blSuccess = true;
                    break;
                //Service Database Locked
                case "11": strReturnMessage = "Service Database Locked";
                    break;
                //Service Dependency Deleted
                case "12": strReturnMessage = "Service Dependency Deleted";
                    break;
                //Service Dependency Failure
                case "13": strReturnMessage = "Service Dependency Failure";
                    break;
                //Service Disabled
                case "14":
                    try
                    {
                        strReturnMessage = "Service Disabled";
                        inParams = mo.GetMethodParameters("Change");
                        inParams["StartMode"] = "Automatic";
                        mo.InvokeMethod("change", inParams, null);//enable service
                        EnableAutoAssignment = true;
                        outParams = mo.InvokeMethod("StartService", null, null);
                        strReturnMessage = "Success";
                    }
                    catch (Exception ex)
                    {
                        string hhh = ex.ToString();
                    }
                    break;
                //Service Logon Failure
                case "15": strReturnMessage = "Service Logon Failure";
                    break;
                //Service Marked For Deletion
                case "16": strReturnMessage = "Service Marked For Deletion";
                    break;
                //Service No Thread
                case "17": strReturnMessage = "No Thread";
                    break;
                //Status Circular Dependency
                case "18": strReturnMessage = "Status Circular Dependency";
                    break;
                //Status Duplicate Name
                case "19": strReturnMessage = "Status Duplicate Name";
                    break;
                //Status Invalid Name
                case "20": strReturnMessage = "Status Invalid Name";
                    break;
                //Status Invalid Parameter
                case "21": strReturnMessage = "Status Invalid Parameter";
                    break;
                //Status Invalid Service Account
                case "22": strReturnMessage = "Status Invalid Service Account";
                    break;
                //Status Service Exists
                case "23": strReturnMessage = "Status Service Exists";
                    break;
                //Service Already Paused
                case "24": strReturnMessage = "Service Already Paused";
                    break;
            }
            return strReturnMessage;

        }

        public bool EnableAutoAssignment
        {
           set
            {
                ManagementObject MO1;
                MO1 = new ManagementObject(@"\\" + this.strComputerName + @"\ROOT\CCM", "SMS_Client=@", null);
                MO1.SetPropertyValue("EnableAutoAssignment", value);
                MO1.Put();
                MO1.Dispose();
            }
        }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top