Im looking some help with code below. It does not work at all and i did try everything i could, but im simply not able to set "StartMode" value to win service. Reading from cim is working just fine but set is other story and it wont change any property value whatever i try. Ahh almost forget im trying to apply this change on remote machine. This piece of code is took from MS codebase. Need to import Microsoft.Management.Infrastructure.
Ref: https://learn.microsoft.com/en-us/windows/win32/wmisdk/updating-an-entire-instance
public async void SetWinService(string nameService)
{
string Namespace = @"root\cimv2";
CimInstance services = _session.QueryInstances(Namespace, "WQL", $"SELECT * from Win32_Service WHERE Name='{nameService}'").FirstOrDefault();
foreach (var property in services.CimInstanceProperties)
{
//Console.WriteLine("Please type value for Property '" + property.Name + "' of Type:({0}) Value:({1})", property.CimType, property.Value);
if (property.Name == "StartMode")
{
//Change property
property.Value = "Manual";
Console.WriteLine($"Property name: {property.Name} | Property value: {property.Value}");
Console.WriteLine($"Setting property Manual.....");
}
}
services.CimInstanceProperties["StartMode"].Value = "Manual";
services.CimInstanceProperties["DelayedAutoStart"].Value = false;
_session.ModifyInstance(Namespace,services);
}