Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4#region Using directives5using System;6using System.Management.Automation;7 8#endregion9 10namespace Microsoft.Management.Infrastructure.CimCmdlets11{12    /// <summary>13    /// <para>14    /// Wrapper of Cmdlet, forward the operation to Cmdlet directly.15    /// This is for unit test purpose, unit test can derive from this class,16    /// to hook up all of the cmdlet related operation and verify the correctness.17    /// </para>18    /// </summary>19    internal class CmdletOperationBase20    {21        /// <summary>22        /// <para>23        /// Wrap the Cmdlet object.24        /// </para>25        /// </summary>26        private readonly Cmdlet cmdlet;27 28        /// <summary>29        /// <para>30        /// Wrap the Cmdlet methods, for testing purpose.31        /// Test binary can define a child class of CmdletOperationBase.32        /// While Execute method of <seealso cref="CimBaseAction"/> accept the33        /// object of CmdletOperationBase as parameter.34        /// </para>35        /// </summary>36        #region CMDLET methods37 38        public virtual bool ShouldContinue(string query, string caption)39        {40            return cmdlet.ShouldContinue(query, caption);41        }42 43        public virtual bool ShouldContinue(string query, string caption, ref bool yesToAll, ref bool noToAll)44        {45            return cmdlet.ShouldContinue(query, caption, ref yesToAll, ref noToAll);46        }47 48        public virtual bool ShouldProcess(string target)49        {50            return cmdlet.ShouldProcess(target);51        }52 53        public virtual bool ShouldProcess(string target, string action)54        {55            return cmdlet.ShouldProcess(target, action);56        }57 58        public virtual bool ShouldProcess(string verboseDescription, string verboseWarning, string caption)59        {60            return cmdlet.ShouldProcess(verboseDescription, verboseWarning, caption);61        }62 63        public virtual bool ShouldProcess(string verboseDescription, string verboseWarning, string caption, out ShouldProcessReason shouldProcessReason)64        {65            return cmdlet.ShouldProcess(verboseDescription, verboseWarning, caption, out shouldProcessReason);66        }67 68        [System.Diagnostics.CodeAnalysis.DoesNotReturn]69        public virtual void ThrowTerminatingError(ErrorRecord errorRecord)70        {71            cmdlet.ThrowTerminatingError(errorRecord);72        }73 74        public virtual void WriteCommandDetail(string text)75        {76            cmdlet.WriteCommandDetail(text);77        }78 79        public virtual void WriteDebug(string text)80        {81            cmdlet.WriteDebug(text);82        }83 84        public virtual void WriteError(ErrorRecord errorRecord)85        {86            cmdlet.WriteError(errorRecord);87        }88 89        public virtual void WriteObject(object sendToPipeline, XOperationContextBase context)90        {91            cmdlet.WriteObject(sendToPipeline);92        }93 94        public virtual void WriteObject(object sendToPipeline, bool enumerateCollection, XOperationContextBase context)95        {96            cmdlet.WriteObject(sendToPipeline, enumerateCollection);97        }98 99        public virtual void WriteProgress(ProgressRecord progressRecord)100        {101            cmdlet.WriteProgress(progressRecord);102        }103 104        public virtual void WriteVerbose(string text)105        {106            cmdlet.WriteVerbose(text);107        }108 109        public virtual void WriteWarning(string text)110        {111            cmdlet.WriteWarning(text);112        }113 114        /// <summary>115        /// <para>116        /// Throw terminating error117        /// </para>118        /// </summary>119        [System.Diagnostics.CodeAnalysis.DoesNotReturn]120        internal void ThrowTerminatingError(Exception exception, string operation)121        {122            ErrorRecord errorRecord = new(exception, operation, ErrorCategory.InvalidOperation, this);123            cmdlet.ThrowTerminatingError(errorRecord);124        }125        #endregion126 127        /// <summary>128        /// Initializes a new instance of the <see cref="CmdletOperationBase"/> class.129        /// </summary>130        public CmdletOperationBase(Cmdlet cmdlet)131        {132            ValidationHelper.ValidateNoNullArgument(cmdlet, "cmdlet");133            this.cmdlet = cmdlet;134        }135    }136 137    #region Class CmdletOperationRemoveCimInstance138 139    /// <summary>140    /// <para>141    /// Wrapper of Cmdlet, override WriteObject function call since142    /// we need to remove <see cref="CimInstance"/>.143    /// </para>144    /// </summary>145    internal class CmdletOperationRemoveCimInstance : CmdletOperationBase146    {147        /// <summary>148        /// Initializes a new instance of the <see cref="CmdletOperationRemoveCimInstance"/> class.149        /// </summary>150        /// <param name="cmdlet"></param>151        public CmdletOperationRemoveCimInstance(Cmdlet cmdlet,152            CimRemoveCimInstance cimRemoveCimInstance)153            : base(cmdlet)154        {155            ValidationHelper.ValidateNoNullArgument(cimRemoveCimInstance, cimRemoveCimInstanceParameterName);156            this.removeCimInstance = cimRemoveCimInstance;157        }158 159        /// <summary>160        /// <para>161        /// Object here need to be removed if it is CimInstance162        /// </para>163        /// </summary>164        /// <param name="sendToPipeline"></param>165        public override void WriteObject(object sendToPipeline, XOperationContextBase context)166        {167            if (sendToPipeline is CimInstance)168            {169                DebugHelper.WriteLog(">>>>CmdletOperationRemoveCimInstance::WriteObject", 4);170                this.removeCimInstance.RemoveCimInstance(sendToPipeline as CimInstance, context, this);171            }172            else173            {174                base.WriteObject(sendToPipeline, context);175            }176        }177 178        public override void WriteObject(object sendToPipeline, bool enumerateCollection, XOperationContextBase context)179        {180            if (sendToPipeline is CimInstance)181            {182                this.WriteObject(sendToPipeline, context);183            }184            else185            {186                base.WriteObject(sendToPipeline, enumerateCollection, context);187            }188        }189 190        #region private methods191 192        private readonly CimRemoveCimInstance removeCimInstance;193 194        private const string cimRemoveCimInstanceParameterName = @"cimRemoveCimInstance";195 196        #endregion197    }198 199    #endregion200 201    #region Class CmdletOperationSetCimInstance202 203    /// <summary>204    /// <para>205    /// Wrapper of Cmdlet, override WriteObject function call since206    /// we need to set <see cref="CimInstance"/>.207    /// </para>208    /// </summary>209    internal class CmdletOperationSetCimInstance : CmdletOperationBase210    {211        /// <summary>212        /// Initializes a new instance of the <see cref="CmdletOperationSetCimInstance"/> class.213        /// </summary>214        /// <param name="cmdlet"></param>215        public CmdletOperationSetCimInstance(Cmdlet cmdlet,216            CimSetCimInstance theCimSetCimInstance)217            : base(cmdlet)218        {219            ValidationHelper.ValidateNoNullArgument(theCimSetCimInstance, theCimSetCimInstanceParameterName);220            this.setCimInstance = theCimSetCimInstance;221        }222 223        /// <summary>224        /// <para>225        /// Object here need to be removed if it is CimInstance226        /// </para>227        /// </summary>228        /// <param name="sendToPipeline"></param>229        public override void WriteObject(object sendToPipeline, XOperationContextBase context)230        {231            DebugHelper.WriteLogEx();232 233            if (sendToPipeline is CimInstance)234            {235                if (context is CimSetCimInstanceContext setContext)236                {237                    if (string.Equals(setContext.ParameterSetName, CimBaseCommand.QueryComputerSet, StringComparison.OrdinalIgnoreCase) ||238                        string.Equals(setContext.ParameterSetName, CimBaseCommand.QuerySessionSet, StringComparison.OrdinalIgnoreCase))239                    {240                        this.setCimInstance.SetCimInstance(sendToPipeline as CimInstance, setContext, this);241                        return;242                    }243                    else244                    {245                        DebugHelper.WriteLog("Write the cimInstance to pipeline since this CimInstance is returned by SetCimInstance.", 4);246                    }247                }248                else249                {250                    DebugHelper.WriteLog("Assert. CimSetCimInstance::SetCimInstance has NULL CimSetCimInstanceContext", 4);251                }252            }253 254            base.WriteObject(sendToPipeline, context);255        }256 257        public override void WriteObject(object sendToPipeline, bool enumerateCollection, XOperationContextBase context)258        {259            if (sendToPipeline is CimInstance)260            {261                this.WriteObject(sendToPipeline, context);262            }263            else264            {265                base.WriteObject(sendToPipeline, enumerateCollection, context);266            }267        }268 269        #region private methods270 271        private readonly CimSetCimInstance setCimInstance;272 273        private const string theCimSetCimInstanceParameterName = @"theCimSetCimInstance";274 275        #endregion276    }277    #endregion278 279    #region Class CmdletOperationInvokeCimMethod280    /// <summary>281    /// <para>282    /// Wrapper of Cmdlet, override WriteObject function call since283    /// we need to invoke cim method.284    /// </para>285    /// </summary>286    internal class CmdletOperationInvokeCimMethod : CmdletOperationBase287    {288        /// <summary>289        /// Initializes a new instance of the <see cref="CmdletOperationInvokeCimMethod"/> class.290        /// </summary>291        /// <param name="cmdlet"></param>292        public CmdletOperationInvokeCimMethod(Cmdlet cmdlet,293            CimInvokeCimMethod theCimInvokeCimMethod)294            : base(cmdlet)295        {296            ValidationHelper.ValidateNoNullArgument(theCimInvokeCimMethod, theCimInvokeCimMethodParameterName);297            this.cimInvokeCimMethod = theCimInvokeCimMethod;298        }299 300        /// <summary>301        /// <para>302        /// Object here need to be removed if it is CimInstance303        /// </para>304        /// </summary>305        /// <param name="sendToPipeline"></param>306        public override void WriteObject(object sendToPipeline, XOperationContextBase context)307        {308            DebugHelper.WriteLogEx();309 310            if (sendToPipeline is CimInstance)311            {312                this.cimInvokeCimMethod.InvokeCimMethodOnCimInstance(sendToPipeline as CimInstance, context, this);313            }314            else315            {316                base.WriteObject(sendToPipeline, context);317            }318        }319 320        public override void WriteObject(object sendToPipeline, bool enumerateCollection, XOperationContextBase context)321        {322            if (sendToPipeline is CimInstance)323            {324                this.WriteObject(sendToPipeline, context);325            }326            else327            {328                base.WriteObject(sendToPipeline, enumerateCollection, context);329            }330        }331 332        #region private methods333 334        private readonly CimInvokeCimMethod cimInvokeCimMethod;335 336        private const string theCimInvokeCimMethodParameterName = @"theCimInvokeCimMethod";337 338        #endregion339    }340 341    #endregion342 343    #region Class CmdletOperationTestCimSession344 345    /// <summary>346    /// <para>347    /// Wrapper of Cmdlet, override WriteObject function call since348    /// we need to add cim session to global cache.349    /// </para>350    /// </summary>351    internal class CmdletOperationTestCimSession : CmdletOperationBase352    {353        /// <summary>354        /// Initializes a new instance of the <see cref="CmdletOperationTestCimSession"/> class.355        /// </summary>356        /// <param name="cmdlet"></param>357        public CmdletOperationTestCimSession(Cmdlet cmdlet,358            CimNewSession theCimNewSession)359            : base(cmdlet)360        {361            ValidationHelper.ValidateNoNullArgument(theCimNewSession, theCimNewSessionParameterName);362            this.cimNewSession = theCimNewSession;363        }364 365        /// <summary>366        /// <para>367        /// Add session object to cache368        /// </para>369        /// </summary>370        /// <param name="sendToPipeline"></param>371        public override void WriteObject(object sendToPipeline, XOperationContextBase context)372        {373            DebugHelper.WriteLogEx();374 375            if (sendToPipeline is CimSession)376            {377                DebugHelper.WriteLog("Call CimNewSession::AddSessionToCache", 1);378 379                this.cimNewSession.AddSessionToCache(sendToPipeline as CimSession, context, this);380            }381            else if (sendToPipeline is PSObject)382            {383                DebugHelper.WriteLog("Write PSObject to pipeline", 1);384                base.WriteObject(sendToPipeline, context);385            }386            else387            {388                // NOTES: May need to output for warning message/verbose message389                DebugHelper.WriteLog("Ignore other type object {0}", 1, sendToPipeline);390            }391        }392 393        #region private methods394 395        private readonly CimNewSession cimNewSession;396 397        private const string theCimNewSessionParameterName = @"theCimNewSession";398 399        #endregion400    }401 402    #endregion403}404