using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections.ObjectModel; using System.Management.Automation; using System.Management.Automation.Provider; using System.Management.Automation.Runspaces; namespace Intel.Management.Module.Amt { /// /// This sample implements a Windows PowerShell provider class /// that acts upon AMT Hardware. /// /// /// Get-PSDrive /// $DebugPreference = "Continue" /// $DebugPreference = "SilentlyContinue" /// [CmdletProvider("AmtSystem", ProviderCapabilities.Credentials)] public class AmtDriveProvider : DriveProvider { //In this context, an "item" is an element of data within a data store. /* MSH> MSH>$a = "foobar" MSH>$a = $a | AddNote CreatedBy Me MSH>$a.CreatedBy Me*/ //[convert]::ToBase64String /* Dynamic properties public class SendGreetingCommandDynamicParameters { [Parameter(Mandatory = true)] [Parameter] [ValidateSet ("Marketing", "Sales", "Development")] public string Department { get { return department; } set { department = value; } } private string department; } */ public AmtDriveProvider() : base() { //PSCredential } /// /// The Windows PowerShell engine calls this method when the /// New-PSDrive cmdlet is run and the path to this provider is /// specified. This method creates a connection to the database /// file and sets the Connection property of the PSDriveInfo object. /// /// /// Information describing the drive to create. /// /// An AmtDriveInfo object that represents /// the new drive. protected override PSDriveInfo NewDrive(PSDriveInfo drive) { //AmtDriveInfo.AmtDriveParameters localParams = _driveParams; //_driveParams=null; WriteDebug("AMT NewDrive"); PSDriveInfo result = null; result = new AmtDriveInfo(drive,DynamicParameters as AmtDriveParameters); //check root for valid store return result; } protected override object NewDriveDynamicParameters() { return new AmtDriveParameters(); } //private Collection _drives; /// /// When the provider engine starts a provider, the InitializeDefaultDrives method /// is called. This is an opportunity for the provider to mount drives that are /// important to it. /// /// A collection of PSDriveInfo objects for each drive the provider /// wants to mount. /* protected override Collection InitializeDefaultDrives() { /* if (_drives == null) { WriteDebug("Initializing amt drive"); _drives = new Collection(); _drives.Add(new PSDriveInfo("AMT", ProviderInfo, "Config", "Intel(r) AMT Subsystem", Credential)); } }*/ /// /// The Windows PowerShell engine calls this method when the /// Remove-PSDrive cmdlet is run and the path to this provider is /// specified. This method closes a hardware connection /// /// The drive to remove. /// An AMTDrive object that represents /// the removed drive. protected override PSDriveInfo RemoveDrive(PSDriveInfo drive) { return drive; } // End RemoveDrive. /* /// /// Definition of an AmtDriveInfo object /// internal class AmtDriveInfo : PSDriveInfo { AmtDriveItem _root; HECIClass _heci; string _cashedPath; AmtDriveItem _cashedItem; bool _bDriverLoaded; bool _bDriverLocked; public AmtDriveInfo(PSDriveInfo info) : base(info) { _heci = new HECIClass(); _cashedItem = null; _cashedPath = string.Empty; //_root = new ContainerItem("LocalDriver", _root = new AmtRootItem(_heci); _bDriverLoaded = false; } public void LockDriver() { _bDriverLocked=true; } public void UnlockDriver() { _bDriverLocked = false; UnLoadDriver(); } public void LoadDriver() { if (!_bDriverLoaded) { _heci.Init(); _bDriverLoaded = true; } } public void UnLoadDriver() { if (_bDriverLoaded && !_bDriverLocked) { _heci.DeInit(); _bDriverLoaded = false; } } public AmtDriveItem GetItemFromPath(string path) { //check root if (string.IsNullOrEmpty(path) || string.Compare(this.Name,path,true)==0 || string.Compare(this.Name+"\\", path, true)==0) { return _root; } // Get path components char[] splits = {'\\'}; string[] paths = path.Split(splits, StringSplitOptions.RemoveEmptyEntries); //find item in path AmtDriveItem parent = _root; AmtDriveItem foundItem = null; //load heci if its in the path foreach (string pathItem in paths) { if (string.Compare(pathItem, AmtHeciRootItem.HostDriveName, true) == 0) LoadDriver(); } //check and see if the item is cached if (string.Compare(path, _cashedPath, true) == 0) { return _cashedItem; } else { //check if parent is cached int pos = path.ToLower().IndexOf(_cashedPath.ToLower() + "\\"); if (pos == 0) { paths = path.Substring(_cashedPath.Length).Split(splits, StringSplitOptions.RemoveEmptyEntries); parent = _cashedItem; } } try { for (int i = 0; i < paths.Length; i++) { foundItem = null; foreach (AmtDriveItem childItem in parent.GetChildItems()) { if (string.Compare(childItem.Name, paths[i], true) == 0) { foundItem = childItem; parent = foundItem; break; } } if (foundItem == null) { break; } else { _cashedItem = foundItem; _cashedPath = path; } } } finally { UnLoadDriver(); } return foundItem; } } // End defintion of AMTDriveInfo internal class AmtRootContainerValue : ContainerItemValue { AmtDriveItem[] _items; public AmtRootContainerValue(HECIClass heci) { //build the array _items = new AmtDriveItem[1]; _items[0]= new AmtHeciRootItem(heci); } public override AmtDriveItem[] GetChildItems() { return _items; } } /// /// Definition of the Root Container /// internal class AmtRootItem : AmtDriveItem { AmtRootContainerValue _value; public AmtRootItem(HECIClass heci) { _value = new AmtRootContainerValue(heci); } public override string Name { get { return DriveName; } } public override object Value { get { return _value; } } public override bool HasChildren { get { return true; } } public static string DriveName { get { return "AMT"; } } } //End Definition of the Root Container internal class AmtSetupContainerValue : ContainerItemValue { HECIClass _heci; public AmtSetupContainerValue(HECIClass heci) { _heci = heci; } public override AmtDriveItem[] GetChildItems() { List list = new List(); HECIClass.ProvisioningMode mode; bool bOk = _heci.GetProvisioningMode(out mode); if (bOk) list.Add(new AmtPropertyItem("ProvisioningMode", mode)); bool ztcEnabled; bOk = _heci.GetZeroTouchEnabled(out ztcEnabled); if (bOk) list.Add(new AmtPropertyItem("ZeroTouch", ztcEnabled)); HECIClass.TlsMode tlsMode; bOk = _heci.GetTlsMode(out tlsMode); if (bOk) list.Add(new AmtPropertyItem("SetupMode", tlsMode)); HECIClass.RngSeedStatus rngStatus; bOk = _heci.GetRngSeedStatus(out rngStatus); if (bOk) list.Add(new AmtPropertyItem("RngStatus", rngStatus)); return list.ToArray(); } } /// /// Definition of the setup container /// internal class AmtSetupItem : AmtDriveItem { AmtSetupContainerValue _value; public AmtSetupItem(HECIClass heci) { _value = new AmtSetupContainerValue(heci); } public override bool HasChildren { get { return true; } } public override string Name { get { return "LegacySetup"; } } public override object Value { get { return _value; } } } /// /// Definition of an individual hash item /// internal class AmtHashEntryItem : AmtDriveItem { AmtHashContainerValue _value; public AmtHashEntryItem(HECIClass.HashEntry entry) { _value = new AmtHashContainerValue(entry); } public override bool HasChildren { get { return true; } } public override string Name { get { return _value.Name; } } public override object Value { get { return _value; } } } // Definition of an Hash Entry Item /// /// Container value for a Hash Entry /// internal class AmtHashContainerValue : ContainerItemValue { HECIClass.HashEntry _entry; public AmtHashContainerValue(HECIClass.HashEntry entry) { _entry = entry; } public string Name { get { return _entry.Name; } } public override AmtDriveItem[] GetChildItems() { List list = new List(); list.Add(new AmtPropertyItem("Name", _entry.Name)); list.Add(new AmtPropertyItem("IsDefault", _entry.IsDefault)); list.Add(new AmtPropertyItem("IsActive", _entry.IsActive)); list.Add(new AmtPropertyItem("Thumbprint", _entry.Thumbprint)); list.Add(new AmtPropertyItem("HashString", _entry.HashString)); return list.ToArray(); } public override string ToString() { return _entry.Thumbprint; } } /// /// Container value for Hashes /// internal class AmtHashesContainerValue : ContainerItemValue { HECIClass _heci; public AmtHashesContainerValue(HECIClass heci) { _heci = heci; } public override AmtDriveItem[] GetChildItems() { HECIClass.HashEntry[] hashes; AmtDriveItem[] result = { }; if (_heci.GetCertHashes(out hashes)) { result = new AmtDriveItem[hashes.Length]; for (int i = 0; i < hashes.Length; i++) { result[i] = new AmtHashEntryItem(hashes[i]); } } return result; } } /// /// Definition of a collection of hashes /// internal class AmtHashesItem : AmtDriveItem { AmtHashesContainerValue _value; bool _hasChildren; public AmtHashesItem(HECIClass heci) : base() { _value = new AmtHashesContainerValue(heci); _hasChildren = false; } public override bool IsContainer { get { return true; } } public override bool HasChildren { get { return _hasChildren; } } public override AmtDriveItem[] GetChildItems() { AmtDriveItem[] result = _value.GetChildItems(); _hasChildren = result.Length > 0; return result; } public override string Name { get { return "Hashes"; } } public override object Value { get { return _value; } } } /// /// Container value for Root Heci Items /// internal class AmtHeciContainerValue : ContainerItemValue { HECIClass _heci; public AmtHeciContainerValue(HECIClass heci) { _heci = heci; } public override AmtDriveItem[] GetChildItems() { List list = new List(); bool bOk; string boisString; HECIClass.ProvisioningState state; bOk = _heci.GetProvisioningState(out state); if (bOk) list.Add(new AmtPropertyItem("ProvisioningState", state)); HECIClass.AMT_VERSION_TYPE[] verTypes; bOk = _heci.GetCodeVersions(out boisString, out verTypes); if (bOk) { list.Add(new AmtPropertyItem("BIOSVersion", boisString)); foreach (HECIClass.AMT_VERSION_TYPE verType in verTypes) { list.Add(new AmtPropertyItem(verType.Description.Text, verType.Version.Text)); } } string userName, password; bOk = _heci.GetLocalAdminCredentials(out userName, out password); if (bOk) { list.Add(new AmtPropertyItem("LocalUser", userName)); list.Add(new AmtPropertyItem("LocalPassword", password)); } list.Add(new AmtPropertyItem("LocalAddress", "http://localhost:16992/wsman")); list.Add(new AmtSetupItem(_heci)); list.Add(new AmtHashesItem(_heci)); return list.ToArray(); } } //Container value for Root Heci Items /// /// Definition of the Heci Root Item /// internal class AmtHeciRootItem : AmtDriveItem { AmtHeciContainerValue _value; public AmtHeciRootItem(HECIClass heci) { _value = new AmtHeciContainerValue(heci); } public override bool HasChildren { get { return true; } } public override string Name { get { return HostDriveName; } } public override object Value { get { return _value; } } public static string HostDriveName { get { return "HostDriver"; } } } /// /// Definition a property item /// internal class AmtPropertyItem : AmtDriveItem { string _name; object _value; public AmtPropertyItem(string name, object value) : base() { _name = name; _value = value; } public override string Name { get { return _name; } } public override object Value { get { return _value; } } }*/ } //End AMTDriveProvider } // end Namepsace