Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/KNOWNISSUES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ temporarily from `Microsoft.PowerShell.Commands.Management` because we
cannot resolve `[Shell32.ShellFolderItem]` for FullCLR builds. This must be
fixed ASAP.

## `GetComputerInfoCommand.cs`

The file
`src\Microsoft.PowerShell.Commands.Management\commands\management\GetComputerInfoCommand.cs`
is not currently compiled because it needs resources.

## xUnit

The xUnit tests can only be run on Linux.
Expand Down
2 changes: 2 additions & 0 deletions mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
"src/monad/monad/src/cimSupport/cmdletization/cim/TerminatingErrorTracker.cs": "src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/TerminatingErrorTracker.cs",
"src/monad/monad/src/cimSupport/cmdletization/SessionBasedWrapper.cs": "src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs",
"src/monad/monad/src/commands/management/AddContentCommand.cs": "src/Microsoft.PowerShell.Commands.Management/commands/management/AddContentCommand.cs",
"src/monad/monad/src/commands/management/CIMHelper.cs": "src/Microsoft.PowerShell.Commands.Management/commands/management/CIMHelper.cs",
"src/monad/monad/src/commands/management/ClearContentCommand.cs": "src/Microsoft.PowerShell.Commands.Management/commands/management/ClearContentCommand.cs",
"src/monad/monad/src/commands/management/ClearPropertyCommand.cs": "src/Microsoft.PowerShell.Commands.Management/commands/management/ClearPropertyCommand.cs",
"src/monad/monad/src/commands/management/ClearRecycleBinCommand.cs": "src/Microsoft.PowerShell.Commands.Management/commands/management/ClearRecycleBinCommand.cs",
Expand All @@ -123,6 +124,7 @@
"src/monad/monad/src/commands/management/Eventlog.cs": "src/Microsoft.PowerShell.Commands.Management/commands/management/Eventlog.cs",
"src/monad/monad/src/commands/management/GetChildrenCommand.cs": "src/Microsoft.PowerShell.Commands.Management/commands/management/GetChildrenCommand.cs",
"src/monad/monad/src/commands/management/GetClipboardCommand.cs": "src/Microsoft.PowerShell.Commands.Management/commands/management/GetClipboardCommand.cs",
"src/monad/monad/src/commands/management/GetComputerInfoCommand.cs": "src/Microsoft.PowerShell.Commands.Management/commands/management/GetComputerInfoCommand.cs",
"src/monad/monad/src/commands/management/GetContentCommand.cs": "src/Microsoft.PowerShell.Commands.Management/commands/management/GetContentCommand.cs",
"src/monad/monad/src/commands/management/GetPropertyCommand.cs": "src/Microsoft.PowerShell.Commands.Management/commands/management/GetPropertyCommand.cs",
"src/monad/monad/src/commands/management/GetTransactionCommand.cs": "src/Microsoft.PowerShell.Commands.Management/commands/management/GetTransactionCommand.cs",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,342 @@
using System;
using System.Collections.Generic;
using System.Reflection;

using Microsoft.Management.Infrastructure;

