Skip to content

Instantly share code, notes, and snippets.

@botmtl
Created August 29, 2016 00:37
Show Gist options
  • Save botmtl/a0726dfb8a9a7c900747524d49858bd1 to your computer and use it in GitHub Desktop.
Save botmtl/a0726dfb8a9a7c900747524d49858bd1 to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using DeviceManagementLib;
using DeviceManagementLib.Win32;
namespace DeviceManagementModule
{
public class DeviceCmdletBase : PSCmdlet
{
protected DeviceClass? @class;
protected IEnumerable<Device> devices = null;
protected string instanceId;
protected string name;
protected IEnumerable<Device> GetDevice()
{
IEnumerable<Device> source = null;
if (!string.IsNullOrEmpty(instanceId) && @class.HasValue)
source = DeviceManagementLib.DeviceManagementLib.GetDevices(instanceId, @class);
else if (!string.IsNullOrEmpty(instanceId) && [email protected])
source = DeviceManagementLib.DeviceManagementLib.GetDevices(instanceId, new DeviceClass?());
else if (string.IsNullOrEmpty(instanceId) && @class.HasValue)
source = DeviceManagementLib.DeviceManagementLib.GetDevices("", @class);
else if (string.IsNullOrEmpty(instanceId) && [email protected])
source = DeviceManagementLib.DeviceManagementLib.GetDevices("", new DeviceClass?());
if (!string.IsNullOrEmpty(name))
source = source.Where(d =>
{
if (!string.IsNullOrEmpty(d.Name))
return d.Name.ToLower() == name.ToLower();
return false;
});
if ((!string.IsNullOrEmpty(name) | !string.IsNullOrEmpty(instanceId)) && (source.Count() == 0))
throw new DeviceNotFoundException();
return source;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment