PageRenderTime 7ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 0ms app.codeStats 1ms

/Source/GetFIMResource.cs

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