コード例 #1
0
        private bool VerifyShadowingExistingCommandsAndWriteError(string aliasName)
        {
            CommandSearcher searcher = new CommandSearcher(aliasName, SearchResolutionOptions.None, CommandTypes.All ^ CommandTypes.Alias, this.Context);

            foreach (string expandedCommandName in searcher.ConstructSearchPatternsFromName(aliasName))
            {
                CommandTypes commandTypeOfExistingCommand;
                if (this.ExistingCommands.TryGetValue(expandedCommandName, out commandTypeOfExistingCommand))
                {
                    // Since the alias already exists, write an error.
                    SessionStateException aliasExists =
                        new SessionStateException(
                            aliasName,
                            SessionStateCategory.Alias,
                            "AliasAlreadyExists",
                            SessionStateStrings.AliasWithCommandNameAlreadyExists,
                            ErrorCategory.ResourceExists,
                            commandTypeOfExistingCommand);

                    WriteError(
                        new ErrorRecord(
                            aliasExists.ErrorRecord,
                            aliasExists));
                    return(true);
                }
            }

            return(false);
        }
コード例 #2
0
ファイル: VariableCommandBase.cs プロジェクト: zaxebo1/Pash
        protected internal void WriteError(SessionStateException ex)
        {
            string errorId = String.Format("{0},{1}", ex.ErrorRecord.ErrorId, GetType().FullName);
            var    error   = new ErrorRecord(ex, errorId, ex.ErrorRecord.CategoryInfo.Category, ex.ItemName);

            error.CategoryInfo.Activity = GetActivityName();

            WriteError(error);
        }
コード例 #3
0
ファイル: NewVariableCommand.cs プロジェクト: zaxebo1/Pash
        private void WriteVariableAlreadyExistsError(PSVariable variable)
        {
            var ex = new SessionStateException(
                String.Format("A variable with name '{0}' already exists.", variable.Name),
                variable.Name,
                SessionStateCategory.Variable);

            string errorId = String.Format("VariableAlreadyExists,{0}", typeof(NewVariableCommand).FullName);
            var    error   = new ErrorRecord(ex, errorId, ErrorCategory.ResourceExists, variable.Name);

            error.CategoryInfo.Activity = "New-Variable";

            WriteError(error);
        }
コード例 #4
0
        private bool VerifyShadowingExistingCommandsAndWriteError(string aliasName)
        {
            CommandSearcher searcher = new CommandSearcher(aliasName, SearchResolutionOptions.None, CommandTypes.Workflow | CommandTypes.Script | CommandTypes.Application | CommandTypes.ExternalScript | CommandTypes.Cmdlet | CommandTypes.Filter | CommandTypes.Function, base.Context);

            foreach (string str in searcher.ConstructSearchPatternsFromName(aliasName))
            {
                CommandTypes types;
                if (this.ExistingCommands.TryGetValue(str, out types))
                {
                    SessionStateException replaceParentContainsErrorRecordException = new SessionStateException(aliasName, SessionStateCategory.Alias, "AliasAlreadyExists", SessionStateStrings.AliasWithCommandNameAlreadyExists, ErrorCategory.ResourceExists, new object[] { types });
                    base.WriteError(new ErrorRecord(replaceParentContainsErrorRecordException.ErrorRecord, replaceParentContainsErrorRecordException));
                    return(true);
                }
            }
            return(false);
        }
