This post is more than 5 years old
4 Posts
1
9157
Using c# and attempting to use WMI InvokeMethod SetBIOSAttributes only returns "Invalid method Parameter(s)"
I am not unknown to WMI, but the DCIM_BIOSService class got me really scratching my head... First attempts with ManagementObject style only rendered: {"Invalid method Parameter(s) "}.
Couldn't figure out what my problem was... so I changed to Cim style code. Below is an attempt to change Microphone from enabled to disabled state without setting or have a admin password set. But, it doesn't work:
string computer = "localhost";
CimSession Session = CimSession.Create(computer);
UInt32[] BiosServiceResult = { 0 };
Microsoft.Management.Infrastructure.Options.CimOperationOptions cooBiosElement = new Microsoft.Management.Infrastructure.Options.CimOperationOptions();
IEnumerable ciBiosElementInstances = Session.QueryInstances("root\\dcim\\sysman", "WQL", "select * from CIM_BIOSElement", cooBiosElement);
CimInstance ciBiosElementInstance = ciBiosElementInstances.FirstOrDefault();
CimInstance ciBiosServiceInstance = new CimInstance("DCIM_BIOSService", "root\\dcim\\sysman");
CimMethodParametersCollection cmpcBiosService = new CimMethodParametersCollection();
cmpcBiosService.Add( CimMethodParameter.Create("AttributeName", new string[] { "Microphone" }, Microsoft.Management.Infrastructure.CimType.StringArray, CimFlags.In));
cmpcBiosService.Add(CimMethodParameter.Create("AttributeValue", new string[] { "1" }, Microsoft.Management.Infrastructure.CimType.StringArray, CimFlags.In));
cmpcBiosService.Add(CimMethodParameter.Create("TargetBIOS", ciBiosElementInstance, Microsoft.Management.Infrastructure.CimType.Reference, CimFlags.In));
cmpcBiosService.Add(CimMethodParameter.Create("AuthorizationToken", null, Microsoft.Management.Infrastructure.CimType.Instance, CimFlags.In));
//cmpcBiosService.Add(CimMethodParameter.Create("PasswordEncoding", (UInt32) 2 , Microsoft.Management.Infrastructure.CimType.UInt32, CimFlags.In));
//cmpcBiosService.Add(CimMethodParameter.Create("SetResult", BiosServiceResult, Microsoft.Management.Infrastructure.CimType.UInt32Array, CimFlags.Out));
CimMethodResult cmrBIOSService = Session.InvokeMethod(ciBiosServiceInstance, "SetBIOSAttributes", cmpcBiosService);
Now, going through the DCIM_Server_BIOSService.mof file and examining the SetBIOSAttributes method, I notice:
"When required, a token to modify BIOSAttribute "
"values for this computer system. This is usually "
"the BIOS administrator password. If this is a "
"password, the PasswordEncoding parameter shall be "
"used to denote the format of the password string. "
"For example: the CIM_SharedCredential subclass may "
"be utilized. In that case, the "
"CIM_SharedCredential.Secret might be required and "
"if is not specified this method would return 6 "
"Invalid Parameter."
Now, I am merely attempting to alter simple settings, like Microphone or Wake-On-LAN, for which no BIOS admin password is required - imho. But, it all fails.
Anyone got some tips on how to get this method working in c# code?! So, no powershell escapes, as shown in another thread as a work around:
var tpm = @"(gwmi DCIM_BIOSService -namespace root\dcim\sysman -cn " + serial + ").SetBIOSAttributes($null,$null,'Trusted Platform Module','1', $null)";
var shell = PowerShell.Create();
shell.Commands.AddScript(tpm.ToString());
var results = shell.Invoke();
Herman van Drie
4 Posts
2
July 15th, 2017 06:00
Ok, figured this one out myself already. Perhaps a bit stupid of me... how I could have overlooked it:
In above code I thought I could attempt to execute method "SetBiosAttributes" on the class DCIM_BIOSService itself, which doesn't work...
You need to have the DCIM:BiosService instance of DCIM_BIOSService and invoke the method using/on that instance.
Once I did that, it all worked as expected.
Here's the c# code, simplified:
using Microsoft.Management.Infrastructure;
CimSession Session = CimSession.Create("localhost");
Microsoft.Management.Infrastructure.Options.CimOperationOptions cooBiosElement = new Microsoft.Management.Infrastructure.Options.CimOperationOptions();
IEnumerable ciBiosServiceInstances = Session.QueryInstances("root\\dcim\\sysman", "WQL", "select * from DCIM_BIOSService", cooBiosElement);
CimInstance ciBiosServiceInstance = ciBiosServiceInstances.First ();
CimMethodParametersCollection cmpcBiosService = new CimMethodParametersCollection();
cmpcBiosService.Add(CimMethodParameter.Create("AttributeName", new string[] { "Microphone" }, Microsoft.Management.Infrastructure.CimType.StringArray, CimFlags.In));
cmpcBiosService.Add(CimMethodParameter.Create("AttributeValue", new string[] { "1" }, Microsoft.Management.Infrastructure.CimType.StringArray, CimFlags.In));
cmpcBiosService.Add(CimMethodParameter.Create("AuthorizationToken", null, Microsoft.Management.Infrastructure.CimType.Instance, CimFlags.In));
CimMethodResult cmrBIOSService = Session.InvokeMethod(ciBiosServiceInstance, "SetBIOSAttributes", cmpcBiosService);