Various Scripts and Application Code Segments for .NET, VB, C#, C++, C, Java, JavaScript, HTML, Python, Perl, AutoIT, Batch, ASP Classic, Objective-C, Swift, Unreal Engine 4, Unity3D & others. Also contains numerous IT tidbits, procedures, and tricks including Technology Hacks on various platforms.
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);
}
Subscribe to:
Post Comments (Atom)
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...
-
In Unreal Engine 4 you will eventually need Linear Interpolation between two values to do something like ping pong between two float val...
-
Recently Possess () has been deprecated from UE4 , and when writing classes based on AAIController you have to use the function OnPossess ...
-
Often we intermingle C++ and Blueprints, and need for the two to communicate. With Behavior Trees, using ENUMs is an everyday occurrence an...
No comments:
Post a Comment