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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#pragma warning disable 1634, 1691
#pragma warning disable 56506

using System;
using Dbg = System.Management.Automation;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Management.Automation;
using System.Management.Automation.Provider;
using System.Security;

namespace Microsoft.PowerShell.Commands
{
    /// <summary>
    /// This is the base class for all the providers that produce a view
    /// on session state data (Variables, Aliases, and Functions)
    /// </summary>
    public abstract class SessionStateProviderBase : ContainerCmdletProvider, IContentCmdletProvider
    {
        #region tracer

        /// <summary>
        /// An instance of the PSTraceSource class used for trace output.
        /// </summary>
        [Dbg.TraceSource(
             "SessionStateProvider",
             "Providers that produce a view of session state data.")]
        private static readonly Dbg.PSTraceSource s_tracer =
            Dbg.PSTraceSource.GetTracer("SessionStateProvider",
             "Providers that produce a view of session state data.");

        #endregion tracer

        #region protected members

        /// <summary>
        /// Derived classes must override to get items from session state.
        /// </summary>
        /// <param name="name">
        /// The name of the item to get.
        /// </param>
        /// <returns>
        /// The item of the given name in the appropriate session state table.
        /// </returns>
        internal abstract object GetSessionStateItem(string name);

        /// <summary>
        /// Sets a session state item in the appropriate session state table.
        /// Derived classes must override this method to set the item in the
        /// proper table.
        /// </summary>
        /// <param name="name">
        /// The name of the item to set.
        /// </param>
        /// <param name="value">
        /// The new value for the item.
        /// </param>
        /// <param name="writeItem">
        /// If true, the item that was set should be written to WriteItemObject.
        /// </param>
        internal abstract void SetSessionStateItem(string name, object value, bool writeItem);

        /// <summary>
        /// Removes a session state item from the appropriate session state table.
        /// Derived classes must override this method to remove items from the
        /// proper table.
        /// </summary>
        /// <param name="name">
        /// The name of the item to remove.
        /// </param>
        internal abstract void RemoveSessionStateItem(string name);

        /// <summary>
        /// Gets all the items in the appropriate session state table.
        /// </summary>
        /// <returns>
        /// An IDictionary representing the items in the session state table.
        /// The key is the name of the item and the value is the value.
        /// </returns>
        internal abstract IDictionary GetSessionStateTable();

        /// <summary>
        /// Since items are often more than their value, this method should
        /// be overridden to provide the value for an item.
        /// </summary>
        /// <param name="item">
        /// The item to extract the value from.
        /// </param>
        /// <returns>
        /// The value of the specified item.
        /// </returns>
        /// <remarks>
        /// The default implementation will get
        /// the Value property of a DictionaryEntry
        /// </remarks>
        internal virtual object GetValueOfItem(object item)
        {
            Dbg.Diagnostics.Assert(
                item != null,
                "Caller should verify the item parameter");

            object value = item;

            if (item is DictionaryEntry)
            {
                value = ((DictionaryEntry)item).Value;
            }

            return value;
        }

        /// <summary>
        /// Determines if the item can be renamed. Derived classes that need
        /// to perform a check should override this method.
        /// </summary>
        /// <param name="item">
        /// The item to verify if it can be renamed.
        /// </param>
        /// <returns>
        /// true if the item can be renamed or false otherwise.
        /// </returns>
        internal virtual bool CanRenameItem(object item)
        {
            return true;
        }

        #endregion protected members

        #region ItemCmdletProvider overrides

        /// <summary>
        /// Gets an item from session state.
        /// </summary>
        /// <param name="name">
        /// Name of the item to get.
        /// </param>
        /// <remarks>
        /// The item instance is written to the WriteObject
        /// method.
        /// </remarks>
        protected override void GetItem(string name)
        {
            bool isContainer = false;
            object item = null;

            IDictionary table = GetSessionStateTable();
            if (table != null)
            {
                if (string.IsNullOrEmpty(name))
                {
                    isContainer = true;
                    item = table.Values;
                }
                else
                {
                    item = table[name];
                }
            }

            if (item != null)
            {
                if (SessionState.IsVisible(this.Context.Origin, item))
                {
                    WriteItemObject(item, name, isContainer);
                }
            }
        }

