Src/Presentation.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
using System;
using System.IO;
using System.Xml;
using System.Globalization;
using System.Reflection;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Presentation;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Configuration;
using Microsoft.EnterpriseManagement.Configuration.IO;
using System.Text.RegularExpressions;

namespace SMLets
{
    public class PresentationCmdletBase : SMCmdletBase
    {
        private string[] _name = { "*" };
        [Parameter(Position = 0)]
        public string[] Name
        {
            get { return _name; }
            set { _name = value; }
        }
    }

    #region SCSMView cmdlets

    [Cmdlet(VerbsCommon.New, "SCSMView")]
    public class NewSMViewCommand : ObjectCmdletHelper
    {
        #region parameters

        private ManagementPackFolder _Folder = null;
        private String _DisplayName = null;
        private String _Criteria = null;
        private ManagementPackClass _Class = null;
        private ManagementPack _ManagementPack = null;
        private SwitchParameter _PassThru;
        private ManagementPackTypeProjection _Projection;
        private Column[] _columns;
        private ManagementPackImage _image;
        private KeyValuePair<string, ManagementPackReference>[] _ManagementPackReferences;

        [Parameter(Position = 0,
        Mandatory = true,
        ValueFromPipelineByPropertyName = true,
        HelpMessage = "The folder of the view.")]

        public ManagementPackFolder Folder
        {
            get { return _Folder; }
            set { _Folder = value; }
        }

        [Parameter(Position = 1,
        Mandatory = false,
        ValueFromPipelineByPropertyName = true,
        HelpMessage = "The display name of the view.")]

        public String DisplayName
        {
            get { return _DisplayName; }
            set { _DisplayName = value; }
        }

        [Parameter(Position = 2,
        Mandatory = false,
        ValueFromPipelineByPropertyName = true,
        HelpMessage = "The target class of the view.")]

        public ManagementPackClass Class
        {
            get { return _Class; }
            set { _Class = value; }
        }


        [Parameter(Position = 3,
        Mandatory = false,
        ValueFromPipelineByPropertyName = true,
        HelpMessage = "The management pack to store the view in.")]

        public ManagementPack ManagementPack
        {
            get { return _ManagementPack; }
            set { _ManagementPack = value; }
        }

        [Parameter(Position = 4,
        Mandatory = true,
        ValueFromPipelineByPropertyName = true,
        HelpMessage = "The view criteria.")]

        public String Criteria
        {
            get { return _Criteria; }
            set { _Criteria = value; }
        }

        [Parameter(Position = 5,
        Mandatory = false,
        ValueFromPipelineByPropertyName = false,
        HelpMessage = "The columns to show.")]

        public Column[] Columns
        {
            get { return _columns; }
            set { _columns = value; }
        }

        [Parameter(Position = 6, 
        Mandatory = false,
        ValueFromPipeline = false,
        HelpMessage = "The tree image for the view")]
        public ManagementPackImage Image
        {
            get { return _image; }
            set { _image = value; }
        }

        [Parameter(Position = 7,
        Mandatory = false,
        ValueFromPipeline = false,
        HelpMessage = "The management pack references to add to be used in the criteria XML")]
        public KeyValuePair<string,ManagementPackReference>[] ManagementPackReference
        {
            get { return _ManagementPackReferences; }
            set { _ManagementPackReferences = value; }
        }

        [Parameter(Position = 8,
        Mandatory = false,
        ValueFromPipeline = false,
        HelpMessage = "The type projection to be used.")]

        public ManagementPackTypeProjection Projection
        {
            get { return _Projection; }
            set { _Projection = value; }
        }

        [Parameter]
        public SwitchParameter PassThru
        {
            get { return _PassThru; }
            set { _PassThru = value; }
        }

        #endregion

        protected override void BeginProcessing()
        {
            base.BeginProcessing();
        }

        protected override void ProcessRecord()
        {
            try
            {
                base.ProcessRecord();

                ManagementPackView view = new ManagementPackView(ManagementPack, SMHelpers.MakeMPElementSafeUniqueIdentifier("View"), ManagementPackAccessibility.Internal);
                view.DisplayName = _DisplayName;     //Set the display name according to what the user specified
                view.Target = _Class;                //Set the class according to what the user specified

                //TODO: Parameterize these later
                view.Visible = true;
                view.Accessibility = ManagementPackAccessibility.Public;
                view.Enabled = true;
                view.Category = "NotUsed";

                //Set the parent folder that was passed in
                ManagementPackFolderItem folderitem = new ManagementPackFolderItem(view, _Folder);

                //Add the management pack references to the MP
                if(_ManagementPackReferences != null)
                {
                    foreach (KeyValuePair<string,ManagementPackReference> kvp in _ManagementPackReferences)
                    {
                        _ManagementPack.References.Add(kvp);
                    }
                }

                //Get the Grid view type and set it for the view
                ManagementPack mpConsole = SMHelpers.GetManagementPack(ManagementPacks.Microsoft_EnterpriseManagement_ServiceManager_UI_Console, _mg);
                view.TypeID = mpConsole.GetViewType("GridViewType");

                #region DataAdapters

                DataAdapter daEMO = new DataAdapter();
                DataAdapter daAdvancedList = new DataAdapter();

                if (_Projection != null)
                {
                    daEMO.Name = "dataportal:EnterpriseManagementObjectAdapter";
                    daEMO.Type = "Microsoft.EnterpriseManagement.UI.SdkDataAccess.DataAdapters.EnterpriseManagementObjectProjectionAdapter";
                }
                else
                {
                    daEMO.Name = "dataportal:EnterpriseManagementObjectProjectionAdapter";
                    daEMO.Type = "Microsoft.EnterpriseManagement.UI.SdkDataAccess.DataAdapters.EnterpriseManagementObjectAdapter";
                }

                daEMO.Assembly = "Microsoft.EnterpriseManagement.UI.SdkDataAccess";

                daAdvancedList.Name = "viewframework://Adapters/AdvancedList";
                daAdvancedList.Assembly = "Microsoft.EnterpriseManagement.UI.ViewFramework";
                daAdvancedList.Type = "Microsoft.EnterpriseManagement.UI.ViewFramework.AdvancedListSupportAdapter";

                Collection<DataAdapter> collDataAdpaters = new Collection<DataAdapter>();
                collDataAdpaters.Add(daEMO);
                collDataAdpaters.Add(daAdvancedList);

                #endregion DataAdapters

                view.Configuration = CreateViewConfiguration(collDataAdpaters, _columns);

                foreach (Column column in _columns)
                {
                    ManagementPackStringResource mpsr = new ManagementPackStringResource(view.GetManagementPack(), column.DisplayNameId);
                    mpsr.DisplayName = column.DisplayNameString;
                }

                //Set the image
                if (_image != null)
                {
                    ManagementPackElementReference<ManagementPackImage> viewIconReference = (ManagementPackElementReference<ManagementPackImage>)_mg.Resources.GetResource<ManagementPackImage>(_image.Name,_image.GetManagementPack());
                    ManagementPackImageReference imageref = new ManagementPackImageReference(view, viewIconReference, view.GetManagementPack());
                }

                view.GetManagementPack().AcceptChanges();

                if (PassThru)
                {
                    //Pass the new object to the pipeline
                    WriteObject(view);
                }
            }
            catch (Exception ex)
            {
                WriteError(new ErrorRecord(ex, "NewView", ErrorCategory.InvalidOperation, DisplayName));
            }
        }

        protected override void EndProcessing()
        {
            base.EndProcessing();
        }

        private string CreateViewConfiguration(Collection<DataAdapter> collDataAdapters, Column[] collColumns)
        {
            XmlDocument xmldoc = new XmlDocument();
            XmlElement elConfiguration = xmldoc.CreateElement("Configuration");
            xmldoc.AppendChild(elConfiguration);

            CreateDataXmlNode(ref xmldoc, collDataAdapters);

            CreatePresentationXmlNode(ref xmldoc, collColumns);

            StringWriter sw = new StringWriter();
            XmlTextWriter tw = new XmlTextWriter(sw);

            xmldoc.WriteTo(tw);

            string strConfiguration = sw.ToString();

            //Strip off the dummy document element.            
            strConfiguration = strConfiguration.Replace("<Configuration>", "");
            strConfiguration = strConfiguration.Replace("</Configuration>", "");

            return (strConfiguration);
        }

        private void CreateDataXmlNode(ref XmlDocument xmldoc, Collection<DataAdapter> collDataAdapters)
        {
            XmlElement elData = xmldoc.CreateElement("Data");
            xmldoc.DocumentElement.AppendChild(elData);  //Add as child of "Configuration" document element for now.
            XmlElement elAdapters = xmldoc.CreateElement("Adapters");

            foreach (DataAdapter da in collDataAdapters)
            {
                XmlElement xmlnodeDataAdapter = CreateDataAdapterNode(ref xmldoc, da);
                elAdapters.AppendChild(xmlnodeDataAdapter);
            }

            elData.AppendChild(elAdapters);

            XmlElement elItemsSource = xmldoc.CreateElement("ItemsSource");
            XmlElement elAdvancedListSupportClass = xmldoc.CreateElement("AdvancedListSupportClass");

            //AdvancedListSupportClass attributes
            XmlAttribute attrXmlnsALSC = xmldoc.CreateAttribute("xmlns");
            XmlAttribute attrXmlnsAvALSC = xmldoc.CreateAttribute("xmlns:av");
            XmlAttribute attrXmlnsXALSC = xmldoc.CreateAttribute("xmlns:x");
            XmlAttribute attrXmlnsSALSC = xmldoc.CreateAttribute("xmlns:s");
            XmlAttribute attrDataTypeName = xmldoc.CreateAttribute("DataTypeName");
            XmlAttribute attrAdapterName = xmldoc.CreateAttribute("AdapterName");
            XmlAttribute attrFullUpdateAdapter = xmldoc.CreateAttribute("FullUpdateAdapter");
            XmlAttribute attrDataSource = xmldoc.CreateAttribute("DataSource");
            XmlAttribute attrFullUpdateFrequency = xmldoc.CreateAttribute("FullUpdateFrequency");
            XmlAttribute attrStreaming = xmldoc.CreateAttribute("Streaming");
            XmlAttribute attrIsRecurring = xmldoc.CreateAttribute("IsRecurring");
            XmlAttribute attrRecurrenceFrequency = xmldoc.CreateAttribute("RecurrenceFrequency");

            //TODO: Parameterize these someday
            attrXmlnsALSC.Value = "clr-namespace:Microsoft.EnterpriseManagement.UI.ViewFramework;assembly=Microsoft.EnterpriseManagement.UI.ViewFramework";
            attrXmlnsAvALSC.Value = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
            attrXmlnsXALSC.Value = "http://schemas.microsoft.com/winfx/2006/xaml";
            attrXmlnsSALSC.Value = "clr-namespace:System;assembly=mscorlib";
            attrDataTypeName.Value = "";
            attrAdapterName.Value = "viewframework://Adapters/AdvancedList";
            attrDataSource.Value = "mom:ManagementGroup";
            attrFullUpdateFrequency.Value = "1";
            attrStreaming.Value = "true";
            attrIsRecurring.Value = "true";
            attrRecurrenceFrequency.Value = "{x:Static s:Int32.MaxValue}";
            if(_Projection != null)
                attrFullUpdateAdapter.Value = "dataportal:EnterpriseManagementObjectProjectionAdapter";
            else
                attrFullUpdateAdapter.Value = "dataportal:EnterpriseManagementObjectAdapter";

            elAdvancedListSupportClass.Attributes.Append(attrXmlnsALSC);
            elAdvancedListSupportClass.Attributes.Append(attrXmlnsAvALSC);
            elAdvancedListSupportClass.Attributes.Append(attrXmlnsSALSC);
            elAdvancedListSupportClass.Attributes.Append(attrXmlnsXALSC);
            elAdvancedListSupportClass.Attributes.Append(attrDataTypeName);
            elAdvancedListSupportClass.Attributes.Append(attrAdapterName);
            elAdvancedListSupportClass.Attributes.Append(attrFullUpdateAdapter);
            elAdvancedListSupportClass.Attributes.Append(attrDataSource);
            elAdvancedListSupportClass.Attributes.Append(attrFullUpdateFrequency);
            elAdvancedListSupportClass.Attributes.Append(attrStreaming);
            elAdvancedListSupportClass.Attributes.Append(attrIsRecurring);
            elAdvancedListSupportClass.Attributes.Append(attrRecurrenceFrequency);

            XmlElement elAdvancedListSupportClassParameters = xmldoc.CreateElement("AdvancedListSupportClass.Parameters");

            XmlElement elQueryParameter = xmldoc.CreateElement("QueryParameter");

            XmlAttribute attrParameter = xmldoc.CreateAttribute("Parameter");
            XmlAttribute attrValue = xmldoc.CreateAttribute("Value");

            ManagementPackElement element;

            if (_Projection != null)
            {
                attrParameter.Value = "TypeProjectionId";
                element = _Projection;
            }
            else
            {
                attrParameter.Value = "ManagementPackClassId";
                element = _Class;
            }

            ManagementPack elementMP = element.GetManagementPack();
            
            string strMPAlias = string.Empty;

            if (elementMP != _ManagementPack)
            {
                //If the class/type projection is not in the same management pack as where we are creating the view then try to either use an existing MP reference or create a new one
                foreach (KeyValuePair<string,ManagementPackReference> mpref in _ManagementPack.References)
                {
                    if (mpref.Value.Id == elementMP.Id)
                    {
                        //Found an existing reference so just use that alias
                        strMPAlias = mpref.Key;
                        break;
                    }
                }
                
                if (strMPAlias == string.Empty)
                {
                    //We didnt find the MP reference in the set of existing references, let's create one.. strMPAlias = elementMP.Name.Replace(".", "_"); ManagementPackReference mpref = new ManagementPackReference(elementMP); _ManagementPack.References.Add(strMPAlias, mpref); } } if(strMPAlias == string.Empty) attrValue.Value = String.Format("$MPElement[Name='{0}']$", element.Name);
            else
                attrValue.Value = String.Format("$MPElement[Name='
{0}!{1}']$", strMPAlias, element.Name);
 
            elQueryParameter.Attributes.Append(attrParameter);
            elQueryParameter.Attributes.Append(attrValue);
 
            elAdvancedListSupportClassParameters.AppendChild(elQueryParameter);
 
            elAdvancedListSupportClass.AppendChild(elAdvancedListSupportClassParameters);
 
            elItemsSource.AppendChild(elAdvancedListSupportClass);
 
            elData.AppendChild(elItemsSource);
 
            XmlDocumentFragment fragCriteria = xmldoc.CreateDocumentFragment();
            fragCriteria.InnerXml = _Criteria;
 
            elData.AppendChild(fragCriteria);
        }
 
        private void CreatePresentationXmlNode(ref XmlDocument xmldoc, Column[] collColumns)
        {
            XmlElement elPresentation = xmldoc.CreateElement("Presentation");
            xmldoc.DocumentElement.AppendChild(elPresentation); //Add as child of "Configuration" document element for now.
            XmlElement elColumns = xmldoc.CreateElement("Columns");
            XmlElement elViewStrings = xmldoc.CreateElement("ViewStrings");
 
            XmlElement elColumnCollection = xmldoc.CreateElement("mux", "ColumnCollection", "http://schemas.microsoft.com/SystemCenter/Common/UI/Views/GridView");
 
            XmlAttribute attrXmlnsCC = xmldoc.CreateAttribute("xmlns");
            attrXmlnsCC.Value = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
             
            XmlAttribute attrXmlnsSCC = xmldoc.CreateAttribute("xmlns:s");
            attrXmlnsSCC.Value = "clr-namespace:System;assembly=mscorlib";
             
            XmlAttribute attrXmlnsXCC = xmldoc.CreateAttribute("xmlns:x");
            attrXmlnsXCC.Value = "http://schemas.microsoft.com/winfx/2006/xaml";
             
            XmlAttribute attrXmlnsDataCC = xmldoc.CreateAttribute("xmlns:data");
            attrXmlnsDataCC.Value = "clr-namespace:Microsoft.EnterpriseManagement.UI.SdkDataAccess.Common;assembly=Microsoft.EnterpriseManagement.UI.SdkDataAccess";
 
            elColumnCollection.Attributes.Append(attrXmlnsCC);
            elColumnCollection.Attributes.Append(attrXmlnsSCC);
            elColumnCollection.Attributes.Append(attrXmlnsXCC);
            elColumnCollection.Attributes.Append(attrXmlnsDataCC);
 
            foreach (Column column in collColumns)
            {
                XmlElement elColumn = CreateColumnNode(ref xmldoc, column);
                elColumnCollection.AppendChild(elColumn);
 
                XmlElement elViewString = CreateViewStringNode(ref xmldoc, column);
                elViewStrings.AppendChild(elViewString);
            }
 
            elColumns.AppendChild(elColumnCollection);
            elPresentation.AppendChild(elColumns);
 
            elPresentation.AppendChild(elViewStrings);
        }
 
        private XmlElement CreateDataAdapterNode(ref XmlDocument xmldoc, DataAdapter da)
        {
 
            XmlElement elAdapter = xmldoc.CreateElement("Adapter");
 
            XmlAttribute attrAdapterName = xmldoc.CreateAttribute("AdapterName");
            attrAdapterName.Value = da.Name;
            elAdapter.Attributes.Append(attrAdapterName);
 
            XmlElement elAdapterAssembly = xmldoc.CreateElement("AdapterAssembly");
            elAdapterAssembly.InnerText = da.Assembly;
 
            XmlElement elAdapterType = xmldoc.CreateElement("AdapterType");
            elAdapterType.InnerText = da.Type;
 
            elAdapter.AppendChild(elAdapterAssembly);
            elAdapter.AppendChild(elAdapterType);
 
            return (elAdapter);
        }
 
        private XmlElement CreateColumnNode(ref XmlDocument xmldoc, Column column)
        {
            XmlElement elColumn = xmldoc.CreateElement("mux","Column","http://schemas.microsoft.com/SystemCenter/Common/UI/Views/GridView");
 
            XmlAttribute attrName = xmldoc.CreateAttribute("Name");
            XmlAttribute attrDisplayMemberBinding = xmldoc.CreateAttribute("DisplayMemberBinding");
            XmlAttribute attrWidth = xmldoc.CreateAttribute("Width");
            XmlAttribute attrDisplayNameId = xmldoc.CreateAttribute("DisplayName");
            XmlAttribute attrProperty = xmldoc.CreateAttribute("Property");
            XmlAttribute attrDataType = xmldoc.CreateAttribute("DataType");
 
            attrName.Value = column.Name;
            attrDisplayMemberBinding.Value = column.DisplayMemberBinding;
            attrWidth.Value = column.Width;
            attrDisplayNameId.Value = column.DisplayNameId;
            attrProperty.Value = column.Property;
            attrDataType.Value = column.DataType;
 
            elColumn.Attributes.Append(attrName);
            elColumn.Attributes.Append(attrDisplayMemberBinding);
            elColumn.Attributes.Append(attrWidth);
            elColumn.Attributes.Append(attrDisplayNameId);
            elColumn.Attributes.Append(attrProperty);
            elColumn.Attributes.Append(attrDataType);
 
            return (elColumn);
        }
 
        private XmlElement CreateViewStringNode(ref XmlDocument xmldoc, Column column)
        {
            XmlElement elViewString = xmldoc.CreateElement("ViewString");
            XmlAttribute attrId = xmldoc.CreateAttribute("ID");
            attrId.Value = column.DisplayNameId;
            elViewString.Attributes.Append(attrId);
            elViewString.InnerText = "$MPElement[Name=\"" + column.DisplayNameId + "\"]$";
            return (elViewString);
        }
    }
 
    [Cmdlet(VerbsCommon.Get, "SCSMView")]
    public class GetSCSMViewCommand : PresentationCmdletBase
    {
        private IList<ManagementPackView> list;
        protected override void BeginProcessing()
        {
            base.BeginProcessing();
            list = _mg.Presentation.GetViews();
        }
        protected override void ProcessRecord()
        {
            foreach (string p in Name)
            {
                WildcardPattern pattern = new WildcardPattern(p, WildcardOptions.CultureInvariant|WildcardOptions.IgnoreCase);
                foreach (ManagementPackView v in list)
                {
                    if (pattern.IsMatch(v.Name))
                    {
                        WriteObject(v);
                    }
                }
            }
        }
    }
 
    [Cmdlet(VerbsCommon.Remove, "SCSMView", SupportsShouldProcess = true)]
    public class RemoveSCSMView : PresentationCmdletBase
    {
        private ManagementPackView _view;
 
        [Parameter(Mandatory = true, ValueFromPipeline = true)]
        public ManagementPackView View
        {
            get { return _view; }
            set { _view = value; }
        }
 
        protected override void ProcessRecord()
        {
            base.ProcessRecord();
 
            ManagementPackView view = _mg.Presentation.GetView(_view.Id);
            ManagementPack mp = view.GetManagementPack();
            view.Status = ManagementPackElementStatus.PendingDelete;
            string viewInfo = view.Name;
            if(view.DisplayName != null)
            {
                viewInfo = view.DisplayName;
            }
 
            if(ShouldProcess(viewInfo))
            {
                mp.AcceptChanges();
            }
        }
    }
 
    #endregion
 
    #region SCSMFolder cmdlets
 
    [Cmdlet(VerbsCommon.Get, "SCSMFolder")]
    public class GetSCSMFolderCommand : PresentationCmdletBase
    {
        private IList<ManagementPackFolder> list;
        protected override void BeginProcessing()
        {
            base.BeginProcessing();
            list = _mg.Presentation.GetFolders();
        }
        protected override void ProcessRecord()
        {
            foreach (string p in Name)
            {
                WildcardPattern pattern = new WildcardPattern(p, WildcardOptions.CultureInvariant | WildcardOptions.IgnoreCase);
                foreach (ManagementPackFolder v in list)
                {
                    if (pattern.IsMatch(v.Name))
                    {
                        WriteObject(v);
                    }
                }
            }
        }
    }
 
    [Cmdlet(VerbsCommon.New, "SCSMFolder")]
    public class NewSMFolderCommand : ObjectCmdletHelper
    {
        private string _displayname;
        ManagementPackFolder _parentfolder;
        ManagementPack _managementpack;
        //TODO: Add support for this someday
        //ManagementPackImage _image;
 
        [Parameter(ValueFromPipeline = false, Mandatory = true)]
        public string DisplayName
        {
            get { return _displayname; }
            set { _displayname = value; }
        }
 
        [Parameter(ValueFromPipeline = false, Mandatory = true)]
        public ManagementPackFolder ParentFolder
        {
            get { return _parentfolder; }
            set { _parentfolder = value; }
        }
 
        [Parameter(ValueFromPipeline = false, Mandatory = true)]
        public ManagementPack ManagementPack
        {
            get { return _managementpack; }
            set { _managementpack = value; }
        }
 
        //TODO: Add support for this at some point
        /*
        [Parameter(ValueFromPipeline = false, Mandatory = false)]
        public ManagementPackImage Image
        {
            get { return _image; }
            set { _image = value; }
        }
        */
 
        protected override void ProcessRecord()
        {
            base.ProcessRecord();
 
            //Create a new folder and set it'
s parent folder and display name
            ManagementPackFolder folder = new ManagementPackFolder(_managementpack, SMHelpers.MakeMPElementSafeUniqueIdentifier("Folder"), ManagementPackAccessibility.Public);
            folder.DisplayName = _displayname;
            folder.ParentFolder = _parentfolder;

            //TODO: Parameterize this someday
            //Set the systemfolder icon to be the icon that is used
            ManagementPackElementReference<ManagementPackImage> foldericonreference = (ManagementPackElementReference<ManagementPackImage>)_mg.Resources.GetResource<ManagementPackImage>(Images.Microsoft_EnterpriseManagement_ServiceManager_UI_Console_Image_Folder, SMHelpers.GetManagementPack(ManagementPacks.Microsoft_EnterpriseManagement_ServiceManager_UI_Console, _mg));
            ManagementPackImageReference image = new ManagementPackImageReference(folder, foldericonreference, _managementpack);

            //Submit changes
            _managementpack.AcceptChanges();
        }
    }

    [Cmdlet(VerbsCommon.Get, "SCSMFolderHierarchy",DefaultParameterSetName="HIERARCHY")]
    public class GetSCSMFolderHierarchyCommand : ObjectCmdletHelper
    {
        private ManagementPackFolder[] _folder;
        [Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true, ParameterSetName="HIERARCHY")]
        public ManagementPackFolder[] Folder
        {
            get { return _folder; }
            set { _folder = value; }
        }
        private SwitchParameter _root;
        [Parameter(Mandatory = true, ParameterSetName = "ROOT")]
        public SwitchParameter Root
        {
            get { return _root; }
            set { _root = value; }
        }
        protected override void ProcessRecord()
        {
            base.ProcessRecord();
            if (ParameterSetName == "ROOT")
            {
                foreach (ManagementPackFolder f in _mg.Presentation.GetFolders())
                {
                    if (f.ParentFolder == null)
                    {
                        WriteObject(_mg.Presentation.GetFolderHierarchy(f.Id));
                    }
                }
            }
            else
            {
                foreach (ManagementPackFolder f in Folder)
                {
                    WriteObject(_mg.Presentation.GetFolderHierarchy(f.Id));
                }
            }
        }
    }

    #endregion

    [Cmdlet(VerbsCommon.Get, "SCSMStringResource")]
    public class GetSCSMStringResourceCommand : PresentationCmdletBase
    {
        private List<ManagementPackStringResource> l;
        protected override void BeginProcessing()
        {
            base.BeginProcessing();
            l = new List<ManagementPackStringResource>();
            foreach (ManagementPack mp in _mg.ManagementPacks.GetManagementPacks())
            {
                foreach (ManagementPackStringResource r in mp.GetStringResources())
                {
                    l.Add(r);
                }
            }
        }
        protected override void ProcessRecord()
        {
            base.ProcessRecord();
            foreach (string n in Name)
            {
                WildcardPattern wp = new WildcardPattern(n, WildcardOptions.IgnoreCase | WildcardOptions.CultureInvariant);
                foreach (ManagementPackStringResource r in l)
                {
                    if (wp.IsMatch(r.Name))
                    {
                        WriteObject(r);
                    }
                }
            }
        }
    }

    [Cmdlet(VerbsCommon.Get, "SCSMLanguagePackCulture")]
    public class GetSCSMLanguagePackCommand : SMCmdletBase
    {
        List<string> cultures;
        CultureInfo[] systemCultures;
        IList<ManagementPackLanguagePack> lpList;
        protected override void BeginProcessing()
        {
            WriteVerbose("BeginProcessing Begin");
            base.BeginProcessing();
            cultures = new List<string>();
            systemCultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
            lpList = _mg.LanguagePacks.GetLanguagePacks();
            WriteVerbose("BeginProcessing End");
        }
        protected override void ProcessRecord()
        {
            base.ProcessRecord();
            WriteVerbose("ProcessRecord");
            foreach (ManagementPackLanguagePack lp in lpList)
            {
                foreach (CultureInfo ci in systemCultures)
                {
                    if (String.Compare(ci.ThreeLetterWindowsLanguageName, lp.Name, true) == 0 && !cultures.Contains(lp.Name))
                    {
                        WriteObject(ci);
                        cultures.Add(lp.Name);
                        break;
                    }
                }
            }
        }
    }

    [Cmdlet(VerbsCommon.Get, "SCSMForm")]
    public class GetSCSMFormCommand : PresentationCmdletBase
    {
        private IList<ManagementPackForm> list;
        protected override void BeginProcessing()
        {
            base.BeginProcessing();
            list = _mg.Presentation.GetForms();
        }
        protected override void ProcessRecord()
        {
            foreach (string p in Name)
            {
                WildcardPattern pattern = new WildcardPattern(p, WildcardOptions.CultureInvariant | WildcardOptions.IgnoreCase);
                foreach (ManagementPackForm v in list)
                {
                    if (pattern.IsMatch(v.Name))
                    {
                        WriteObject(v);
                    }
                }
            }
        }
    }

    [Cmdlet(VerbsCommon.Get, "SCSMPage")]
    public class GetSCSMPageCommand : PresentationCmdletBase
    {
        private IList<ManagementPackUIPage> list;
        protected override void BeginProcessing()
        {
            base.BeginProcessing();
            list = _mg.Presentation.GetPages();
        }
        protected override void ProcessRecord()
        {
            foreach (string p in Name)
            {
                WildcardPattern pattern = new WildcardPattern(p, WildcardOptions.CultureInvariant | WildcardOptions.IgnoreCase);
                foreach (ManagementPackUIPage v in list)
                {
                    if (pattern.IsMatch(v.Name))
                    {
                        WriteObject(v);
                    }
                }
            }
        }
    }
    
    [Cmdlet(VerbsCommon.Get, "SCSMPageSet")]
    public class GetSCSMPageSetCommand : PresentationCmdletBase
    {
        private IList<ManagementPackUIPageSet> list;
        protected override void BeginProcessing()
        {
            base.BeginProcessing();
            list = _mg.Presentation.GetPageSets();
        }
        protected override void ProcessRecord()
        {
            foreach (string p in Name)
            {
                WildcardPattern pattern = new WildcardPattern(p, WildcardOptions.CultureInvariant | WildcardOptions.IgnoreCase);
                foreach (ManagementPackUIPageSet v in list)
                {
                    if (pattern.IsMatch(v.Name))
                    {
                        WriteObject(v);
                    }
                }
            }
        }
    }

    [Cmdlet(VerbsCommon.Get, "SCSMViewSetting")]
    public class GetSCSMViewSettingCommand : PresentationCmdletBase
    {
        private IList<ViewSetting> list;
        protected override void BeginProcessing()
        {
            base.BeginProcessing();
            list = _mg.Presentation.GetViewSettings();
        }
        protected override void ProcessRecord()
        {
            foreach (string p in Name)
            {
                WildcardPattern pattern = new WildcardPattern(p, WildcardOptions.CultureInvariant | WildcardOptions.IgnoreCase);
                foreach (ViewSetting v in list)
                {
                    if (pattern.IsMatch(v.ViewId.ToString()))
                    {
                        WriteObject(v);
                    }
                }
            }
        }
    }

    [Cmdlet(VerbsCommon.Get, "SCSMViewType")]
    public class GetSCSMViewTypeCommand : PresentationCmdletBase
    {
        private IList<ManagementPackViewType> list;
        protected override void BeginProcessing()
        {
            base.BeginProcessing();
            list = _mg.Presentation.GetViewTypes();
        }
        protected override void ProcessRecord()
        {
            foreach (string p in Name)
            {
                WildcardPattern pattern = new WildcardPattern(p, WildcardOptions.CultureInvariant | WildcardOptions.IgnoreCase);
                foreach (ManagementPackViewType v in list)
                {
                    if (pattern.IsMatch(v.Name))
                    {
                        WriteObject(v);
                    }
                }
            }
        }
    }

    [Cmdlet(VerbsCommon.Get, "SCSMImage")]
    public class GetSCSMImageCommand : PresentationCmdletBase
    {

        private SwitchParameter _listOnly;
        [Parameter]
        public SwitchParameter ListOnly
        {
            get { return _listOnly; }
            set { _listOnly = value; }
        }
 
        private IList<ManagementPackImage> list;
        private string _currentDirectory;
        protected override void BeginProcessing()
        {
            base.BeginProcessing();
            list = _mg.Resources.GetResources<ManagementPackImage>();
            _currentDirectory = SessionState.Path.CurrentFileSystemLocation.Path;
            if (ListOnly)
            {
                foreach (string p in Name)
                {
                    WildcardPattern pattern = new WildcardPattern(p, WildcardOptions.CultureInvariant | WildcardOptions.IgnoreCase);
                    foreach (ManagementPackImage i in list)
                    {
                        if (pattern.IsMatch(i.FileName) && !i.HasNullStream)
                        {
                            WriteObject(i);
                        }
                    }
                }
            }
        }
        protected override void ProcessRecord()
        {
            if (ListOnly)
            {
                return;
            }
            foreach (string p in Name)
            {
                WildcardPattern pattern = new WildcardPattern(p, WildcardOptions.CultureInvariant | WildcardOptions.IgnoreCase);
                foreach (ManagementPackImage v in list)
                {
                    if (pattern.IsMatch(v.FileName) && ! v.HasNullStream)
                    {
                        if (ShouldProcess(v.FileName))
                        {
                            try
                            {
                                string outputFile = String.Format("{0}\\{1}", _currentDirectory, v.FileName);
                                WriteVerbose("output filename: " + outputFile);
                                Stream s = _mg.Resources.GetResourceData(v.Id);
                                byte[] b = new byte[s.Length];
                                s.Read(b, 0, (int)s.Length);
                                s.Close();
                                s.Dispose();
                                FileStream fs = new FileStream(outputFile, FileMode.Create);
                                fs.Write(b, 0, b.Length);
                                fs.Close();
                                fs.Dispose();
                                WriteObject(SessionState.InvokeCommand.InvokeScript("Get-ChildItem " + outputFile));
                            }
                            catch (Exception e)
                            {
                                WriteError(new ErrorRecord(e, "Save Image", ErrorCategory.InvalidOperation, v));
                            }
                        }
                    }
                }
            }
        }
    }

    [Cmdlet(VerbsCommon.Get, "SCSMConsoleTask")]
    public class GetSCSMConsoleTaskCommand : PresentationCmdletBase
    {
        private IList<ManagementPackConsoleTask> consoleTaskList;
        protected override void BeginProcessing()
        {
            base.BeginProcessing();
            consoleTaskList = _mg.TaskConfiguration.GetConsoleTasks();
        }
        protected override void ProcessRecord()
        {
            base.ProcessRecord();
            foreach (ManagementPackConsoleTask task in consoleTaskList)
            {
                foreach (string n in Name)
                {
                    WildcardPattern wp = new WildcardPattern(n, WildcardOptions.CultureInvariant | WildcardOptions.IgnoreCase);
                    if (wp.IsMatch(task.Name))
                    {
                        WriteObject(task);
                        break;
                    }
                }
            }
        }

    }

    [Cmdlet(VerbsCommon.New, "SCSMColumn")]
    public class NewSCSMColum : SMCmdletBase
    {
        private string _name;
        private string _displayname;
        private string _width = "100";
        private string _bindingpath;
        private string _datatype = "s:String";
        private SwitchParameter _PassThru = false;

        [Parameter(Mandatory = true)]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        [Parameter(Mandatory = true)]
        public string BindingPath
        {
            get { return _bindingpath; }
            set { _bindingpath = value; }
        }

        [Parameter(Mandatory = false)]
        public string DisplayName
        {
            get { return _displayname; }
            set { _displayname = value; }
        }
        
        [Parameter]
        public string Width
        {
            get { return _width; }
            set { _width = value; }
        }

        [Parameter]
        public string DataType
        {
            get { return _datatype; }
            set { _datatype = value; }
        }

        [Parameter]
        public SwitchParameter PassThru
        {
            get { return _PassThru; }
            set { _PassThru = value; }
        }

        protected override void ProcessRecord()
        {
            base.ProcessRecord();
        }

        protected override void  EndProcessing()
        {
          base.EndProcessing();

            Column columnOutput = new Column();
            columnOutput.DataType = _datatype;
            columnOutput.DisplayMemberBinding = String.Format("{{Binding Path={0}}}", _bindingpath);
            columnOutput.DisplayNameId = SMHelpers.MakeMPElementSafeUniqueIdentifier(_displayname);
            columnOutput.DisplayNameString = _displayname;
            if (_name != null)
                columnOutput.Name = _name;
            else
                columnOutput.Name = SMHelpers.MakeMPElementSafeUniqueIdentifier("Column");
            columnOutput.Property = _bindingpath;
            columnOutput.Width = _width;

            if (PassThru)
            {
                WriteObject(columnOutput);
            }
        }
    }
}