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.Collections;7using System.Collections.Generic;8using System.Diagnostics;9using System.Globalization;10using System.Management.Automation;11using System.Net;12using System.Text;13using Microsoft.Management.Infrastructure.Options;14#endregion15 16namespace Microsoft.Management.Infrastructure.CimCmdlets17{18    #region Parameter Set Resolving Classes19 20    /// <summary>21    /// <para>22    /// Define class <c>ParameterDefinitionEntry</c>.23    /// </para>24    /// </summary>25    internal class ParameterDefinitionEntry26    {27        /// <summary>28        /// Initializes a new instance of the <see cref="ParameterDefinitionEntry"/> class.29        /// </summary>30        /// <param name="parameterSetName"></param>31        /// <param name="mandatory"></param>32        internal ParameterDefinitionEntry(string parameterSetName, bool mandatory)33        {34            this.IsMandatory = mandatory;35            this.ParameterSetName = parameterSetName;36        }37 38        /// <summary>39        /// Property ParameterSetName.40        /// </summary>41        internal string ParameterSetName { get; }42 43        /// <summary>44        /// Whether the parameter is mandatory to the set.45        /// </summary>46        internal bool IsMandatory { get; }47    }48 49    /// <summary>50    /// <para>51    /// Define class <c>ParameterSetEntry</c>.52    /// </para>53    /// </summary>54    internal class ParameterSetEntry55    {56        /// <summary>57        /// Initializes a new instance of the <see cref="ParameterSetEntry"/> class.58        /// </summary>59        /// <param name="mandatoryParameterCount"></param>60        internal ParameterSetEntry(uint mandatoryParameterCount)61        {62            this.MandatoryParameterCount = mandatoryParameterCount;63            this.IsDefaultParameterSet = false;64            reset();65        }66 67        /// <summary>68        /// Initializes a new instance of the <see cref="ParameterSetEntry"/> class.69        /// </summary>70        /// <param name="toClone"></param>71        internal ParameterSetEntry(ParameterSetEntry toClone)72        {73            this.MandatoryParameterCount = toClone.MandatoryParameterCount;74            this.IsDefaultParameterSet = toClone.IsDefaultParameterSet;75            reset();76        }77 78        /// <summary>79        /// Initializes a new instance of the <see cref="ParameterSetEntry"/> class.80        /// </summary>81        /// <param name="mandatoryParameterCount"></param>82        /// <param name="mandatory"></param>83        internal ParameterSetEntry(uint mandatoryParameterCount, bool isDefault)84        {85            this.MandatoryParameterCount = mandatoryParameterCount;86            this.IsDefaultParameterSet = isDefault;87            reset();88        }89 90        /// <summary>91        /// Reset the internal status.92        /// </summary>93        internal void reset()94        {95            this.SetMandatoryParameterCount = this.SetMandatoryParameterCountAtBeginProcess;96            this.IsValueSet = this.IsValueSetAtBeginProcess;97        }98 99        /// <summary>100        /// Property <c>DefaultParameterSet</c>101        /// </summary>102        internal bool IsDefaultParameterSet { get; }103 104        /// <summary>105        /// Property <c>MandatoryParameterCount</c>106        /// </summary>107        internal uint MandatoryParameterCount { get; } = 0;108 109        /// <summary>110        /// Property <c>IsValueSet</c>111        /// </summary>112        internal bool IsValueSet { get; set; }113 114        /// <summary>115        /// Property <c>IsValueSetAtBeginProcess</c>116        /// </summary>117        internal bool IsValueSetAtBeginProcess { get; set; }118 119        /// <summary>120        /// Property <c>SetMandatoryParameterCount</c>121        /// </summary>122        internal uint SetMandatoryParameterCount { get; set; } = 0;123 124        /// <summary>125        /// Property <c>SetMandatoryParameterCountAtBeginProcess</c>126        /// </summary>127        internal uint SetMandatoryParameterCountAtBeginProcess { get; set; } = 0;128    }129 130    /// <summary>131    /// Define class <c>ParameterBinder</c>.132    /// </summary>133    internal class ParameterBinder134    {135        /// <summary>136        /// Initializes a new instance of the <see cref="ParameterBinder"/> class.137        /// </summary>138        /// <param name="parameters"></param>139        /// <param name="sets"></param>140        internal ParameterBinder(141            Dictionary<string, HashSet<ParameterDefinitionEntry>> parameters,142            Dictionary<string, ParameterSetEntry> sets)143        {144            this.CloneParameterEntries(parameters, sets);145        }146 147        #region Two dictionaries used to determine the bound parameter set148 149        /// <summary>150        /// Define the parameter definition entries,151        /// each parameter may belong a set of parameterSets, each parameter set152        /// are defined by a <seealso cref="ParameterDefinitionEntry"/>.153        /// </summary>154        private Dictionary<string, HashSet<ParameterDefinitionEntry>> parameterDefinitionEntries;155 156        /// <summary>157        /// <para>158        /// Define parameter set entries,159        /// each cmdlet has a list of parameter set, each has number of mandatory parameters, etc.160        /// This data structure is used to track the number of mandatory parameter has been set for161        /// current parameterset, whether the parameter set was been set by user.162        /// </para>163        /// </summary>164        private Dictionary<string, ParameterSetEntry> parameterSetEntries;165 166        #endregion167 168        /// <summary>169        /// <para>170        /// Used to remember the set of parameterset were set171        /// if any conflict occurred with current parameter,172        /// throw exception173        /// </para>174        /// </summary>175        private List<string> parametersetNamesList = new();176 177        /// <summary>178        /// Parameter names list.179        /// </summary>180        private readonly List<string> parameterNamesList = new();181 182        /// <summary>183        /// <para>184        /// Used to remember the set of parameterset were set before begin process185        /// if any conflict occurred with current parameter,186        /// throw exception187        /// </para>188        /// </summary>189        private List<string> parametersetNamesListAtBeginProcess = new();190 191        /// <summary>192        /// Parameter names list before begin process.193        /// </summary>194        private readonly List<string> parameterNamesListAtBeginProcess = new();195 196        /// <summary>197        /// <para>198        /// Reset the status of parameter set entries199        /// </para>200        /// </summary>201        internal void reset()202        {203            foreach (KeyValuePair<string, ParameterSetEntry> setEntry in parameterSetEntries)204            {205                setEntry.Value.reset();206            }207 208            this.parametersetNamesList.Clear();209            foreach (string parametersetName in this.parametersetNamesListAtBeginProcess)210            {211                this.parametersetNamesList.Add(parametersetName);212            }213 214            this.parameterNamesList.Clear();215            foreach (string parameterName in this.parameterNamesListAtBeginProcess)216            {217                this.parameterNamesList.Add(parameterName);218            }219        }220 221        /// <summary>222        /// <para>223        /// A given parameter's value was set by cmdlet caller,224        /// check and change the status of parameter set,225        /// throw exception if confliction occurred226        /// </para>227        /// </summary>228        /// <param name="parameterName"></param>229        /// <exception cref="PSArgumentException">Throw if conflict parameter was set.</exception>230        internal void SetParameter(string parameterName, bool isBeginProcess)231        {232            DebugHelper.WriteLogEx("ParameterName = {0}, isBeginProcess = {1}", 0, parameterName, isBeginProcess);233 234            if (this.parameterNamesList.Contains(parameterName))235            {236                DebugHelper.WriteLogEx("ParameterName {0} is already bound ", 1, parameterName);237                return;238            }239            else240            {241                this.parameterNamesList.Add(parameterName);242                if (isBeginProcess)243                {244                    this.parameterNamesListAtBeginProcess.Add(parameterName);245                }246            }247 248            if (this.parametersetNamesList.Count == 0)249            {250                List<string> nameset = new();251                foreach (ParameterDefinitionEntry parameterDefinitionEntry in this.parameterDefinitionEntries[parameterName])252                {253                    DebugHelper.WriteLogEx("parameterset name = '{0}'; mandatory = '{1}'", 1, parameterDefinitionEntry.ParameterSetName, parameterDefinitionEntry.IsMandatory);254                    ParameterSetEntry psEntry = this.parameterSetEntries[parameterDefinitionEntry.ParameterSetName];255                    if (psEntry == null)256                        continue;257 258                    if (parameterDefinitionEntry.IsMandatory)259                    {260                        psEntry.SetMandatoryParameterCount++;261                        if (isBeginProcess)262                        {263                            psEntry.SetMandatoryParameterCountAtBeginProcess++;264                        }265 266                        DebugHelper.WriteLogEx("parameterset name = '{0}'; SetMandatoryParameterCount = '{1}'", 1, parameterDefinitionEntry.ParameterSetName, psEntry.SetMandatoryParameterCount);267                    }268 269                    if (!psEntry.IsValueSet)270                    {271                        psEntry.IsValueSet = true;272                        if (isBeginProcess)273                        {274                            psEntry.IsValueSetAtBeginProcess = true;275                        }276                    }277 278                    nameset.Add(parameterDefinitionEntry.ParameterSetName);279                }280 281                this.parametersetNamesList = nameset;282                if (isBeginProcess)283                {284                    this.parametersetNamesListAtBeginProcess = nameset;285                }286            }287            else288            {289                List<string> nameset = new();290                foreach (ParameterDefinitionEntry entry in this.parameterDefinitionEntries[parameterName])291                {292                    if (this.parametersetNamesList.Contains(entry.ParameterSetName))293                    {294                        nameset.Add(entry.ParameterSetName);295                        if (entry.IsMandatory)296                        {297                            ParameterSetEntry psEntry = this.parameterSetEntries[entry.ParameterSetName];298                            psEntry.SetMandatoryParameterCount++;299                            if (isBeginProcess)300                            {301                                psEntry.SetMandatoryParameterCountAtBeginProcess++;302                            }303 304                            DebugHelper.WriteLogEx("parameterset name = '{0}'; SetMandatoryParameterCount = '{1}'",305                                1,306                                entry.ParameterSetName,307                                psEntry.SetMandatoryParameterCount);308                        }309                    }310                }311 312                if (nameset.Count == 0)313                {314                    throw new PSArgumentException(CimCmdletStrings.UnableToResolveParameterSetName);315                }316                else317                {318                    this.parametersetNamesList = nameset;319                    if (isBeginProcess)320                    {321                        this.parametersetNamesListAtBeginProcess = nameset;322                    }323                }324            }325        }326 327        /// <summary>328        /// Get the parameter set name based on current binding results.329        /// </summary>330        /// <returns></returns>331        internal string GetParameterSet()332        {333            DebugHelper.WriteLogEx();334 335            string boundParameterSetName = null;336            string defaultParameterSetName = null;337            List<string> noMandatoryParameterSet = new();338 339            // Looking for parameter set which have mandatory parameters340            foreach (string parameterSetName in this.parameterSetEntries.Keys)341            {342                ParameterSetEntry entry = this.parameterSetEntries[parameterSetName];343                DebugHelper.WriteLogEx(344                    "parameterset name = {0}, {1}/{2} mandatory parameters.",345                    1,346                    parameterSetName,347                    entry.SetMandatoryParameterCount,348                    entry.MandatoryParameterCount);349 350                // Ignore the parameter set which has no mandatory parameter firstly351                if (entry.MandatoryParameterCount == 0)352                {353                    if (entry.IsDefaultParameterSet)354                    {355                        defaultParameterSetName = parameterSetName;356                    }357 358                    if (entry.IsValueSet)359                    {360                        noMandatoryParameterSet.Add(parameterSetName);361                    }362 363                    continue;364                }365 366                if ((entry.SetMandatoryParameterCount == entry.MandatoryParameterCount) &&367                    this.parametersetNamesList.Contains(parameterSetName))368                {369                    if (boundParameterSetName != null)370                    {371                        throw new PSArgumentException(CimCmdletStrings.UnableToResolveParameterSetName);372                    }373 374                    boundParameterSetName = parameterSetName;375                }376            }377 378            // Looking for parameter set which has no mandatory parameters379            if (boundParameterSetName == null)380            {381                // throw if there are > 1 parameter set382                if (noMandatoryParameterSet.Count > 1)383                {384                    throw new PSArgumentException(CimCmdletStrings.UnableToResolveParameterSetName);385                }386                else if (noMandatoryParameterSet.Count == 1)387                {388                    boundParameterSetName = noMandatoryParameterSet[0];389                }390            }391 392            // Looking for default parameter set393            boundParameterSetName ??= defaultParameterSetName;394 395            // throw if still can not find the parameter set name396            if (boundParameterSetName == null)397            {398                throw new PSArgumentException(CimCmdletStrings.UnableToResolveParameterSetName);399            }400 401            return boundParameterSetName;402        }403 404        /// <summary>405        /// Deep clone the parameter entries to member variable.406        /// </summary>407        private void CloneParameterEntries(408            Dictionary<string, HashSet<ParameterDefinitionEntry>> parameters,409            Dictionary<string, ParameterSetEntry> sets)410        {411            this.parameterDefinitionEntries = parameters;412            this.parameterSetEntries = new Dictionary<string, ParameterSetEntry>();413            foreach (KeyValuePair<string, ParameterSetEntry> parameterSet in sets)414            {415                this.parameterSetEntries.Add(parameterSet.Key, new ParameterSetEntry(parameterSet.Value));416            }417        }418    }419 420    #endregion421 422    /// <summary>423    /// Base command for all cim cmdlets.424    /// </summary>425    public class CimBaseCommand : Cmdlet, IDisposable426    {427        #region resolve parameter set name428        /// <summary>429        /// <para>430        /// Check set parameters and set ParameterSetName431        /// </para>432        /// <para>433        /// Following are special types to be handled434        /// Microsoft.Management.Infrastructure.Options.PacketEncoding435        /// Microsoft.Management.Infrastructure.Options.ImpersonationType436        /// UInt32437        /// Authentication.None  (default value)?438        /// ProxyType.None439        /// </para>440        /// </summary>441        internal void CheckParameterSet()442        {443            if (this.parameterBinder != null)444            {445                try446                {447                    this.ParameterSetName = this.parameterBinder.GetParameterSet();448                }449                finally450                {451                    this.parameterBinder.reset();452                }453            }454 455            DebugHelper.WriteLog("current parameterset is: " + this.ParameterSetName, 4);456        }457 458        /// <summary>459        /// Redirect to parameterBinder to set one parameter.460        /// </summary>461        /// <param name="parameterName"></param>462        internal void SetParameter(object value, string parameterName)463        {464            // Ignore the null value being set,465            // Null value could be set by caller unintentionally,466            // or by powershell to reset the parameter to default value467            // before the next parameter binding, and ProcessRecord call468            if (value == null)469            {470                return;471            }472 473            this.parameterBinder?.SetParameter(parameterName, this.AtBeginProcess);474        }475        #endregion476 477        #region constructors478 479        /// <summary>480        /// Initializes a new instance of the <see cref="CimBaseCommand"/> class.481        /// </summary>482        internal CimBaseCommand()483        {484            this.disposed = false;485            this.parameterBinder = null;486        }487 488        /// <summary>489        /// Initializes a new instance of the <see cref="CimBaseCommand"/> class.490        /// </summary>491        internal CimBaseCommand(Dictionary<string, HashSet<ParameterDefinitionEntry>> parameters,492            Dictionary<string, ParameterSetEntry> sets)493        {494            this.disposed = false;495            this.parameterBinder = new ParameterBinder(parameters, sets);496        }497 498        #endregion499 500        #region override functions of Cmdlet501 502        /// <summary>503        /// StopProcessing method.504        /// </summary>505        protected override void StopProcessing()506        {507            Dispose();508        }509 510        #endregion511 512        #region IDisposable interface513        /// <summary>514        /// IDisposable interface.515        /// </summary>516        private bool disposed;517 518        /// <summary>519        /// <para>520        /// Dispose() calls Dispose(true).521        /// Implement IDisposable. Do not make this method virtual.522        /// A derived class should not be able to override this method.523        /// </para>524        /// </summary>525        public void Dispose()526        {527            Dispose(true);528            // This object will be cleaned up by the Dispose method.529            // Therefore, you should call GC.SuppressFinalize to530            // take this object off the finalization queue531            // and prevent finalization code for this object532            // from executing a second time.533            GC.SuppressFinalize(this);534        }535 536        /// <summary>537        /// <para>538        /// Dispose(bool disposing) executes in two distinct scenarios.539        /// If disposing equals true, the method has been called directly540        /// or indirectly by a user's code. Managed and unmanaged resources541        /// can be disposed.542        /// If disposing equals false, the method has been called by the543        /// runtime from inside the finalizer and you should not reference544        /// other objects. Only unmanaged resources can be disposed.545        /// </para>546        /// </summary>547        /// <param name="disposing">Whether it is directly called.</param>548        protected void Dispose(bool disposing)549        {550            // Check to see if Dispose has already been called.551            if (!this.disposed)552            {553                // If disposing equals true, dispose all managed554                // and unmanaged resources.555                if (disposing)556                {557                    DisposeInternal();558                }559 560                // Call the appropriate methods to clean up561                // unmanaged resources here.562                // If disposing is false,563                // only the following code is executed.564 565                // Note disposing has been done.566                disposed = true;567            }568        }569 570        /// <summary>571        /// Clean up resources.572        /// </summary>573        protected virtual void DisposeInternal()574        {575            // Dispose managed resources.576            this.operation?.Dispose();577        }578        #endregion579 580        #region private members581 582        /// <summary>583        /// Parameter binder used to resolve parameter set name.584        /// </summary>585        private readonly ParameterBinder parameterBinder;586 587        /// <summary>588        /// <para>589        /// Async operation handler590        /// </para>591        /// </summary>592        private CimAsyncOperation operation;593 594        /// <summary>595        /// Lock object.596        /// </summary>597        private readonly object myLock = new();598 599        /// <summary>600        /// This flag is introduced to resolve the parameter set name601        /// during process record602        /// Whether at begin process time, false means in processrecord.603        /// </summary>604        private bool atBeginProcess = true;605 606        internal bool AtBeginProcess607        {608            get609            {610                return this.atBeginProcess;611            }612 613            set614            {615                this.atBeginProcess = value;616            }617        }618        #endregion619 620        #region internal properties621 622        /// <summary>623        /// <para>624        /// Set <see cref="CimAsyncOperation"/> object, to which625        /// current cmdlet will delegate all operations.626        /// </para>627        /// </summary>628        internal CimAsyncOperation AsyncOperation629        {630            get631            {632                return this.operation;633            }634 635            set636            {637                lock (this.myLock)638                {639                    Debug.Assert(this.operation == null, "Caller should verify that operation is null");640                    this.operation = value;641                }642            }643        }644 645        /// <summary>646        /// <para>647        /// Get current ParameterSetName of the cmdlet648        /// </para>649        /// </summary>650        internal string ParameterSetName { get; private set; }651 652        /// <summary>653        /// Gets/Sets cmdlet operation wrapper object.654        /// </summary>655        internal virtual CmdletOperationBase CmdletOperation656        {657            get;658            set;659        }660 661        /// <summary>662        /// <para>663        /// Throw terminating error664        /// </para>665        /// </summary>666        [System.Diagnostics.CodeAnalysis.DoesNotReturn]667        internal void ThrowTerminatingError(Exception exception, string operation)668        {669            ErrorRecord errorRecord = new(exception, operation, ErrorCategory.InvalidOperation, this);670            this.CmdletOperation.ThrowTerminatingError(errorRecord);671        }672 673        #endregion674 675        #region internal const strings676 677        /// <summary>678        /// Alias CN - computer name.679        /// </summary>680        internal const string AliasCN = "CN";681 682        /// <summary>683        /// Alias ServerName - computer name.684        /// </summary>685        internal const string AliasServerName = "ServerName";686 687        /// <summary>688        /// Alias OT - operation timeout.689        /// </summary>690        internal const string AliasOT = "OT";691 692        /// <summary>693        /// Session set name.694        /// </summary>695        internal const string SessionSetName = "SessionSet";696 697        /// <summary>698        /// Computer set name.699        /// </summary>700        internal const string ComputerSetName = "ComputerSet";701 702        /// <summary>703        /// Class name computer set name.704        /// </summary>705        internal const string ClassNameComputerSet = "ClassNameComputerSet";706 707        /// <summary>708        /// Resource Uri computer set name.709        /// </summary>710        internal const string ResourceUriComputerSet = "ResourceUriComputerSet";711 712        /// <summary>713        /// <see cref="CimInstance"/> computer set name.714        /// </summary>715        internal const string CimInstanceComputerSet = "CimInstanceComputerSet";716 717        /// <summary>718        /// Query computer set name.719        /// </summary>720        internal const string QueryComputerSet = "QueryComputerSet";721 722        /// <summary>723        /// Class name session set name.724        /// </summary>725        internal const string ClassNameSessionSet = "ClassNameSessionSet";726 727        /// <summary>728        /// Resource Uri session set name.729        /// </summary>730        internal const string ResourceUriSessionSet = "ResourceUriSessionSet";731 732        /// <summary>733        /// <see cref="CimInstance"/> session set name.734        /// </summary>735        internal const string CimInstanceSessionSet = "CimInstanceSessionSet";736 737        /// <summary>738        /// Query session set name.739        /// </summary>740        internal const string QuerySessionSet = "QuerySessionSet";741 742        /// <summary>743        /// <see cref="CimClass"/> computer set name.744        /// </summary>745        internal const string CimClassComputerSet = "CimClassComputerSet";746 747        /// <summary>748        /// <see cref="CimClass"/> session set name.749        /// </summary>750        internal const string CimClassSessionSet = "CimClassSessionSet";751 752        #region Session related parameter set name753 754        internal const string ComputerNameSet = "ComputerNameSet";755        internal const string SessionIdSet = "SessionIdSet";756        internal const string InstanceIdSet = "InstanceIdSet";757        internal const string NameSet = "NameSet";758        internal const string CimSessionSet = "CimSessionSet";759        internal const string WSManParameterSet = "WSManParameterSet";760        internal const string DcomParameterSet = "DcomParameterSet";761        internal const string ProtocolNameParameterSet = "ProtocolTypeSet";762        #endregion763 764        #region register cimindication parameter set name765        internal const string QueryExpressionSessionSet = "QueryExpressionSessionSet";766        internal const string QueryExpressionComputerSet = "QueryExpressionComputerSet";767        #endregion768 769        /// <summary>770        /// Credential parameter set.771        /// </summary>772        internal const string CredentialParameterSet = "CredentialParameterSet";773 774        /// <summary>775        /// Certificate parameter set.776        /// </summary>777        internal const string CertificateParameterSet = "CertificateParameterSet";778 779        /// <summary>780        /// CimInstance parameter alias.781        /// </summary>782        internal const string AliasCimInstance = "CimInstance";783 784        #endregion785 786        #region internal helper function787 788        /// <summary>789        /// <para>790        /// Throw invalid AuthenticationType791        /// </para>792        /// </summary>793        /// <param name="operationName"></param>794        /// <param name="parameterName"></param>795        /// <param name="authentication"></param>796        internal void ThrowInvalidAuthenticationTypeError(797            string operationName,798            string parameterName,799            PasswordAuthenticationMechanism authentication)800        {801            string message = string.Format(CultureInfo.CurrentUICulture, CimCmdletStrings.InvalidAuthenticationTypeWithNullCredential,802                authentication,803                ImpersonatedAuthenticationMechanism.None,804                ImpersonatedAuthenticationMechanism.Negotiate,805                ImpersonatedAuthenticationMechanism.Kerberos,806                ImpersonatedAuthenticationMechanism.NtlmDomain);807            PSArgumentOutOfRangeException exception = new(808                parameterName, authentication, message);809            ThrowTerminatingError(exception, operationName);810        }811 812        /// <summary>813        /// Throw conflict parameter error.814        /// </summary>815        /// <param name="operationName"></param>816        /// <param name="parameterName"></param>817        /// <param name="conflictParameterName"></param>818        internal void ThrowConflictParameterWasSet(819            string operationName,820            string parameterName,821            string conflictParameterName)822        {823            string message = string.Format(CultureInfo.CurrentUICulture,824                CimCmdletStrings.ConflictParameterWasSet,825                parameterName, conflictParameterName);826            PSArgumentException exception = new(message, parameterName);827            ThrowTerminatingError(exception, operationName);828        }829 830        /// <summary>831        /// <para>832        /// Throw not found property error833        /// </para>834        /// </summary>835        internal void ThrowInvalidProperty(836            IEnumerable<string> propertiesList,837            string className,838            string parameterName,839            string operationName,840            IDictionary actualValue)841        {842            StringBuilder propList = new();843            foreach (string property in propertiesList)844            {845                if (propList.Length > 0)846                {847                    propList.Append(',');848                }849 850                propList.Append(property);851            }852 853            string message = string.Format(CultureInfo.CurrentUICulture, CimCmdletStrings.CouldNotFindPropertyFromGivenClass,854                className, propList);855            PSArgumentOutOfRangeException exception = new(856                parameterName, actualValue, message);857            ThrowTerminatingError(exception, operationName);858        }859 860        /// <summary>861        /// Create credentials based on given authentication type and PSCredential.862        /// </summary>863        /// <param name="psCredentials"></param>864        /// <param name="passwordAuthentication"></param>865        /// <returns></returns>866        internal CimCredential CreateCimCredentials(PSCredential psCredentials,867            PasswordAuthenticationMechanism passwordAuthentication,868            string operationName,869            string parameterName)870        {871            DebugHelper.WriteLogEx("PSCredential:{0}; PasswordAuthenticationMechanism:{1}; operationName:{2}; parameterName:{3}.", 0, psCredentials, passwordAuthentication, operationName, parameterName);872 873            CimCredential credentials = null;874            if (psCredentials != null)875            {876                NetworkCredential networkCredential = psCredentials.GetNetworkCredential();877                DebugHelper.WriteLog("Domain:{0}; UserName:{1}; Password:{2}.", 1, networkCredential.Domain, networkCredential.UserName, psCredentials.Password);878                credentials = new CimCredential(passwordAuthentication, networkCredential.Domain, networkCredential.UserName, psCredentials.Password);879            }880            else881            {882                ImpersonatedAuthenticationMechanism impersonatedAuthentication;883                switch (passwordAuthentication)884                {885                    case PasswordAuthenticationMechanism.Default:886                        impersonatedAuthentication = ImpersonatedAuthenticationMechanism.None;887                        break;888                    case PasswordAuthenticationMechanism.Negotiate:889                        impersonatedAuthentication = ImpersonatedAuthenticationMechanism.Negotiate;890                        break;891                    case PasswordAuthenticationMechanism.Kerberos:892                        impersonatedAuthentication = ImpersonatedAuthenticationMechanism.Kerberos;893                        break;894                    case PasswordAuthenticationMechanism.NtlmDomain:895                        impersonatedAuthentication = ImpersonatedAuthenticationMechanism.NtlmDomain;896                        break;897                    default:898                        ThrowInvalidAuthenticationTypeError(operationName, parameterName, passwordAuthentication);899                        return null;900                }901 902                credentials = new CimCredential(impersonatedAuthentication);903            }904 905            DebugHelper.WriteLogEx("return credential {0}", 1, credentials);906            return credentials;907        }908        #endregion909    }910}911