Unsolved
This post is more than 5 years old
12 Posts
0
220129
July 21st, 2014 11:00
OMCI in C#
I am trying to incorporate OMCI into my existing C#.net application. To be exact I am trying to change boot order to a remote PC.
So far I can enumerate the current boot order of a remote PC like:
ManagementPath path = new ManagementPath("\\\\" + computername+ "\\root\\dcim\\sysman");
ManagementScope scope = new ManagementScope(path, coWMI);
SelectQuery BootConfigSetting = new SelectQuery("SELECT * FROM DCIM_BootConfigSetting");
ManagementObjectSearcher bootconfig = new ManagementObjectSearcher(scope, BootConfigSetting);
SelectQuery ElementSettingData = new SelectQuery("SELECT * FROM DCIM_ElementSettingData");
ManagementObjectSearcher element = new ManagementObjectSearcher(scope, ElementSettingData);
SelectQuery BootSourceSetting = new SelectQuery("SELECT * FROM DCIM_BootSourceSetting");
ManagementObjectSearcher bootsource = new ManagementObjectSearcher(scope, BootSourceSetting);
SelectQuery OrderedComponent = new SelectQuery("SELECT * FROM DCIM_OrderedComponent");
ManagementObjectSearcher ordered = new ManagementObjectSearcher(scope, OrderedComponent);
foreach (ManagementObject oldorder in ordered.Get())
{
Response.Write("");
}
I need help translating either powershell or vbscript "ChangeBootOrder" Method into C# without using PowerShell Pipelines if possible.
Any help or direction will be greatly appreciated!
karthikchandran89
49 Posts
0
July 22nd, 2014 05:00
Hi Eddie,
You need to use the InvokeMethod Class ( C# ) for setting "ChangeBootOrder" on DCIM_BootConfigSetting Class of Dell OMCI to set the boot order values. you need to give your modified Array in the source parameter of this method.
this array is nothing but the list of "PartComponent" property in DCIM_OrderedComponent class in Dell OMCI.
Below link is sample of Invokemethod for WMI namespace . you can use the same way by using the Dell OMCI namespace.
http://msdn.microsoft.com/en-us/library/f9ck6sf2(v=vs.110).aspx
// Get the object on which the
// method will be invoked
ManagementClass processClass = new ManagementClass("Dcim_BootConfigSetting");
// Create an array containing all
// arguments for the method
object[] methodArgs = {"your array to set ", "password if set" , null };
//Execute the method
object result = processClass.InvokeMethod( "ChangeBootOrder", methodArgs);
Please share the observations.
eddie1969
12 Posts
0
July 22nd, 2014 09:00
Thanks for the quick response, I really appreciate it. I am a bit confused how to set the new order array and iIam getting an Error "Not found" when I try to pass the array....I am very new to C# so excuse my inexperience. I re-factored a WMI part i already have working to start and shutdown remote pcs and i am trying to adapt it to change the boot order on the Dells, please let me know what I am doing wrong:
string serial = "123456";
ConnectionOptions coWMI = new ConnectionOptions();
string Username = WebConfigurationManager.AppSettings["Username"].ToString();
string Password = WebConfigurationManager.AppSettings["Password"].ToString();
string Authority = WebConfigurationManager.AppSettings["Authority"].ToString();
coWMI.Username = Username;
coWMI.Password = Password;
coWMI.Authority = Authority;
ManagementScope msRP = new ManagementScope(@"\\" + serial + "\\root\\dcim\\sysman", coWMI);
msRP.Connect();
ObjectGetOptions ogoRP = new ObjectGetOptions();
ManagementPath mpRP = new ManagementPath("Dcim_BootConfigSetting");
ManagementClass mcRP = new ManagementClass(msRP, mpRP, ogoRP);
ManagementBaseObject inParams = mcRP.GetMethodParameters("ChangeBootOrder");
object[] strNewOrder = { "0", "2", "3" }; // not sure how to pass the array of modified boot order here!
inParams["PartComponent"] = strNewOrder; //Not Found Error! not even sure if PartComponent is the ///correct property to set here.
inParams["Password"] = Password;
ManagementBaseObject outParams = mcRP.InvokeMethod("ChangeBootOrder", inParams, null);
Again Thanks for the help!
karthikchandran89
49 Posts
0
July 22nd, 2014 23:00
Hi Eddie,
The strNewOrder format seems to be wrong here.Give a try on this.
Array should not be like 0,2,3 .
Run the below command in admin cmd prompt (OMCI installed machine).
wmic /namespace:\\root\dcim\sysman path dcim_orderedcomponent get partcomponent
this will give a output like below list ( this is the current array which needs to be modified for changing the boot order ) .
\\computername\root\dcim\sysman:DCIM_BootSourceSetting.InstanceID="DCIM:BootSoureSetting:UEFI:BootListType-1:index-3"
\\computername\root\dcim\sysman:DCIM_BootSourceSetting.InstanceID="DCIM:BootSoureSetting:UEFI:BootListType-1:index-1"
\\computername\root\dcim\sysman:DCIM_BootSourceSetting.InstanceID="DCIM:BootSoureSetting:UEFI:BootListType-1:index-2"
\\computername\root\dcim\sysman:DCIM_BootSourceSetting.InstanceID="DCIM:BootSoureSetting:UEFI:BootListType-1:index-4"
\\computername\root\dcim\sysman:DCIM_BootSourceSetting.InstanceID="DCIM:BootSoureSetting:UEFI:BootListType-1:index-5"
\\computername\root\dcim\sysman:DCIM_BootSourceSetting.InstanceID="DCIM:BootSoureSetting:UEFI:BootListType-2:index-0"
you need to modify the boot order array like below or as you wish.
strNewOrder
\\computername\root\dcim\sysman:DCIM_BootSourceSetting.InstanceID="DCIM:BootSoureSetting:UEFI:BootListType-1:index-5"
\\computername\root\dcim\sysman:DCIM_BootSourceSetting.InstanceID="DCIM:BootSoureSetting:UEFI:BootListType-1:index-4"
\\computername\root\dcim\sysman:DCIM_BootSourceSetting.InstanceID="DCIM:BootSoureSetting:UEFI:BootListType-1:index-3"
\\computername\root\dcim\sysman:DCIM_BootSourceSetting.InstanceID="DCIM:BootSoureSetting:UEFI:BootListType-1:index-2"
\\computername\root\dcim\sysman:DCIM_BootSourceSetting.InstanceID="DCIM:BootSoureSetting:UEFI:BootListType-1:index-1"
\\computername\root\dcim\sysman:DCIM_BootSourceSetting.InstanceID="DCIM:BootSoureSetting:UEFI:BootListType-2:index-0"
Then finally you need to use the modified array strNewOrder in the Invokemethod class.
To find the exact boot order string ( like USB, DVD rom, HDD etc. ) you need to map the order with the class Dcim_BootSourceSetting
eddie1969
12 Posts
0
July 24th, 2014 18:00
eddie1969
12 Posts
0
July 30th, 2014 06:00
karthikchandran89
49 Posts
0
August 5th, 2014 08:00
Hi Eddie,
if this same works in other machines, then this could be a Bios bug also. can you please share us Bios version details so that we can check the same .
eddie1969
12 Posts
0
August 5th, 2014 08:00
eddie1969
12 Posts
0
August 5th, 2014 08:00
Dell - Sharmad
52 Posts
0
August 10th, 2014 23:00
Hi Eddie.
A few questions in this regards
1. Do you see the behavior consistent across all your systems.
2. Can you check if the DSM Data manager service is running on the system or not?
3. I assume you are using the latest version of OMCI available (v8.2.1) and all its dependencies met.
Since we are not able to reproduce the stated behavior here. We would need more help from you in understand the environment setup you have to help us resolve this at the earliest. Kindly also share any other information incase you feel its needed
eddie1969
12 Posts
0
August 11th, 2014 16:00
eddie1969
12 Posts
0
August 24th, 2014 12:00
Dell - Sharmad
52 Posts
0
August 31st, 2014 23:00
Hi Eddi,
I assume you have set the BIOS password before you are trying to run the script. It is mandatory to set the BIOS password to set TPM.
eddie1969
12 Posts
0
September 2nd, 2014 09:00
Dell - Sharmad
52 Posts
0
September 10th, 2014 10:00
Hi Eddie,
I assume your problem seems to be resolved. Will be glad if you could post the code for others use.
eddie1969
12 Posts
0
September 11th, 2014 06:00
Absolutely, with all the help on this forum I made a solution to set boot order to use only the main HDD and disables all other devices, but this can be worked to any other solution, also turns on TPM and allows copying Bitlocker keys on: 6400, 6510, 755, 5520, 5530, 5400, 5440 models I tested so far...Hope this helps :)
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Management;
using System.Management.Automation;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Dell_Boot_TPM
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
textBox4.ReadOnly = true;
textBox5.ReadOnly = true;
textBox6.ReadOnly = true;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private bool ValidateName()
{
bool bStatus = true;
if (textBox1.Text == "")
{
errorProvider1.SetError(textBox1, "Please Enter Username!");
bStatus = false;
}
else
errorProvider1.SetError(textBox1, "");
return bStatus;
}
private bool ValidatePassword()
{
bool bStatus = true;
if (textBox2.Text == "")
{
errorProvider2.SetError(textBox2, "Please Enter Password!");
bStatus = false;
}
else
errorProvider2.SetError(textBox2, "");
return bStatus;
}
private bool ValidateSerial()
{
bool bStatus = true;
if (textBox3.Text == "")
{
errorProvider3.SetError(textBox3, "Please Enter PC Serial or IP!");
bStatus = false;
}
else
errorProvider3.SetError(textBox3, "");
return bStatus;
}
private void button1_Click(object sender, EventArgs e)
{
ValidateName();
ValidatePassword();
ValidateSerial();
string username = textBox1.Text;
string password = textBox2.Text;
string serial = textBox3.Text;
changeBoot(serial, username, password);
}
private void changeBoot(string host, string username, string password)
{
try
{
string serial = textBox3.Text;
ConnectionOptions coWMI = new ConnectionOptions();
if (!string.IsNullOrEmpty(username))
{
coWMI.Username = username;
coWMI.Password = password;
coWMI.Authentication = AuthenticationLevel.PacketPrivacy;
coWMI.Impersonation = ImpersonationLevel.Impersonate;
coWMI.EnablePrivileges = true;
}
ManagementScope DellmsRP = new ManagementScope("\\\\" + serial + "\\root\\dcim\\sysman", coWMI);
DellmsRP.Connect();
ObjectQuery BootSource = new ObjectQuery("SELECT * FROM DCIM_BootSourceSetting");
ManagementObjectSearcher BootSourceOrder = new ManagementObjectSearcher(DellmsRP, BootSource);
ArrayList element = new ArrayList();
ArrayList instance = new ArrayList();
foreach (ManagementObject queryObjBoot in BootSourceOrder.Get())
{
element.Add(queryObjBoot["ElementName"].ToString());
instance.Add(queryObjBoot["InstanceID"].ToString());
}
string index;
ArrayList device = new ArrayList();
//For UEFI Systems
//index = "UEFI: Hard Drive";
//device.Add(index);
//6400 6510
if (radioButton2.Checked)
{
index = "Internal HDD (IRRT)";
device.Add(index);
}
//755
else if (radioButton1.Checked)
{
index = "Hard drive";
device.Add(index);
}
//5520 5530 5400 5440
else if (radioButton3.Checked)
{
index = "Internal HDD";
device.Add(index);
}
//390 no TPM
else if (radioButton4.Checked)
{
textBox5.Text = "System does not have TPM Module";
string subString = "SATA:";
foreach (string item in element)
{
if (item.Contains(subString))
{
device.Add(item);
}
}
}
//780
else if (radioButton5.Checked)
{
index = "Hard drive";
device.Add(index);
}
int indexItem = element.IndexOf(device[0]);
element.Add(indexItem);
string modorder = instance[indexItem].ToString();
ObjectQuery BootConfig = new ObjectQuery("SELECT * FROM DCIM_BootConfigSetting");
ManagementObjectSearcher BootConfigOrder = new ManagementObjectSearcher(DellmsRP, BootConfig);
ManagementObjectCollection oc = BootConfigOrder.Get();
ManagementObjectCollection.ManagementObjectEnumerator oe = oc.GetEnumerator();
if (oc.Count != 0)
{
while (oe.MoveNext())
{
ArrayList properties = new ArrayList();
ArrayList indexNumber = new ArrayList();
foreach (PropertyData prop in oe.Current.Properties)
{
properties.Add(prop.Value);
}
if (properties[5].ToString() == "DCIM:BootConfigSetting:Next")
{
object[] boot = { "//" + serial + "/root/dcim/sysman:DCIM_BootSourceSetting.InstanceID=\"" + modorder + "\"" };
ManagementObject classInstance = new ManagementObject("\\\\" + serial + "\\root\\dcim\\sysman", "DCIM_BootConfigSetting.InstanceID='" + properties[5] + "'", null);
ManagementBaseObject inParams = classInstance.GetMethodParameters("ChangeBootOrder");
inParams["AuthorizationToken"] = "";
inParams["source"] = boot;
ManagementBaseObject outParams = classInstance.InvokeMethod("ChangeBootOrder", inParams, null);
}
else
{
object[] boot = { "//" + serial + "/root/dcim/sysman:DCIM_BootSourceSetting.InstanceID=\"" + modorder + "\"" };
ManagementObject classInstance = new ManagementObject("\\\\" + serial + "\\root\\dcim\\sysman", "DCIM_BootConfigSetting.InstanceID='" + properties[5] + "'", null);
ManagementBaseObject inParams = classInstance.GetMethodParameters("ChangeBootOrder");
inParams["AuthorizationToken"] = "";
inParams["source"] = boot;
ManagementBaseObject outParams = classInstance.InvokeMethod("ChangeBootOrder", inParams, null);
}
textBox4.Text = "PC: " + serial + " Boot Order Changed Successfully, Changes Will Take Effect After Reboot!";
//Enable TPM
ObjectQuery BiosTPM = new ObjectQuery("SELECT * FROM DCIM_BIOSEnumeration");
ManagementObjectSearcher EnumTPM = new ManagementObjectSearcher(DellmsRP, BiosTPM);
string strKeyValue = "Root/MainSystemChassis/BIOSSetupParent/BiosSetupTPM";
ArrayList instances = new ArrayList();
ArrayList values = new ArrayList();
ArrayList names = new ArrayList();
foreach (ManagementObject queryObjTPM in EnumTPM.Get())
{
instances.Add(queryObjTPM["InstanceID"].ToString());
values.Add(queryObjTPM["CurrentValue"]);
names.Add(queryObjTPM["AttributeName"].ToString());
if (queryObjTPM["InstanceID"].ToString().Equals(strKeyValue))
{
break;
}
}
int indexTPM = instances.IndexOf(strKeyValue);
string tpmindex = instances[indexTPM].ToString();
foreach (var value in values.OfType ().ElementAt(indexTPM))
{
if (value == "2")
{
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();
textBox5.Text = "Serial: " + serial + " TPM Is Now Enabled!";
}
else
{
textBox5.Text = "PC: " + serial + " TPM Already Enabled!";
}
}
}
}
else
{
textBox4.Text = "PC: " + serial + " does not have OMCI Installed!";
}
}
catch (ManagementException err)
{
string serial = textBox4.Text;
textBox4.Text = "PC: " + serial + " Error Accessing System " + err.Message + " or does not have OMCI Installed!";
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
}
private void label5_Click(object sender, EventArgs e)
{
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
}
protected bool Bitlocker()
{
try
{
string username = textBox1.Text;
string password = textBox2.Text;
string serial = textBox3.Text;
ManagementPath path = new ManagementPath();
path.Server = serial;
path.NamespacePath = "\\ROOT\\CIMV2\\Security\\MicrosoftVolumeEncryption";
path.ClassName = "Win32_EncryptableVolume";
ConnectionOptions options = new ConnectionOptions();
options.Username = username;
options.Password = password;
options.Authentication = AuthenticationLevel.PacketPrivacy;
options.Impersonation = ImpersonationLevel.Impersonate;
options.EnablePrivileges = true;
ManagementScope scope = new ManagementScope(path, options);
ObjectGetOptions getOptions = new ObjectGetOptions();
ManagementClass management = new ManagementClass(scope, path, getOptions);
management.Get();
foreach (var vol in management.GetInstances())
if ((uint)vol["ProtectionStatus"] == 1)
{
return true;
}
return false;
}
catch (ManagementException err)
{
string serial = textBox3.Text;
textBox6.Text = "PC: " + serial + " Error Accessing System " + err.Message;
}
return false;
}
private void button2_Click(object sender, EventArgs e)
{
ValidateName();
ValidatePassword();
ValidateSerial();
if (Bitlocker())
{
try
{
string username = textBox1.Text;
string password = textBox2.Text;
string serial = textBox3.Text;
string strManageBDEPath = @"C:\Windows\System32\";
ConnectionOptions connOptions = new ConnectionOptions();
connOptions.Authentication = AuthenticationLevel.PacketPrivacy;
connOptions.Impersonation = ImpersonationLevel.Impersonate;
connOptions.Username = username;
connOptions.Password = password;
ManagementScope mgtScope = new ManagementScope("\\\\" + serial + "\\ROOT\\CIMV2", connOptions);
mgtScope.Connect();
ObjectGetOptions objectGetOptions = new ObjectGetOptions();
ManagementPath mgtPath = new ManagementPath("Win32_Process");
ManagementClass processClass = new ManagementClass(mgtScope, mgtPath, objectGetOptions);
ManagementBaseObject inParams = processClass.GetMethodParameters("Create");
inParams["CommandLine"] = "cmd.exe /k" + strManageBDEPath + "manage-bde" + " -cn " + serial + " -protectors -get c: >" + @"C:\" + serial + ".txt";
ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, null);
string fileToCopy = @"\\" + serial + "\\C$\\" + serial + ".txt";
SaveFileDialog sfd = new SaveFileDialog();
sfd.InitialDirectory = @"C:\";
sfd.FileName = fileToCopy;
sfd.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
sfd.FilterIndex = 1;
sfd.RestoreDirectory = true;
if (sfd.ShowDialog() == DialogResult.OK)
{
System.IO.File.Copy(fileToCopy, sfd.FileName, true);
}
textBox6.Text = "Bitlocker Key also Copied to the Root of C: on " + serial;
}
catch (ManagementException err)
{
string serial = textBox3.Text;
textBox6.Text = "PC: " + serial + " Could Not Save Bitlocker File, Try Again!" + " Error Accessing System " + err.Message;
}
}
else
{
string serial = textBox3.Text;
textBox6.Text = "PC: " + serial + " Bitlocker Paused or System Not Bitlocked Yet!";
}
}
private void textBox6_TextChanged(object sender, EventArgs e)
{
}
private void radioButton4_CheckedChanged(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
ValidateName();
ValidatePassword();
ValidateSerial();
try
{
string username = textBox1.Text;
string password = textBox2.Text;
string serial = textBox3.Text;
ConnectionOptions coWMI = new ConnectionOptions();
coWMI.Username = username;
coWMI.Password = password;
coWMI.Authentication = AuthenticationLevel.PacketPrivacy;
coWMI.Impersonation = ImpersonationLevel.Impersonate;
coWMI.EnablePrivileges = true;
//Creates Remote Process on Selected System to Restart it in 30 seconds
ManagementScope msRP = new ManagementScope("\\\\" + serial + "\\root\\cimv2", coWMI);
msRP.Connect();
ObjectGetOptions ogoRP = new ObjectGetOptions();
ManagementPath mpRP = new ManagementPath("Win32_Process");
ManagementClass mcRP = new ManagementClass(msRP, mpRP, ogoRP);
ManagementBaseObject inParams = mcRP.GetMethodParameters("Create");
inParams["CommandLine"] = @"shutdown /r /t 30";
ManagementBaseObject outParams = mcRP.InvokeMethod("Create", inParams, null);
textBox4.Text = "PC: " + serial + " will Restart in 30 seconds";
}
catch
{
string serial = textBox3.Text;
textBox4.Text = "PC: " + serial + " Error Accessing System.";
}
}
private void button4_Click(object sender, EventArgs e)
{
}
}
}