        /// <summary>
        /// Sets a session state item to a given value.
        /// </summary>
        /// <param name="name">
        /// Name of the item to set
        /// </param>
        /// <param name="value">
        /// The value to which to set the item
        /// </param>
        /// <returns>
        /// Nothing. The item that was set is written to the
        /// WriteObject method.
        /// </returns>
        protected override void SetItem(
            string name,
            object value)
        {
            if (string.IsNullOrEmpty(name))
            {
                WriteError(new ErrorRecord(
                    PSTraceSource.NewArgumentNullException(nameof(name)),
                    "SetItemNullName",
                    ErrorCategory.InvalidArgument,
                    name));
                return;
            }

            try
            {
                // Confirm the set item with the user

                string action = SessionStateProviderBaseStrings.SetItemAction;

                string resourceTemplate = SessionStateProviderBaseStrings.SetItemResourceTemplate;

                string resource =
                    string.Format(
                        Host.CurrentCulture,
                        resourceTemplate,
                        name,
                        value);

                if (ShouldProcess(resource, action))
                {
                    SetSessionStateItem(name, value, true);
                }
            }
            catch (SessionStateException e)
            {
                WriteError(
                    new ErrorRecord(
                        e.ErrorRecord,
                        e));
            }
            catch (PSArgumentException argException)
            {
                WriteError(
                    new ErrorRecord(
                        argException.ErrorRecord,
                        argException));
            }
        }

        /// <summary>
        /// </summary>
        /// <param name="path"></param>
        protected override void ClearItem(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                WriteError(new ErrorRecord(
                    PSTraceSource.NewArgumentNullException(nameof(path)),
                    "ClearItemNullPath",
                    ErrorCategory.InvalidArgument,
                    path));
                return;
            }

            try
            {
                // Confirm the clear item with the user

                string action = SessionStateProviderBaseStrings.ClearItemAction;

                string resourceTemplate = SessionStateProviderBaseStrings.ClearItemResourceTemplate;

                string resource =
                    string.Format(
                        Host.CurrentCulture,
                        resourceTemplate,
                        path);

                if (ShouldProcess(resource, action))
                {
                    SetSessionStateItem(path, null, false);
                }
            }
            catch (SessionStateException e)
            {
                WriteError(
                    new ErrorRecord(
                        e.ErrorRecord,
                        e));
            }
            catch (PSArgumentException argException)
            {
                WriteError(
                    new ErrorRecord(
                        argException.ErrorRecord,
                        argException));
            }
        }

        #endregion ItemCmdletProvider overrides

        #region ContainerCmdletProvider overrides

