Created
August 29, 2016 00:37
-
-
Save botmtl/a0726dfb8a9a7c900747524d49858bd1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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