namespace Microsoft.PowerShell.Commands
{
using Extensions;

internal static class CIMHelper
{
internal static class ClassNames
{
internal const string OperatingSystem = "Win32_OperatingSystem";
internal const string PageFileUsage = "Win32_PageFileUsage";
internal const string Bios = "Win32_BIOS";
internal const string BaseBoard = "Win32_BaseBoard";
internal const string ComputerSystem = "Win32_ComputerSystem";
internal const string Keyboard = "Win32_Keyboard";
internal const string DeviceGuard = "Win32_DeviceGuard";
internal const string HotFix = "Win32_QuickFixEngineering";
internal const string MicrosoftNetworkAdapter = "MSFT_NetAdapter";
internal const string NetworkAdapter = "Win32_NetworkAdapter";
internal const string NetworkAdapterConfiguration = "Win32_NetworkAdapterConfiguration";
internal const string Processor = "Win32_Processor";
internal const string PhysicalMemory = "Win32_PhysicalMemory";
internal const string TimeZone = "Win32_TimeZone";
}

internal const string DefaultNamespace = @"root\cimv2";
internal const string DeviceGuardNamespace = @"root\Microsoft\Windows\DeviceGuard";
internal const string MicrosoftNetworkAdapterNamespace = "root/StandardCimv2";
internal const string DefaultQueryDialect = "WQL";

/// <summary>
/// Create a WQL query string to retrieve all properties from
/// the specified WMI class.
/// </summary>
/// <param name="from">A string containing the WMI class name.</param>
/// <returns>
/// A string containing the WQL query string
/// </returns>
internal static string WqlQueryAll(string from)
{
return "SELECT * from " + from;
}

/// <summary>
/// Retrieve a new object of type T, whose properties and fields are
/// populated from an instance of the named WMI class. If the CIM
/// query results in multiple instances, only the first instance is
/// returned.
/// </summary>
/// <typeparam name="T">
/// The type of the object to be created. Must be a default-constructable
/// reference type.
/// </typeparam>
/// <param name="session">
/// The CIM session to be queried.
/// </param>
/// <param name="nameSpace">
/// A string containing the namespace to run the query against
/// </param>
/// <param name="wmiClassName">
/// A string containing the name of the WMI class from which to populate
/// the resultant object.
/// </param>
/// <returns>
/// A new object of type T if successful, null otherwise.
/// </returns>
/// <remarks>
/// This method matches property and field names of type T with identically
/// named properties in the WMI class instance. The WMI property is converted
/// to the type of T's property or field.
/// </remarks>
internal static T GetFirst<T>(CimSession session, string nameSpace, string wmiClassName) where T : class, new()
{
if (string.IsNullOrEmpty(wmiClassName))
throw new ArgumentException("String argument may not be null or empty", "wmiClassName");

try
{
var type = typeof(T);
var binding = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
T rv = new T();

using (var instance = session.QueryFirstInstance(nameSpace, CIMHelper.WqlQueryAll(wmiClassName)))
{
SetObjectDataMembers(rv, binding, instance);
}

return rv;
}
catch (Exception /*ex*/)
{
// on any error fall through to the null return below
}

return null;
}

/// <summary>
/// Retrieve an array of new objects of type T, whose properties and fields are
/// populated from an instance of the specified WMI class on the specified CIM
/// session.
/// </summary>
/// <typeparam name="T">
/// The type of the object to be created. Must be a default-constructable
/// reference type.
/// </typeparam>
/// <param name="session">
/// The CIM session to be queried.
/// </param>
/// <param name="nameSpace">
/// A string containing the namespace to run the query against
/// </param>
/// <param name="wmiClassName">
/// A string containing the name of the WMI class from which to populate
/// the resultant array elements.
/// </param>
/// <returns>
/// An array of new objects of type T if successful, null otherwise.
/// </returns>
/// <remarks>
/// This method matches property and field names of type T with identically
/// named properties in the WMI class instance. The WMI property is converted
/// to the type of T's property or field.
/// </remarks>
internal static T[] GetAll<T>(CimSession session, string nameSpace, string wmiClassName) where T : class, new()
{
if (string.IsNullOrEmpty(wmiClassName))
throw new ArgumentException("String argument may not be null or empty", "wmiClassName");

var rv = new List<T>();

try
{
var instances = session.QueryInstances(nameSpace, CIMHelper.WqlQueryAll(wmiClassName));

if (instances != null)
{
var type = typeof(T);
var binding = BindingFlags.Public | BindingFlags.Instance;

foreach (var instance in instances)
{
T objT = new T();

using (instance)
{
SetObjectDataMembers(objT, binding, instance);
}

rv.Add(objT);
}
}
}
catch (Exception /*ex*/)
{
// on any error we'll just fall through to the return below
}

return rv.ToArray();
}

/// <summary>
/// Retrieve an array of new objects of type T, whose properties and fields are
/// populated from an instance of the specified WMI class on the specified CIM
/// session.
/// </summary>
/// <typeparam name="T">
/// The type of the object to be created. Must be a default-constructable
/// reference type.
/// </typeparam>
/// <param name="session">
/// The CIM session to be queried.
/// </param>
/// <param name="wmiClassName">
/// A string containing the name of the WMI class from which to populate
/// the resultant array elements.
/// </param>
/// <returns>
/// An array of new objects of type T if successful, null otherwise.
/// </returns>
/// <remarks>
/// This method matches property and field names of type T with identically
/// named properties in the WMI class instance. The WMI property is converted
/// to the type of T's property or field.
/// </remarks>
internal static T[] GetAll<T>(CimSession session, string wmiClassName) where T : class, new()
{
return GetAll<T>(session, DefaultNamespace, wmiClassName);
}

internal static void SetObjectDataMember(object obj, BindingFlags binding, CimProperty cimProperty)
{
var type = obj.GetType();

var pi = type.GetProperty(cimProperty.Name, binding);

if (pi != null && pi.CanWrite)
{
pi.SetValue(obj, cimProperty.Value, null);
}
else
{
var fi = type.GetField(cimProperty.Name, binding);

if (fi != null && !fi.IsInitOnly)
{
fi.SetValue(obj, cimProperty.Value);
}
}
}

internal static void SetObjectDataMembers(object obj, BindingFlags binding, CimInstance instance)
{
foreach (var wmiProp in instance.CimInstanceProperties)
SetObjectDataMember(obj, binding, wmiProp);
}

/// <summary>
/// Escape any backslash (\) characters in a path with an additional
/// backslash, allowing the path to be used within a WMI query.
/// </summary>
/// <param name="path">
/// A string that may contain backslash characters.
/// </param>
/// <returns>
/// A new string in which any backshlash characters have been "escaped"
/// by prefacing then with an additional backslash
/// </returns>
internal static string EscapePath(string path)
{
return string.Join(@"\\", path.Split('\\'));
}
}
}