        /// <summary>
        /// Gets the item(s) at the given path.
        /// </summary>
        /// <param name="path">
        /// The name of the item to retrieve, or all if empty or null.
        /// </param>
        /// <param name="recurse">
        /// Ignored.
        /// </param>
        protected override void GetChildItems(string path, bool recurse)
        {
            CommandOrigin origin = this.Context.Origin;
            if (string.IsNullOrEmpty(path))
            {
                IDictionary dictionary = null;

                try
                {
                    dictionary = GetSessionStateTable();
                }
                catch (SecurityException e)
                {
                    WriteError(
                        new ErrorRecord(
                            e,
                            "GetTableSecurityException",
                            ErrorCategory.ReadError,
                            path));
                    return;
                }

                // bug Windows7 #300974 says that we should sort
                List<DictionaryEntry> sortedEntries = new List<DictionaryEntry>(dictionary.Count + 1);
                foreach (DictionaryEntry entry in dictionary)
                {
                    sortedEntries.Add(entry);
                }

                sortedEntries.Sort(
                    (DictionaryEntry left, DictionaryEntry right) =>
                    {
                        string leftKey = (string)left.Key;
                        string rightKey = (string)right.Key;
                        IComparer<string> stringComparer = StringComparer.CurrentCultureIgnoreCase;
                        return stringComparer.Compare(leftKey, rightKey);
                    });

                // Now write out each object
                foreach (DictionaryEntry entry in sortedEntries)
                {
                    try
                    {
                        if (SessionState.IsVisible(origin, entry.Value))
                        {
                            WriteItemObject(entry.Value, (string)entry.Key, false);
                        }
                    }
                    catch (PSArgumentException argException)
                    {
                        WriteError(
                            new ErrorRecord(
                                argException.ErrorRecord,
                                argException));

                        return;
                    }
                    catch (SecurityException securityException)
                    {
                        WriteError(
                            new ErrorRecord(
                                securityException,
                                "GetItemSecurityException",
                                ErrorCategory.PermissionDenied,
                                (string)entry.Key));
                        return;
                    }
                }
            }
            else
            {
                object item = null;

                try
                {
                    item = GetSessionStateItem(path);
                }
                catch (PSArgumentException argException)
                {
                    WriteError(
                        new ErrorRecord(
                            argException.ErrorRecord,
                            argException));

                    return;
                }
                catch (SecurityException securityException)
                {
                    WriteError(
                        new ErrorRecord(
                            securityException,
                            "GetItemSecurityException",
                            ErrorCategory.PermissionDenied,
                            path));
                    return;
                }

                if (item != null)
                {
                    if (SessionState.IsVisible(origin, item))
                    {
                        WriteItemObject(item, path, false);
                    }
                }
            }
        }

        /// <summary>
        /// Gets the name(s) of the item(s) at the given path.
        /// </summary>
        /// <param name="path">
        /// The name of the item to retrieve, or all if empty or null.
        /// </param>
        /// <param name="returnContainers">
        /// Ignored.
        /// </param>
        protected override void GetChildNames(string path, ReturnContainers returnContainers)
        {
            CommandOrigin origin = this.Context.Origin;
            if (string.IsNullOrEmpty(path))
            {
                IDictionary dictionary = null;

                try
                {
                    dictionary = GetSessionStateTable();
                }
                catch (SecurityException e)
                {
                    WriteError(
                        new ErrorRecord(
                            e,
                            "GetChildNamesSecurityException",
                            ErrorCategory.ReadError,
                            path));
                    return;
                }

                // Now write out each object's key...

                foreach (DictionaryEntry entry in dictionary)
                {
                    try
                    {
                        if (SessionState.IsVisible(origin, entry.Value))
                        {
                            WriteItemObject(entry.Key, (string)entry.Key, false);
                        }
                    }
                    catch (PSArgumentException argException)
                    {
                        WriteError(
                            new ErrorRecord(
                                argException.ErrorRecord,
                                argException));

                        return;
                    }
                    catch (SecurityException securityException)
                    {
                        WriteError(
                            new ErrorRecord(
                                securityException,
                                "GetItemSecurityException",
                                ErrorCategory.PermissionDenied,
                                (string)entry.Key));
                        return;
                    }
                }
            }
            else
            {
                object item = null;

                try
                {
                    item = GetSessionStateItem(path);
                }
                catch (SecurityException e)
                {
                    WriteError(
                        new ErrorRecord(
                            e,
                            "GetChildNamesSecurityException",
                            ErrorCategory.ReadError,
                            path));
                    return;
                }

                if (item != null)
                {
                    if (SessionState.IsVisible(origin, item))
                    {
                        WriteItemObject(path, path, false);
                    }
                }
            }
        }

        /// <summary>
        /// Determines if there are any items.
        /// </summary>
        /// <param name="path">
        /// The container to check to see if there are any children.
        /// </param>
        /// <returns>
        /// True if path is empty or null, false otherwise.
        /// </returns>
        protected override bool HasChildItems(string path)
        {
            bool result = false;

            if (string.IsNullOrEmpty(path))
            {
                try
                {
                    if (GetSessionStateTable().Count > 0)
                    {
                        result = true;
                    }
                }
                catch (SecurityException e)
                {
                    WriteError(
                        new ErrorRecord(
                            e,
                            "HasChildItemsSecurityException",
                            ErrorCategory.ReadError,
                            path));
                }
            }

            return result;
        }

        /// <summary>
        /// Determines if the specified item exists.
        /// </summary>
        /// <param name="path">
        /// The path to the item to check. If this is null or empty, the item
        /// container is used (and always exists).
        /// </param>
        /// <returns>
        /// True if the item exists, false otherwise.
        /// </returns>
        protected override bool ItemExists(string path)
        {
            bool result = false;

            if (string.IsNullOrEmpty(path))
            {
                result = true;
            }
            else
            {
                object item = null;

                try
                {
                    item = GetSessionStateItem(path);
                }
                catch (SecurityException e)
                {
                    WriteError(
                        new ErrorRecord(
                            e,
                            "ItemExistsSecurityException",
                            ErrorCategory.ReadError,
                            path));
                }

                if (item != null)
                {
                    result = true;
                }
            }

            return result;
        }

        /// <summary>
        /// Determines if the specified path is syntactically and semantically valid.
        /// </summary>
        /// <param name="path">
        /// The path to validate.
        /// </param>
        /// <returns>
        /// True if the path is valid, or false otherwise.
        /// </returns>
        /// <remarks>
        /// The path may not contain the following characters:
        /// . ( ) :
        /// </remarks>
        protected override bool IsValidPath(string path)
        {
            return !string.IsNullOrEmpty(path);
        }

        /// <summary>
        /// Removes the item at the specified path.
        /// </summary>
        /// <param name="path">
        /// The name of the item to be removed.
        /// </param>
        /// <param name="recurse">
        /// Ignored.
        /// </param>
        protected override void RemoveItem(string path, bool recurse)
        {
            if (string.IsNullOrEmpty(path))
            {
                Exception e =
                    PSTraceSource.NewArgumentException(nameof(path));
                WriteError(new ErrorRecord(
                    e,
                    "RemoveItemNullPath",
                    ErrorCategory.InvalidArgument,
                    path));
            }
            else
            {
                // Confirm the remove item with the user

                string action = SessionStateProviderBaseStrings.RemoveItemAction;

                string resourceTemplate = SessionStateProviderBaseStrings.RemoveItemResourceTemplate;

                string resource =
                    string.Format(
                        Host.CurrentCulture,
                        resourceTemplate,
                        path);

                if (ShouldProcess(resource, action))
                {
                    try
                    {
                        RemoveSessionStateItem(path);
                    }
                    catch (SessionStateException e)
                    {
                        WriteError(
                            new ErrorRecord(
                                e.ErrorRecord,
                                e));
                        return;
                    }
                    catch (SecurityException securityException)
                    {
                        WriteError(
                            new ErrorRecord(
                                securityException,
                                "RemoveItemSecurityException",
                                ErrorCategory.PermissionDenied,
                                path));
                        return;
                    }
                    catch (PSArgumentException argException)
                    {
                        WriteError(
                            new ErrorRecord(
                                argException.ErrorRecord,
                                argException));
                        return;
                    }
                }
            }
        }

        /// <summary>
        /// Creates a new item if one of the same name doesn't already exist.
        /// </summary>
        /// <param name="path">
        /// The name of the item to create.
        /// </param>
        /// <param name="type">
        /// Ignored.
        /// </param>
        /// <param name="newItem">
        /// The value of the new item.
        /// </param>
        protected override void NewItem(string path, string type, object newItem)
        {
            if (string.IsNullOrEmpty(path))
            {
                Exception e =
                    PSTraceSource.NewArgumentException(nameof(path));
                WriteError(new ErrorRecord(
                    e,
                    "NewItemNullPath",
                    ErrorCategory.InvalidArgument,
                    path));
                return;
            }

            if (newItem == null)
            {
                ArgumentNullException argException =
                    PSTraceSource.NewArgumentNullException("value");

                WriteError(
                    new ErrorRecord(
                        argException,
                        "NewItemValueNotSpecified",
                        ErrorCategory.InvalidArgument,
                        path));
                return;
            }

            if (ItemExists(path) && !Force)
            {
                PSArgumentException e =
                    (PSArgumentException)PSTraceSource.NewArgumentException(
                        nameof(path),
                        SessionStateStrings.NewItemAlreadyExists,
                        path);

                WriteError(
                    new ErrorRecord(
                        e.ErrorRecord,
                        e));
                return;
            }
            else
            {
                // Confirm the new item with the user

                string action = SessionStateProviderBaseStrings.NewItemAction;

                string resourceTemplate = SessionStateProviderBaseStrings.NewItemResourceTemplate;

                string resource =
                    string.Format(
                        Host.CurrentCulture,
                        resourceTemplate,
                        path,
                        type,
                        newItem);

                if (ShouldProcess(resource, action))
                {
                    SetItem(path, newItem);
                }
            }
        }

        /// <summary>
        /// Copies the specified item.
        /// </summary>
        /// <param name="path">
        /// The name of the item to copy.
        /// </param>
        /// <param name="copyPath">
        /// The name of the item to create.
        /// </param>
        /// <param name="recurse">
        /// Ignored.
        /// </param>
        protected override void CopyItem(string path, string copyPath, bool recurse)
        {
            if (string.IsNullOrEmpty(path))
            {
                Exception e =
                    PSTraceSource.NewArgumentException(nameof(path));
                WriteError(new ErrorRecord(
                    e,
                    "CopyItemNullPath",
                    ErrorCategory.InvalidArgument,
                    path));
                return;
            }

            // If copyPath is null or empty, that means we are trying to copy
            // the item to itself so it should be a no-op.

            if (string.IsNullOrEmpty(copyPath))
            {
                // Just get the item for -passthru
                GetItem(path);
                return;
            }

            object item = null;

            try
            {
                item = GetSessionStateItem(path);
            }
            catch (SecurityException e)
            {
                WriteError(
                    new ErrorRecord(
                        e,
                        "CopyItemSecurityException",
                        ErrorCategory.ReadError,
                        path));
                return;
            }

            if (item != null)
            {
                // Confirm the new item with the user

                string action = SessionStateProviderBaseStrings.CopyItemAction;

                string resourceTemplate = SessionStateProviderBaseStrings.CopyItemResourceTemplate;

                string resource =
                    string.Format(
                        Host.CurrentCulture,
                        resourceTemplate,
                        path,
                        copyPath);

                if (ShouldProcess(resource, action))
                {
                    try
                    {
                        SetSessionStateItem(copyPath, GetValueOfItem(item), true);
                    }
                    catch (SessionStateException e)
                    {
                        WriteError(
                            new ErrorRecord(
                                e.ErrorRecord,
                                e));
                        return;
                    }
                    catch (PSArgumentException argException)
                    {
                        WriteError(
                            new ErrorRecord(
                                argException.ErrorRecord,
                                argException));
                        return;
                    }
                }
            }
            else
            {
                PSArgumentException e =
                    (PSArgumentException)PSTraceSource.NewArgumentException(
                        nameof(path),
                        SessionStateStrings.CopyItemDoesntExist,
                        path);

                WriteError(
                    new ErrorRecord(
                        e.ErrorRecord,
                        e));
                return;
            }
        }

        /// <summary>
        /// Copies the specified item.
        /// </summary>
        /// <param name="name">
        /// The name of the item to copy.
        /// </param>
        /// <param name="newName">
        /// The new name of the item.
        /// </param>
        protected override void RenameItem(string name, string newName)
        {
            if (string.IsNullOrEmpty(name))
            {
                Exception e =
                    PSTraceSource.NewArgumentException(nameof(name));
                WriteError(new ErrorRecord(
                    e,
                    "RenameItemNullPath",
                    ErrorCategory.InvalidArgument,
                    name));
                return;
            }

            object item = null;

            try
            {
                item = GetSessionStateItem(name);
            }
            catch (SecurityException e)
            {
                WriteError(
                    new ErrorRecord(
                        e,
                        "RenameItemSecurityException",
                        ErrorCategory.ReadError,
                        name));
                return;
            }

            if (item != null)
            {
                if (ItemExists(newName) && !Force)
                {
                    PSArgumentException e =
                        (PSArgumentException)PSTraceSource.NewArgumentException(
                            nameof(newName),
                            SessionStateStrings.NewItemAlreadyExists,
                            newName);

                    WriteError(
                        new ErrorRecord(
                            e.ErrorRecord,
                            e));
                    return;
                }
                else
                {
                    try
                    {
                        if (CanRenameItem(item))
                        {
                            // Confirm the new item with the user

                            string action = SessionStateProviderBaseStrings.RenameItemAction;

                            string resourceTemplate = SessionStateProviderBaseStrings.RenameItemResourceTemplate;

                            string resource =
                                string.Format(
                                    Host.CurrentCulture,
                                    resourceTemplate,
                                    name,
                                    newName);

                            if (ShouldProcess(resource, action))
                            {
                                if (string.Equals(name, newName, StringComparison.OrdinalIgnoreCase))
                                {
                                    // This is a no-op. Just get the item for -passthru
                                    GetItem(newName);
                                    return;
                                }

                                try
                                {
                                    SetSessionStateItem(newName, item, true);
                                    RemoveSessionStateItem(name);
                                }
                                catch (SessionStateException e)
                                {
                                    WriteError(
                                        new ErrorRecord(
                                            e.ErrorRecord,
                                            e));
                                    return;
                                }
                                catch (PSArgumentException argException)
                                {
                                    WriteError(
                                        new ErrorRecord(
                                            argException.ErrorRecord,
                                            argException));
                                    return;
                                }
                                catch (SecurityException securityException)
                                {
                                    WriteError(
                                        new ErrorRecord(
                                            securityException,
                                            "RenameItemSecurityException",
                                            ErrorCategory.PermissionDenied,
                                            name));
                                    return;
                                }
                            }
                        }
                    }
                    catch (SessionStateException e)
                    {
                        WriteError(
                            new ErrorRecord(
                                e.ErrorRecord,
                                e));
                        return;
                    }
                }
            }
            else
            {
                PSArgumentException e =
                    (PSArgumentException)PSTraceSource.NewArgumentException(
                        nameof(name),
                        SessionStateStrings.RenameItemDoesntExist,
                        name);

                WriteError(
                    new ErrorRecord(
                        e.ErrorRecord,
                        e));
                return;
            }
        }

        #endregion ContainerCmdletProvider overrides

        #region IContentCmdletProvider methods

        /// <summary>
        /// Gets an instance of the content reader for this provider for the
        /// specified path.
        /// </summary>
        /// <param name="path">
        /// The path to get the content reader for.
        /// </param>
        /// <returns>
        /// An instance of an IContentReader for the given path.
        /// </returns>
        public IContentReader GetContentReader(string path)
        {
            return new SessionStateProviderBaseContentReaderWriter(path, this);
        }

        /// <summary>
        /// Gets an instance of the content writer for this provider for the
        /// specified path.
        /// </summary>
        /// <param name="path">
        /// The path to get the content writer for.
        /// </param>
        /// <returns>
        /// An instance of an IContentWriter for the given path.
        /// </returns>
        public IContentWriter GetContentWriter(string path)
        {
            return new SessionStateProviderBaseContentReaderWriter(path, this);
        }

        /// <summary>
        /// Always throws a NotSupportedException.
        /// </summary>
        /// <param name="path">
        /// ignored.
        /// </param>
        /// <exception cref="NotSupportedException">
        /// This exception is always thrown.
        /// </exception>
        public void ClearContent(string path)
        {
            throw
                PSTraceSource.NewNotSupportedException(
                    SessionStateStrings.IContent_Clear_NotSupported);
        }

        #region dynamic parameters

        // For now, none of the derived providers need dynamic parameters
        // so these methods just return null

        /// <summary>
        /// Always returns null.
        /// </summary>
        /// <param name="path"></param>
        /// <returns>Null.</returns>
        public object GetContentReaderDynamicParameters(string path) { return null; }

        /// <summary>
        /// Always returns null.
        /// </summary>
        /// <param name="path"></param>
        /// <returns>Null.</returns>
        public object GetContentWriterDynamicParameters(string path) { return null; }

        /// <summary>
        /// Always returns null.
        /// </summary>
        /// <param name="path"></param>
        /// <returns>Null.</returns>
        public object ClearContentDynamicParameters(string path) { return null; }

        #endregion
        #endregion
    }

    /// <summary>
    /// The content reader/writer for all providers deriving from SessionStateProviderBase.
    /// </summary>
    public class SessionStateProviderBaseContentReaderWriter : IContentReader, IContentWriter
    {
        /// <summary>
        /// Constructs a content reader/writer for the specified provider using the specified
        /// path to read or write the content.
        /// </summary>
        /// <param name="path">
        /// The path to the session state item which the content will be read or written.
        /// </param>
        /// <param name="provider">
        /// The SessionStateProviderBase derived provider that the content will be read or written
        /// from/to.
        /// </param>
        /// <exception cref="ArgumentException">
        /// if <paramref name="path"/> is null or empty.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="provider"/> is null.
        /// </exception>
        internal SessionStateProviderBaseContentReaderWriter(string path, SessionStateProviderBase provider)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw PSTraceSource.NewArgumentException(nameof(path));
            }

            if (provider == null)
            {
                throw PSTraceSource.NewArgumentNullException(nameof(provider));
            }

            _path = path;
            _provider = provider;
        }

        private readonly string _path;
        private readonly SessionStateProviderBase _provider;

        /// <summary>
        /// Reads the content from the item.
        /// </summary>
        /// <param name="readCount">
        /// The number of "blocks" of data to be read from the item.
        /// </param>
        /// <returns>
        /// An array of the blocks of data read from the item.
        /// </returns>
        /// <remarks>
        /// A "block" of content is provider specific.  For the file system
        /// a "block" may be considered a byte, a character, or delimited string.
        /// </remarks>
        public IList Read(long readCount)
        {
            IList result = null;

            if (!_contentRead)
            {
                object item = _provider.GetSessionStateItem(_path);

                if (item != null)
                {
                    object getItemValueResult = _provider.GetValueOfItem(item);

                    if (getItemValueResult != null)
                    {
                        result = getItemValueResult as IList ?? new object[] { getItemValueResult };
                    }

                    _contentRead = true;
                }
            }

            return result;
        }

        private bool _contentRead;

        /// <summary>
        /// Writes content to the item.
        /// </summary>
        /// <param name="content">
        /// An array of content "blocks" to be written to the item.
        /// </param>
        /// <returns>
        /// The blocks of content that were successfully written to the item.
        /// </returns>
        /// <remarks>
        /// A "block" of content is provider specific.  For the file system
        /// a "block" may be considered a byte, a character, or delimited string.
        /// </remarks>
        public IList Write(IList content)
        {
            if (content == null)
            {
                throw PSTraceSource.NewArgumentNullException(nameof(content));
            }

            // Unravel the IList if there is only one value
            object valueToSet = content;
            if (content.Count == 1)
            {
                valueToSet = content[0];
            }

            _provider.SetSessionStateItem(_path, valueToSet, false);

            return content;
        }

        /// <summary>
        /// None of the derived providers supports seeking for V1 so this
        /// always throws a NotSupportedException.
        /// </summary>
        /// <param name="offset">
        /// ignored
        /// </param>
        /// <param name="origin">
        /// ignored
        /// </param>
        /// <exception cref="NotSupportedException">
        /// This exception is always thrown.
        /// </exception>
        public void Seek(long offset, SeekOrigin origin)
        {
            throw
                PSTraceSource.NewNotSupportedException(
                    SessionStateStrings.IContent_Seek_NotSupported);
        }

        /// <summary>
        /// Closes the reader. None of the derived providers need to
        /// close their reader so do nothing.
        /// </summary>
        public void Close() { }

        /// <summary>
        /// Closes the reader. None of the derived providers need to
        /// close their reader so do nothing.
        /// </summary>
        public void Dispose() { Close(); GC.SuppressFinalize(this); }
    }
}

#pragma warning restore 56506