Friday, November 5, 2010

Using C# WMI to change credentials and start Services



Using WMI C# I was able to create a windows based application that remotely changes credentials then starts and stops services. Above is a screenshot of the application and below is a segment of code.



           try
            {
                ConnectionOptions co = new ConnectionOptions();
                co.Username = txtUserName.Text;
                co.Password = txtPassword.Text;
                co.Impersonation = ImpersonationLevel.Impersonate;
                co.EnablePrivileges = true;
                co.Authentication = AuthenticationLevel.PacketPrivacy;


                string queryStr = 
                "select * from Win32_Service where StartName like '";
                queryStr += txtSU.Text.Replace('\\', '%');
                queryStr += "'";


                string temp1 = txtMachine.Text;
                System.Management.ManagementScope ms =
                    new 
 System.Management.ManagementScope("\\\\" + temp1 + "\\root\\cimv2", co);
                //Query remote computer across the connection


                ObjectQuery oQuery = new ObjectQuery(queryStr);


                //Execute the query  
                ManagementObjectSearcher oSearcher = 
new ManagementObjectSearcher(ms, oQuery);


                //Get the results
                ManagementObjectCollection oReturnCollection = oSearcher.Get();


                foreach (ManagementObject oReturn in oReturnCollection)
                {
                    string serviceName = oReturn.GetPropertyValue("Name") as string;


                    string fullServiceName = "Win32_Service.Name='";
                    fullServiceName += serviceName;
                    fullServiceName += "'";


                    MessageBox.Show("Changing Service name for " +
                        serviceName.ToString() + " to " + txtU.Text,
                   appTitle,
                   MessageBoxButtons.OK,
                   MessageBoxIcon.Information);


                    oReturn.InvokeMethod
                        ("Change", new object[] 
                        { null, null, null, null, null, null, txtU.Text, txtP.Text, null, null, null });


                    oReturn.InvokeMethod
                        ("StopService", new object[] { null });


                    oReturn.InvokeMethod
                        ("StartService", new object[] { null });


                    oReturn.Dispose();
                }
                MessageBox.Show("New credentials for this service has been set!",
                       appTitle,
                       MessageBoxButtons.OK,
                       MessageBoxIcon.Information);
                panel0.Hide();
                panel1.Hide();
                Height = 100;
                lstServices.Items.Clear();
                txtMachine.Text = "";
                txtU.Text = System.Environment.UserDomainName + "\\";
                txtSU.Text = System.Environment.UserDomainName + "\\";
                txtP.Text = "";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,
                       appTitle,
                       MessageBoxButtons.OK,
                       MessageBoxIcon.Error);
            }
        }
            
        private void GetServices1()
        {
            try
            {
                ConnectionOptions co = new ConnectionOptions();
                co.Username = txtUserName.Text;
                co.Password = txtPassword.Text;
                co.Impersonation = ImpersonationLevel.Impersonate;
                co.EnablePrivileges = true;
                co.Authentication = AuthenticationLevel.PacketPrivacy;


                string queryStr = 
"select * from Win32_Service where StartName like '";
                queryStr += txtSU.Text.Replace('\\', '%');
                queryStr += "'";


                string temp1 = txtMachine.Text;
                System.Management.ManagementScope ms = 
                    new System.Management.ManagementScope("\\\\" + temp1 + "\\root\\cimv2", co);
                //Query remote computer across the connection


                ObjectQuery oQuery = new ObjectQuery(queryStr);


                //Execute the query  
                ManagementObjectSearcher oSearcher = 
new ManagementObjectSearcher(ms, oQuery);


                //Get the results
                ManagementObjectCollection oReturnCollection = 
oSearcher.Get();


                foreach (ManagementObject oReturn in oReturnCollection)
                {
                    string serviceName = 
oReturn.GetPropertyValue("Name") as string;


                    string fullServiceName = "Win32_Service.Name='";
                    fullServiceName += serviceName;
                    fullServiceName += "'";
                    lstServices.Items.Add(serviceName);
                }
                panel1.Show();
                Height = 300;
                MessageBox.Show("Services are loaded!",
                   appTitle,
                   MessageBoxButtons.OK,
                   MessageBoxIcon.Information);
                
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,
                   appTitle,
                   MessageBoxButtons.OK,
                   MessageBoxIcon.Error); 
            }

No comments:

Post a Comment

Generating "Always On Top" NSWindow in macOS across all detected displays

Also: Using UIKit & Cocoa Frameworks using Objective-C In m acOS or OS X , written in either Objective-C or Swift  Langues, you m...