namespace Extensions
{
using Microsoft.PowerShell.Commands;

internal static class CIMExtensions
{
/// <summary>
/// An "overload" of the
/// <see cref="Microsoft.Management.Infrastructure.CimSession"/>.QueryInstances
/// method that takes only the query string as a parameter
/// </summary>
/// <param name="session">The CimSession to be queried</param>
/// <param name="query">A string containing the query to be run</param>
/// <returns>
/// An IEnumerable interface that can be used to enumerate the instances
/// </returns>
internal static IEnumerable<CimInstance> QueryInstances(this CimSession session, string query)
{
return session.QueryInstances(CIMHelper.DefaultNamespace, CIMHelper.DefaultQueryDialect, query);
}

/// <summary>
/// An "overload" of the
/// <see cref="Microsoft.Management.Infrastructure.CimSession"/>.QueryInstances
/// method that takes only the namespace and query string as a parameters
/// </summary>
/// <param name="session">The CimSession to be queried</param>
/// <param name="nameSpace">A string containing the namespace to run the query against</param>
/// <param name="query">A string containing the query to be run</param>
/// <returns>
/// An IEnumerable interface that can be used to enumerate the instances
/// </returns>
internal static IEnumerable<CimInstance> QueryInstances(this CimSession session, string nameSpace, string query)
{
return session.QueryInstances(nameSpace, CIMHelper.DefaultQueryDialect, query);
}

/// <summary>
/// Execute a CIM query and return only the first intance in the result.
/// </summary>
/// <param name="session">The CimSesson to be queried</param>
/// <param name="nameSpace">A string containing the namespace to run the query against</param>
/// <param name="query">A string containing the query to be run</param>
/// <returns>
/// A <see cref="Microsoft.Management.Infrastructure.CimInstance"/> object
/// representing the first instance in a query result if successful, null
/// otherwise.
/// </returns>
internal static CimInstance QueryFirstInstance(this CimSession session, string nameSpace, string query)
{
try
{
var instances = session.QueryInstances(nameSpace, query);
var enumerator = instances.GetEnumerator();

if (enumerator.MoveNext())
return enumerator.Current;
}
catch (Exception /*ex*/)
{
// on any error, fall through to the null return below
}

return null;
}

/// <summary>
/// Execute a CIM query and return only the first intance in the result.
/// </summary>
/// <param name="session">The CimSesson to be queried</param>
/// <param name="query">A string containing the query to be run</param>
/// <returns>
/// A <see cref="Microsoft.Management.Infrastructure.CimInstance"/> object
/// representing the first instance in a query result if successful, null
/// otherwise.
/// </returns>
internal static CimInstance QueryFirstInstance(this CimSession session, string query)
{
return session.QueryFirstInstance(CIMHelper.DefaultNamespace, query);
}

internal static T GetFirst<T>(this CimSession session, string wmiClassName) where T : class, new()
{
return session.GetFirst<T>(CIMHelper.DefaultNamespace, wmiClassName);
}

internal static T GetFirst<T>(this CimSession session, string wmiNamespace, string wmiClassName) where T : class, new()
{
return CIMHelper.GetFirst<T>(session, wmiNamespace, wmiClassName);
}

internal static T[] GetAll<T>(this CimSession session, string wmiClassName) where T : class, new()
{
return Microsoft.PowerShell.Commands.CIMHelper.GetAll<T>(session, wmiClassName);
}

internal static T[] GetAll<T>(this CimSession session, string wmiNamespace, string wmiClassName) where T : class, new()
{
return Microsoft.PowerShell.Commands.CIMHelper.GetAll<T>(session, wmiNamespace, wmiClassName);
}
}
}
Loading