コード例 #5
0
ファイル: Var.cs プロジェクト: Syed-Hassan46/powershell
        /// <summary>
        /// Add objects received on the pipeline to an ArrayList of values, to
        /// take the place of the Value parameter if none was specified on the
        /// command line.
        /// </summary>
        protected override void ProcessRecord()
        {
            // If Force is not specified, see if the variable already exists
            // in the specified scope. If the scope isn't specified, then
            // check to see if it exists in the current scope.

            if (!Force)
            {
                PSVariable varFound = null;
                if (string.IsNullOrEmpty(Scope))
                {
                    varFound =
                        SessionState.PSVariable.GetAtScope(Name, "local");
                }
                else
                {
                    varFound =
                        SessionState.PSVariable.GetAtScope(Name, Scope);
                }

                if (varFound != null)
                {
                    SessionStateException sessionStateException =
                        new SessionStateException(
                            Name,
                            SessionStateCategory.Variable,
                            "VariableAlreadyExists",
                            SessionStateStrings.VariableAlreadyExists,
                            ErrorCategory.ResourceExists);

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

            // Since the variable doesn't exist or -Force was specified,
            // Call should process to validate the set with the user.

            string action = VariableCommandStrings.NewVariableAction;

            string target = StringUtil.Format(VariableCommandStrings.NewVariableTarget, Name, Value);

            if (ShouldProcess(target, action))
            {
                PSVariable newVariable = new PSVariable(Name, Value, Option);

                if (_visibility != null)
                {
                    newVariable.Visibility = (SessionStateEntryVisibility)_visibility;
                }

                if (Description != null)
                {
                    newVariable.Description = Description;
                }

                try
                {
                    if (string.IsNullOrEmpty(Scope))
                    {
                        SessionState.Internal.NewVariable(newVariable, Force);
                    }
                    else
                    {
                        SessionState.Internal.NewVariableAtScope(newVariable, Scope, Force);
                    }
                }
                catch (SessionStateException sessionStateException)
                {
                    WriteError(
                        new ErrorRecord(
                            sessionStateException.ErrorRecord,
                            sessionStateException));
                    return;
                }
                catch (PSArgumentException argException)
                {
                    WriteError(
                        new ErrorRecord(
                            argException.ErrorRecord,
                            argException));
                    return;
                }

                if (_passThru)
                {
                    WriteObject(newVariable);
                }
            }
        }
コード例 #6
0
        /// <summary>
        /// The main processing loop of the command.
        /// </summary>
        ///
        protected override void ProcessRecord()
        {
            // If not force, then see if the alias already exists

            if (!Force)
            {
                AliasInfo existingAlias = null;
                if (String.IsNullOrEmpty(Scope))
                {
                    existingAlias = SessionState.Internal.GetAlias(Name);
                }
                else
                {
                    existingAlias = SessionState.Internal.GetAliasAtScope(Name, Scope);
                }

                if (existingAlias != null)
                {
                    // Throw if alias exists and is private...
                    SessionState.ThrowIfNotVisible(this.CommandOrigin, existingAlias);

                    // Since the alias already exists, write an error.

                    SessionStateException aliasExists =
                        new SessionStateException(
                            Name,
                            SessionStateCategory.Alias,
                            "AliasAlreadyExists",
                            SessionStateStrings.AliasAlreadyExists,
                            ErrorCategory.ResourceExists);

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

            // Create the alias info

            AliasInfo newAlias =
                new AliasInfo(
                    Name,
                    Value,
                    Context,
                    Option);

            newAlias.Description = Description;

            string action =
                AliasCommandStrings.NewAliasAction;

            string target =
                StringUtil.Format(AliasCommandStrings.NewAliasTarget, Name, Value);

            if (ShouldProcess(target, action))
            {
                // Set the alias in the specified scope or the
                // current scope.

                AliasInfo result = null;

                try
                {
                    if (String.IsNullOrEmpty(Scope))
                    {
                        result = SessionState.Internal.SetAliasItem(newAlias, Force, MyInvocation.CommandOrigin);
                    }
                    else
                    {
                        result = SessionState.Internal.SetAliasItemAtScope(newAlias, Scope, Force, MyInvocation.CommandOrigin);
                    }
                }
                catch (SessionStateException sessionStateException)
                {
                    WriteError(
                        new ErrorRecord(
                            sessionStateException.ErrorRecord,
                            sessionStateException));
                    return;
                }
                catch (PSArgumentOutOfRangeException argOutOfRange)
                {
                    WriteError(
                        new ErrorRecord(
                            argOutOfRange.ErrorRecord,
                            argOutOfRange));
                    return;
                }
                catch (PSArgumentException argException)
                {
                    WriteError(
                        new ErrorRecord(
                            argException.ErrorRecord,
                            argException));
                    return;
                }

                // Write the alias to the pipeline if PassThru was specified

                if (PassThru && result != null)
                {
                    WriteObject(result);
                }
            }
        } // ProcessRecord
コード例 #7
0
        /// <summary>
        /// The main processing loop of the command.
        /// </summary>
        ///
        protected override void ProcessRecord()
        {
            Collection <AliasInfo> importedAliases = GetAliasesFromFile(this.ParameterSetName.Equals(LiteralPathParameterSetName,
                                                                                                     StringComparison.OrdinalIgnoreCase));

            CommandOrigin origin = MyInvocation.CommandOrigin;

            foreach (AliasInfo alias in importedAliases)
            {
                // If not force, then see if the alias already exists

                // NTRAID#Windows Out Of Band Releases-906910-2006/03/17-JonN
                string action = AliasCommandStrings.ImportAliasAction;

                string target = StringUtil.Format(AliasCommandStrings.ImportAliasTarget, alias.Name, alias.Definition);

                if (!ShouldProcess(target, action))
                {
                    continue;
                }

                if (!Force)
                {
                    AliasInfo existingAlias = null;
                    if (String.IsNullOrEmpty(Scope))
                    {
                        existingAlias = SessionState.Internal.GetAlias(alias.Name);
                    }
                    else
                    {
                        existingAlias = SessionState.Internal.GetAliasAtScope(alias.Name, Scope);
                    }

                    if (existingAlias != null)
                    {
                        // Write an error for aliases that aren't visible...
                        try
                        {
                            SessionState.ThrowIfNotVisible(origin, existingAlias);
                        }
                        catch (SessionStateException sessionStateException)
                        {
                            WriteError(
                                new ErrorRecord(
                                    sessionStateException.ErrorRecord,
                                    sessionStateException));
                            // Only report the error once...
                            continue;
                        }

                        // Since the alias already exists, write an error.

                        SessionStateException aliasExists =
                            new SessionStateException(
                                alias.Name,
                                SessionStateCategory.Alias,
                                "AliasAlreadyExists",
                                SessionStateStrings.AliasAlreadyExists,
                                ErrorCategory.ResourceExists);

                        WriteError(
                            new ErrorRecord(
                                aliasExists.ErrorRecord,
                                aliasExists));
                        continue;
                    }

                    if (VerifyShadowingExistingCommandsAndWriteError(alias.Name))
                    {
                        continue;
                    }
                } // if (!Force)

                // Set the alias in the specified scope or the
                // current scope.

                AliasInfo result = null;

                try
                {
                    if (String.IsNullOrEmpty(Scope))
                    {
                        result = SessionState.Internal.SetAliasItem(alias, Force, MyInvocation.CommandOrigin);
                    }
                    else
                    {
                        result = SessionState.Internal.SetAliasItemAtScope(alias, Scope, Force, MyInvocation.CommandOrigin);
                    }
                }
                catch (SessionStateException sessionStateException)
                {
                    WriteError(
                        new ErrorRecord(
                            sessionStateException.ErrorRecord,
                            sessionStateException));
                    continue;
                }
                catch (PSArgumentOutOfRangeException argOutOfRange)
                {
                    WriteError(
                        new ErrorRecord(
                            argOutOfRange.ErrorRecord,
                            argOutOfRange));
                    continue;
                }
                catch (PSArgumentException argException)
                {
                    WriteError(
                        new ErrorRecord(
                            argException.ErrorRecord,
                            argException));
                    continue;
                }

                // Write the alias to the pipeline if PassThru was specified

                if (PassThru && result != null)
                {
                    WriteObject(result);
                }
            }
        } // ProcessRecord
コード例 #8
0
        protected override void ProcessRecord()
        {
            Collection <AliasInfo> aliasesFromFile = this.GetAliasesFromFile(base.ParameterSetName.Equals("ByLiteralPath", StringComparison.OrdinalIgnoreCase));
            CommandOrigin          commandOrigin   = base.MyInvocation.CommandOrigin;

            foreach (AliasInfo info in aliasesFromFile)
            {
                string importAliasAction = AliasCommandStrings.ImportAliasAction;
                string target            = StringUtil.Format(AliasCommandStrings.ImportAliasTarget, info.Name, info.Definition);
                if (base.ShouldProcess(target, importAliasAction))
                {
                    if (this.Force == 0)
                    {
                        AliasInfo valueToCheck = null;
                        if (string.IsNullOrEmpty(this.Scope))
                        {
                            valueToCheck = base.SessionState.Internal.GetAlias(info.Name);
                        }
                        else
                        {
                            valueToCheck = base.SessionState.Internal.GetAliasAtScope(info.Name, this.Scope);
                        }
                        if (valueToCheck != null)
                        {
                            try
                            {
                                SessionState.ThrowIfNotVisible(commandOrigin, valueToCheck);
                            }
                            catch (SessionStateException exception)
                            {
                                base.WriteError(new ErrorRecord(exception.ErrorRecord, exception));
                                continue;
                            }
                            SessionStateException replaceParentContainsErrorRecordException = new SessionStateException(info.Name, SessionStateCategory.Alias, "AliasAlreadyExists", SessionStateStrings.AliasAlreadyExists, ErrorCategory.ResourceExists, new object[0]);
                            base.WriteError(new ErrorRecord(replaceParentContainsErrorRecordException.ErrorRecord, replaceParentContainsErrorRecordException));
                            continue;
                        }
                        if (this.VerifyShadowingExistingCommandsAndWriteError(info.Name))
                        {
                            continue;
                        }
                    }
                    AliasInfo sendToPipeline = null;
                    try
                    {
                        if (string.IsNullOrEmpty(this.Scope))
                        {
                            sendToPipeline = base.SessionState.Internal.SetAliasItem(info, (bool)this.Force, base.MyInvocation.CommandOrigin);
                        }
                        else
                        {
                            sendToPipeline = base.SessionState.Internal.SetAliasItemAtScope(info, this.Scope, (bool)this.Force, base.MyInvocation.CommandOrigin);
                        }
                    }
                    catch (SessionStateException exception3)
                    {
                        base.WriteError(new ErrorRecord(exception3.ErrorRecord, exception3));
                        continue;
                    }
                    catch (PSArgumentOutOfRangeException exception4)
                    {
                        base.WriteError(new ErrorRecord(exception4.ErrorRecord, exception4));
                        continue;
                    }
                    catch (PSArgumentException exception5)
                    {
                        base.WriteError(new ErrorRecord(exception5.ErrorRecord, exception5));
                        continue;
                    }
                    if ((this.PassThru != 0) && (sendToPipeline != null))
                    {
                        base.WriteObject(sendToPipeline);
                    }
                }
            }
        }
コード例 #9
0
        protected override void ProcessRecord()
        {
            ProviderInfo singleProvider = null;

            try
            {
                singleProvider = base.SessionState.Internal.GetSingleProvider(this.PSProvider);
            }
            catch (ProviderNotFoundException providerNotFoundException1)
            {
                ProviderNotFoundException providerNotFoundException = providerNotFoundException1;
                base.WriteError(new ErrorRecord(providerNotFoundException.ErrorRecord, providerNotFoundException));
                return;
            }
            if (singleProvider != null)
            {
                string   newDriveConfirmAction           = NavigationResources.NewDriveConfirmAction;
                string   newDriveConfirmResourceTemplate = NavigationResources.NewDriveConfirmResourceTemplate;
                object[] name = new object[3];
                name[0] = this.Name;
                name[1] = singleProvider.FullName;
                name[2] = this.Root;
                string str = string.Format(Thread.CurrentThread.CurrentCulture, newDriveConfirmResourceTemplate, name);
                if (base.ShouldProcess(str, newDriveConfirmAction))
                {
                    if (this.Persist && !singleProvider.Name.Equals("FileSystem", StringComparison.OrdinalIgnoreCase))
                    {
                        ErrorRecord errorRecord = new ErrorRecord(new NotSupportedException(FileSystemProviderStrings.PersistNotSupported), "DriveRootNotNetworkPath", ErrorCategory.InvalidArgument, this);
                        base.ThrowTerminatingError(errorRecord);
                    }
                    PSDriveInfo pSDriveInfo = new PSDriveInfo(this.Name, singleProvider, this.Root, this.Description, base.Credential, this.Persist);
                    try
                    {
                        base.SessionState.Drive.New(pSDriveInfo, this.Scope, this.CmdletProviderContext);
                    }
                    catch (PSNotSupportedException pSNotSupportedException1)
                    {
                        PSNotSupportedException pSNotSupportedException = pSNotSupportedException1;
                        base.WriteError(new ErrorRecord(pSNotSupportedException.ErrorRecord, pSNotSupportedException));
                    }
                    catch (DriveNotFoundException driveNotFoundException1)
                    {
                        DriveNotFoundException driveNotFoundException = driveNotFoundException1;
                        base.WriteError(new ErrorRecord(driveNotFoundException.ErrorRecord, driveNotFoundException));
                    }
                    catch (ProviderNotFoundException providerNotFoundException3)
                    {
                        ProviderNotFoundException providerNotFoundException2 = providerNotFoundException3;
                        base.WriteError(new ErrorRecord(providerNotFoundException2.ErrorRecord, providerNotFoundException2));
                    }
                    catch (PSArgumentException pSArgumentException1)
                    {
                        PSArgumentException pSArgumentException = pSArgumentException1;
                        base.WriteError(new ErrorRecord(pSArgumentException.ErrorRecord, pSArgumentException));
                    }
                    catch (ItemNotFoundException itemNotFoundException1)
                    {
                        ItemNotFoundException itemNotFoundException = itemNotFoundException1;
                        base.WriteError(new ErrorRecord(itemNotFoundException.ErrorRecord, itemNotFoundException));
                    }
                    catch (SessionStateOverflowException sessionStateOverflowException)
                    {
                        throw;
                    }
                    catch (SessionStateException sessionStateException1)
                    {
                        SessionStateException sessionStateException = sessionStateException1;
                        base.WriteError(new ErrorRecord(sessionStateException.ErrorRecord, sessionStateException));
                    }
                }
            }
        }
コード例 #10
0
ファイル: NewAliasCommand.cs プロジェクト: modulexcite/pash-1
        protected override void ProcessRecord()
        {
            if (base.Force == 0)
            {
                AliasInfo valueToCheck = null;
                if (string.IsNullOrEmpty(base.Scope))
                {
                    valueToCheck = base.SessionState.Internal.GetAlias(base.Name);
                }
                else
                {
                    valueToCheck = base.SessionState.Internal.GetAliasAtScope(base.Name, base.Scope);
                }
                if (valueToCheck != null)
                {
                    SessionState.ThrowIfNotVisible(base.CommandOrigin, valueToCheck);
                    SessionStateException replaceParentContainsErrorRecordException = new SessionStateException(base.Name, SessionStateCategory.Alias, "AliasAlreadyExists", SessionStateStrings.AliasAlreadyExists, ErrorCategory.ResourceExists, new object[0]);
                    base.WriteError(new ErrorRecord(replaceParentContainsErrorRecordException.ErrorRecord, replaceParentContainsErrorRecordException));
                    return;
                }
            }
            AliasInfo alias = new AliasInfo(base.Name, base.Value, base.Context, base.Option)
            {
                Description = base.Description
            };
            string newAliasAction = AliasCommandStrings.NewAliasAction;
            string target         = StringUtil.Format(AliasCommandStrings.NewAliasTarget, base.Name, base.Value);

            if (base.ShouldProcess(target, newAliasAction))
            {
                AliasInfo sendToPipeline = null;
                try
                {
                    if (string.IsNullOrEmpty(base.Scope))
                    {
                        sendToPipeline = base.SessionState.Internal.SetAliasItem(alias, (bool)base.Force, base.MyInvocation.CommandOrigin);
                    }
                    else
                    {
                        sendToPipeline = base.SessionState.Internal.SetAliasItemAtScope(alias, base.Scope, (bool)base.Force, base.MyInvocation.CommandOrigin);
                    }
                }
                catch (SessionStateException exception2)
                {
                    base.WriteError(new ErrorRecord(exception2.ErrorRecord, exception2));
                    return;
                }
                catch (PSArgumentOutOfRangeException exception3)
                {
                    base.WriteError(new ErrorRecord(exception3.ErrorRecord, exception3));
                    return;
                }
                catch (PSArgumentException exception4)
                {
                    base.WriteError(new ErrorRecord(exception4.ErrorRecord, exception4));
                    return;
                }
                if ((base.PassThru != 0) && (sendToPipeline != null))
                {
                    base.WriteObject(sendToPipeline);
                }
            }
        }
コード例 #11
0
        protected override void ProcessRecord()
        {
            if (this.Force == 0)
            {
                PSVariable atScope = null;
                if (string.IsNullOrEmpty(base.Scope))
                {
                    atScope = base.SessionState.PSVariable.GetAtScope(this.name, "local");
                }
                else
                {
                    atScope = base.SessionState.PSVariable.GetAtScope(this.name, base.Scope);
                }
                if (atScope != null)
                {
                    SessionStateException replaceParentContainsErrorRecordException = new SessionStateException(this.name, SessionStateCategory.Variable, "VariableAlreadyExists", SessionStateStrings.VariableAlreadyExists, ErrorCategory.ResourceExists, new object[0]);
                    base.WriteError(new ErrorRecord(replaceParentContainsErrorRecordException.ErrorRecord, replaceParentContainsErrorRecordException));
                    return;
                }
            }
            string newVariableAction = VariableCommandStrings.NewVariableAction;
            string target            = StringUtil.Format(VariableCommandStrings.NewVariableTarget, this.Name, this.Value);

            if (base.ShouldProcess(target, newVariableAction))
            {
                PSVariable variable = new PSVariable(this.name, this._value, this.options);
                if (this._visibility.HasValue)
                {
                    variable.Visibility = this._visibility.Value;
                }
                if (this.description != null)
                {
                    variable.Description = this.description;
                }
                try
                {
                    if (string.IsNullOrEmpty(base.Scope))
                    {
                        base.SessionState.Internal.NewVariable(variable, (bool)this.Force);
                    }
                    else
                    {
                        base.SessionState.Internal.NewVariableAtScope(variable, base.Scope, (bool)this.Force);
                    }
                }
                catch (SessionStateException exception2)
                {
                    base.WriteError(new ErrorRecord(exception2.ErrorRecord, exception2));
                    return;
                }
                catch (PSArgumentException exception3)
                {
                    base.WriteError(new ErrorRecord(exception3.ErrorRecord, exception3));
                    return;
                }
                if (this.passThru)
                {
                    base.WriteObject(variable);
                }
            }
        }