PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/GetFIMResource.cs

#
C# | 127 lines | 112 code | 15 blank | 0 comment | 10 complexity | 3f52de94fed2ee4f993c60b17f0858a9 MD5 | raw file
  1. using System;
  2. using System.Linq;
  3. using System.Management.Automation;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using Microsoft.ResourceManagement.Client;
  7. using Microsoft.ResourceManagement.ObjectModel;
  8. namespace Quest.FIMPowerShellSnapin
  9. {
  10. [Cmdlet(VerbsCommon.Get, Constants.Nouns.FIMResource)]
  11. public class GetFIMResource : PSCmdlet
  12. {
  13. private FIMPSSession _session;
  14. private String _objectID;
  15. private String _filter;
  16. private int _resultSetSize;
  17. private String[] _attributes;
  18. private String _locale;
  19. public GetFIMResource()
  20. {
  21. _resultSetSize = Constants.DefaultResultSetSize;
  22. }
  23. [Parameter(Mandatory = true, ParameterSetName = "ObjectID", Position = 0, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
  24. [ValidateNotNullOrEmpty]
  25. public String ObjectID
  26. {
  27. get { return _objectID; }
  28. set { _objectID = value; }
  29. }
  30. [Parameter(ParameterSetName = "Filter")]
  31. [ValidateNotNullOrEmpty]
  32. public String Filter
  33. {
  34. get { return _filter; }
  35. set { _filter = value; }
  36. }
  37. [Parameter(ParameterSetName = "Filter")]
  38. [ValidateRange(0, int.MaxValue)]
  39. public int ResultSetSize
  40. {
  41. get { return _resultSetSize; }
  42. set { _resultSetSize = value; }
  43. }
  44. [Parameter]
  45. [ValidateNotNullOrEmpty]
  46. public String[] Attributes
  47. {
  48. get { return _attributes; }
  49. set { _attributes = value; }
  50. }
  51. [Parameter]
  52. [ValidateNotNullOrEmpty]
  53. public String Locale
  54. {
  55. get { return _locale; }
  56. set { _locale = value; }
  57. }
  58. [Parameter(Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
  59. [ValidateNotNullOrEmpty]
  60. public FIMPSSession Session
  61. {
  62. get { return _session; }
  63. set { _session = value; }
  64. }
  65. protected override void ProcessRecord()
  66. {
  67. if (_attributes == null || _attributes.Count() == 0)
  68. {
  69. _attributes = GetDefaultAttributes();
  70. }
  71. else
  72. {
  73. if (_attributes.Count() == 1 && _attributes[0].ToUpperInvariant() == Constants.AttributeSpecialValueAll)
  74. {
  75. _attributes = null;
  76. }
  77. else
  78. {
  79. _attributes = _attributes.Union(GetDefaultAttributes()).ToArray();
  80. }
  81. }
  82. switch (ParameterSetName)
  83. {
  84. case "ObjectID":
  85. RmReference id = new RmReference(ObjectID);
  86. CultureInfo cultureInfo = (_locale != null) ? new CultureInfo(_locale, false) : null;
  87. RmResource specificResource = _session.Client.Get(id, cultureInfo, _attributes);
  88. WriteObject(new FIMPSResource(specificResource, _session));
  89. break;
  90. case "Filter":
  91. foreach(RmResource resource in _session.Client.Enumerate(_filter, _attributes))
  92. {
  93. WriteObject(new FIMPSResource(resource, _session));
  94. }
  95. break;
  96. default:
  97. throw new Exception(Constants.Messages.InternalError_UnknownParameterSet);
  98. }
  99. }
  100. private string[] GetDefaultAttributes()
  101. {
  102. List<string> attributeNames = _session.Client.Schema.AllAttributesNames("Resource");
  103. string[] namesToReturn = new string[attributeNames.Count];
  104. int i = 0;
  105. foreach(string attributeName in attributeNames)
  106. {
  107. namesToReturn[i++] = attributeName;
  108. }
  109. return namesToReturn;
  110. }
  111. }
  112. }