当前位置: 首页>>代码示例>>C#>>正文


C# CmdletProviderContext.WriteError方法代码示例

本文整理汇总了C#中System.Management.Automation.CmdletProviderContext.WriteError方法的典型用法代码示例。如果您正苦于以下问题:C# CmdletProviderContext.WriteError方法的具体用法?C# CmdletProviderContext.WriteError怎么用?C# CmdletProviderContext.WriteError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Management.Automation.CmdletProviderContext的用法示例。


在下文中一共展示了CmdletProviderContext.WriteError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: RemoveDrive

        } // RemoveDrive

        /// <summary>
        /// Removes the drive with the specified name.
        /// </summary>
        /// 
        /// <param name="driveName">
        /// The name of the drive to remove.
        /// </param>
        /// 
        /// <param name="force">
        /// Determines whether drive should be forcefully removed even if there was errors.
        /// </param>
        ///
        /// <param name="scopeID">
        /// The ID of the scope from which to remove the drive.
        /// If the scope ID is null or empty, the scope hierarchy will be searched
        /// starting at the current scope through all the parent scopes to the
        /// global scope until a drive of the given name is found to remove.
        /// </param>
        /// 
        /// <param name="context">
        /// The context of the command.
        /// </param>
        /// 
        internal void RemoveDrive(
            string driveName,
            bool force,
            string scopeID,
            CmdletProviderContext context)
        {
            if (driveName == null)
            {
                throw PSTraceSource.NewArgumentNullException("driveName");
            }

            Dbg.Diagnostics.Assert(
                context != null,
                "The caller should verify the context");

            PSDriveInfo drive = GetDrive(driveName, scopeID);

            if (drive == null)
            {
                DriveNotFoundException e = new DriveNotFoundException(
                    driveName,
                    "DriveNotFound",
                    SessionStateStrings.DriveNotFound);
                context.WriteError(new ErrorRecord(e.ErrorRecord, e));
            }
            else
            {
                RemoveDrive(drive, force, scopeID, context);
            }
        } // RemoveDrive
开发者ID:40a,项目名称:PowerShell,代码行数:55,代码来源:SessionStateDriveAPIs.cs

示例2: NewDrive

 internal void NewDrive(PSDriveInfo drive, string scopeID, CmdletProviderContext context)
 {
     if (drive == null)
     {
         throw PSTraceSource.NewArgumentNullException("drive");
     }
     if (context == null)
     {
         throw PSTraceSource.NewArgumentNullException("context");
     }
     if (!IsValidDriveName(drive.Name))
     {
         throw PSTraceSource.NewArgumentException("drive.Name", "SessionStateStrings", "DriveNameIllegalCharacters", new object[0]);
     }
     PSDriveInfo newDrive = this.ValidateDriveWithProvider(drive, context, true);
     if (newDrive != null)
     {
         if (string.Compare(newDrive.Name, drive.Name, true, Thread.CurrentThread.CurrentCulture) != 0)
         {
             throw this.NewProviderInvocationException("NewDriveProviderFailed", SessionStateStrings.NewDriveProviderFailed, drive.Provider, drive.Root, PSTraceSource.NewArgumentException("root"));
         }
         try
         {
             SessionStateScope currentScope = this.currentScope;
             if (!string.IsNullOrEmpty(scopeID))
             {
                 currentScope = this.GetScopeByID(scopeID);
             }
             currentScope.NewDrive(newDrive);
         }
         catch (ArgumentException exception2)
         {
             context.WriteError(new ErrorRecord(exception2, "NewDriveError", ErrorCategory.InvalidArgument, newDrive));
             return;
         }
         catch (SessionStateException)
         {
             throw;
         }
         if (this.ProvidersCurrentWorkingDrive[drive.Provider] == null)
         {
             this.ProvidersCurrentWorkingDrive[drive.Provider] = drive;
         }
         context.WriteObject(newDrive);
     }
 }
开发者ID:nickchal,项目名称:pash,代码行数:46,代码来源:SessionStateInternal.cs

示例3: WriteErrorsToContext

 internal void WriteErrorsToContext(CmdletProviderContext errorContext)
 {
     if (errorContext == null)
     {
         throw PSTraceSource.NewArgumentNullException("errorContext");
     }
     if (this.HasErrors())
     {
         foreach (ErrorRecord record in this.GetAccumulatedErrorObjects())
         {
             errorContext.WriteError(record);
         }
     }
 }
开发者ID:nickchal,项目名称:pash,代码行数:14,代码来源:CmdletProviderContext.cs

示例4: ValidateDriveWithProvider

        private PSDriveInfo ValidateDriveWithProvider(
            DriveCmdletProvider driveProvider,
            PSDriveInfo drive,
            CmdletProviderContext context,
            bool resolvePathIfPossible)
        {
            Dbg.Diagnostics.Assert(
                drive != null,
                "drive should have been validated by the caller");

            Dbg.Diagnostics.Assert(
                driveProvider != null,
                "driveProvider should have been validated by the caller");

            // Mark the drive as being created so that the provider can modify the
            // root if necessary

            drive.DriveBeingCreated = true;

            // Only try to resolve the root as an MSH path if there is a current drive.

            if (CurrentDrive != null && resolvePathIfPossible)
            {
                string newRoot = GetProviderRootFromSpecifiedRoot(drive.Root, drive.Provider);

                if (newRoot != null)
                {
                    drive.SetRoot(newRoot);
                }
            }

            PSDriveInfo result = null;

            try
            {
                result = driveProvider.NewDrive(drive, context);
            }
            catch (LoopFlowException)
            {
                throw;
            }
            catch (PipelineStoppedException)
            {
                throw;
            }
            catch (ActionPreferenceStopException)
            {
                throw;
            }
            catch (Exception e) // Catch-all OK, 3rd party callout
            {
                CommandProcessorBase.CheckForSevereException(e);
                ProviderInvocationException pie =
                    NewProviderInvocationException(
                        "NewDriveProviderException",
                        SessionStateStrings.NewDriveProviderException,
                        driveProvider.ProviderInfo,
                        drive.Root,
                        e);
                context.WriteError(
                    new ErrorRecord(
                        pie.ErrorRecord,
                        pie));
            }
            finally
            {
                drive.DriveBeingCreated = false;
            }
            return result;
        } // ValidateDriveWithProvider
开发者ID:40a,项目名称:PowerShell,代码行数:70,代码来源:SessionStateProviderAPIs.cs

示例5: GetChildNamesInDir


//.........这里部分代码省略.........
            getChildNamesContext.SetFilters(
                new Collection<string>(),
                new Collection<string>(),
                context.Filter);

            try
            {
                // Use the provider to get the children
                string unescapedDir = null;
                modifiedDirPath = null;

                if (dirIsProviderPath)
                {
                    modifiedDirPath = unescapedDir = context.SuppressWildcardExpansion ? dir : RemoveGlobEscaping(dir);
                }
                else
                {
                    Dbg.Diagnostics.Assert(
                        drive != null,
                        "Caller should verify that drive is not null when dirIsProviderPath is false");

                    // If the directory is an MSH path we must resolve it before calling GetChildNames()
                    // -- If the path is passed in by LiteralPath (context.SuppressWildcardExpansion == false), we surely should use 'dir' unchanged.
                    // -- If the path is passed in by Path (context.SuppressWildcardExpansion == true), we still should use 'dir' unchanged, in case that the special character
                    //    in 'dir' is escaped
                    modifiedDirPath = GetMshQualifiedPath(dir, drive);

                    ProviderInfo providerIgnored = null;
                    CmdletProvider providerInstanceIgnored = null;
                    Collection<string> resolvedPaths =
                        GetGlobbedProviderPathsFromMonadPath(
                            modifiedDirPath,
                            false,
                            getChildNamesContext,
                            out providerIgnored,
                            out providerInstanceIgnored);

                    // After resolving the path, we unescape the modifiedDirPath if necessary.
                    modifiedDirPath = context.SuppressWildcardExpansion
                                          ? modifiedDirPath
                                          : RemoveGlobEscaping(modifiedDirPath);
                    if (resolvedPaths.Count > 0)
                    {
                        unescapedDir = resolvedPaths[0];
                    }
                    else
                    {
                        // If there were no results from globbing but no
                        // exception was thrown, that means there was filtering.
                        // So return an empty collection and let the caller deal
                        // with it.

                        if (changedPathOrFilter)
                        {
                            context.Filter = originalFilter;
                        }

                        return new Collection<PSObject>();
                    }
                }

                if (provider.HasChildItems(unescapedDir, getChildNamesContext))
                {
                    provider.GetChildNames(
                        unescapedDir,
                        returnContainers,
                        getChildNamesContext);
                }

                // First check to see if there were any errors, and write them
                // to the real context if there are.

                if (getChildNamesContext.HasErrors())
                {
                    Collection<ErrorRecord> errors = getChildNamesContext.GetAccumulatedErrorObjects();

                    if (errors != null &&
                        errors.Count > 0)
                    {
                        foreach (ErrorRecord errorRecord in errors)
                        {
                            context.WriteError(errorRecord);
                        }
                    }
                }

                Collection<PSObject> childNamesObjectArray = getChildNamesContext.GetAccumulatedObjects();

                if (changedPathOrFilter)
                {
                    context.Filter = originalFilter;
                }

                return childNamesObjectArray;
            }
            finally
            {
                getChildNamesContext.RemoveStopReferral();
            }
        } // GetChildNamesInDir
开发者ID:40a,项目名称:PowerShell,代码行数:101,代码来源:LocationGlobber.cs

示例6: ValidateDriveWithProvider

 private PSDriveInfo ValidateDriveWithProvider(DriveCmdletProvider driveProvider, PSDriveInfo drive, CmdletProviderContext context, bool resolvePathIfPossible)
 {
     drive.DriveBeingCreated = true;
     if ((this.CurrentDrive != null) && resolvePathIfPossible)
     {
         string providerRootFromSpecifiedRoot = this.GetProviderRootFromSpecifiedRoot(drive.Root, drive.Provider);
         if (providerRootFromSpecifiedRoot != null)
         {
             drive.SetRoot(providerRootFromSpecifiedRoot);
         }
     }
     PSDriveInfo info = null;
     try
     {
         info = driveProvider.NewDrive(drive, context);
     }
     catch (LoopFlowException)
     {
         throw;
     }
     catch (PipelineStoppedException)
     {
         throw;
     }
     catch (ActionPreferenceStopException)
     {
         throw;
     }
     catch (Exception exception)
     {
         CommandProcessorBase.CheckForSevereException(exception);
         ProviderInvocationException replaceParentContainsErrorRecordException = this.NewProviderInvocationException("NewDriveProviderException", SessionStateStrings.NewDriveProviderException, driveProvider.ProviderInfo, drive.Root, exception);
         context.WriteError(new ErrorRecord(replaceParentContainsErrorRecordException.ErrorRecord, replaceParentContainsErrorRecordException));
     }
     finally
     {
         drive.DriveBeingCreated = false;
     }
     return info;
 }
开发者ID:nickchal,项目名称:pash,代码行数:40,代码来源:SessionStateInternal.cs

示例7: RenameItem

 internal void RenameItem(string path, string newName, CmdletProviderContext context)
 {
     if (path == null)
     {
         throw PSTraceSource.NewArgumentNullException("path");
     }
     ProviderInfo info = null;
     CmdletProvider providerInstance = null;
     Collection<string> targetObject = this.Globber.GetGlobbedProviderPathsFromMonadPath(path, false, context, out info, out providerInstance);
     if (targetObject.Count == 1)
     {
         this.RenameItem(providerInstance, targetObject[0], newName, context);
     }
     else
     {
         ArgumentException exception = PSTraceSource.NewArgumentException("path", "SessionStateStrings", "RenameMultipleItemError", new object[0]);
         context.WriteError(new ErrorRecord(exception, "RenameMultipleItemError", ErrorCategory.InvalidArgument, targetObject));
     }
 }
开发者ID:nickchal,项目名称:pash,代码行数:19,代码来源:SessionStateInternal.cs

示例8: RemoveDrive

 internal void RemoveDrive(string driveName, bool force, string scopeID, CmdletProviderContext context)
 {
     if (driveName == null)
     {
         throw PSTraceSource.NewArgumentNullException("driveName");
     }
     PSDriveInfo drive = this.GetDrive(driveName, scopeID);
     if (drive == null)
     {
         System.Management.Automation.DriveNotFoundException replaceParentContainsErrorRecordException = new System.Management.Automation.DriveNotFoundException(driveName, "DriveNotFound", SessionStateStrings.DriveNotFound);
         context.WriteError(new ErrorRecord(replaceParentContainsErrorRecordException.ErrorRecord, replaceParentContainsErrorRecordException));
     }
     else
     {
         this.RemoveDrive(drive, force, scopeID, context);
     }
 }
开发者ID:nickchal,项目名称:pash,代码行数:17,代码来源:SessionStateInternal.cs

示例9: NormalizeRelativePath

        } //NormalizeRelativePath

        /// <summary>
        /// Normalizes the path that was passed in and returns the normalized path
        /// as a relative path to the basePath that was passed.
        /// </summary>
        /// 
        /// <param name="path">
        /// An MSH path to an item. The item should exist
        /// or the provider should write out an error.
        /// </param>
        /// 
        /// <param name="basePath">
        /// The path that the return value should be relative to.
        /// </param>
        /// 
        /// <param name="context">
        /// The context under which the command is running.
        /// </param>
        /// 
        /// <returns>
        /// A normalized path that is relative to the basePath that was passed. 
        /// </returns>
        ///
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="path"/> is null.
        /// </exception>
        /// 
        /// <exception cref="NotSupportedException">
        /// If the <paramref name="providerInstance"/> does not support this operation.
        /// </exception>
        /// 
        /// <exception cref="PipelineStoppedException">
        /// If the pipeline is being stopped while executing the command.
        /// </exception>
        /// 
        /// <exception cref="ProviderInvocationException">
        /// If the provider threw an exception.
        /// </exception>
        /// 
        internal string NormalizeRelativePath(
            string path,
            string basePath,
            CmdletProviderContext context)
        {
            if (path == null)
            {
                throw PSTraceSource.NewArgumentNullException("path");
            }

            CmdletProviderContext getProviderPathContext =
                new CmdletProviderContext(context);

            try
            {
                PSDriveInfo drive = null;
                ProviderInfo provider = null;

                string workingPath = Globber.GetProviderPath(
                     path,
                     getProviderPathContext,
                     out provider,
                     out drive);

                if (getProviderPathContext.HasErrors())
                {
                    getProviderPathContext.WriteErrorsToContext(context);
                    return null;
                }

                if (workingPath == null ||
                    provider == null)
                {
                    // Since the provider didn't write an error, and we didn't get any
                    // results ourselves, we need to write out our own error.

                    Exception e = PSTraceSource.NewArgumentException("path");
                    context.WriteError(new ErrorRecord(e, "NormalizePathNullResult", ErrorCategory.InvalidArgument, path));
                    return null;
                }

                if (basePath != null)
                {
                    PSDriveInfo baseDrive = null;
                    ProviderInfo baseProvider = null;

                    Globber.GetProviderPath(
                         basePath,
                         getProviderPathContext,
                         out baseProvider,
                         out baseDrive);

                    if (drive != null && baseDrive != null)
                    {
                        if (!drive.Name.Equals(baseDrive.Name, StringComparison.OrdinalIgnoreCase))
                        {
                            // Make sure they are from physically different drives
                            // Doing StartsWith from both directions covers the following cases
                            // C:\ and C:\Temp
                            // C:\Temp and C:\
//.........这里部分代码省略.........
开发者ID:40a,项目名称:PowerShell,代码行数:101,代码来源:SessionStateNavigation.cs

示例10: isValidSession

        private bool isValidSession(PSSession session, CmdletProviderContext context, out PSLanguageMode? languageMode)
        {
            // session == null is validated by the parameter binding
            if (session.Availability != RunspaceAvailability.Available)
            {
                context.WriteError(new ErrorRecord(
                                    new InvalidOperationException(
                                        String.Format(System.Globalization.CultureInfo.InvariantCulture,
                                            SessionStateStrings.CopyItemSessionProperties,
                                            "Availability", session.Availability)),
                                            "SessionIsNotAvailable",
                                        ErrorCategory.InvalidOperation,
                                        session.Availability));

                languageMode = null;
                return false;
            }

            languageMode = session.Runspace.SessionStateProxy.LanguageMode;

            return true;
        }
开发者ID:dfinke,项目名称:powershell,代码行数:22,代码来源:SessionStateContainer.cs

示例11: MoveItem

        } // MoveItem

        /// <summary>
        /// Moves the item specified by path to the specified destination.
        /// </summary>
        /// 
        /// <param name="paths">
        /// The path(s) to the item(s) to be moved.
        /// </param>
        /// 
        /// <param name="destination">
        /// The path of the destination container.
        /// </param>
        /// 
        /// <param name="context">
        /// The context which the core command is running.
        /// </param>
        /// 
        /// <returns>
        /// Nothing. All items that are moved are written into the context object.
        /// </returns>
        /// 
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="path"/> is null.
        /// </exception>
        /// 
        /// <exception cref="ProviderNotFoundException">
        /// If the <paramref name="path"/> refers to a provider that could not be found.
        /// </exception>
        /// 
        /// <exception cref="DriveNotFoundException">
        /// If the <paramref name="path"/> refers to a drive that could not be found.
        /// </exception>
        /// 
        /// <exception cref="NotSupportedException">
        /// If the provider that the <paramref name="path"/> refers to does
        /// not support this operation.
        /// </exception>
        /// 
        /// <exception cref="ProviderInvocationException">
        /// If the provider threw an exception.
        /// </exception>
        /// 
        /// <exception cref="ItemNotFoundException">
        /// If <paramref name="path"/> does not contain glob characters and
        /// could not be found.
        /// </exception>
        /// 
        internal void MoveItem(
            string[] paths,
            string destination,
            CmdletProviderContext context)
        {
            if (paths == null)
            {
                throw PSTraceSource.NewArgumentNullException("paths");
            }

            if (destination == null)
            {
                throw PSTraceSource.NewArgumentNullException("destination");
            }

            ProviderInfo provider = null;
            CmdletProvider providerInstance = null;

            Collection<PathInfo> providerDestinationPaths =
                Globber.GetGlobbedMonadPathsFromMonadPath(
                    destination,
                    true,
                    context,
                    out providerInstance);

            if (providerDestinationPaths.Count > 1)
            {
                ArgumentException argException =
                    PSTraceSource.NewArgumentException(
                        "destination",
                        SessionStateStrings.MoveItemOneDestination);

                context.WriteError(new ErrorRecord(argException, argException.GetType().FullName, ErrorCategory.InvalidArgument, destination));
            }
            else
            {
                foreach (string path in paths)
                {
                    if (path == null)
                    {
                        throw PSTraceSource.NewArgumentNullException("paths");
                    }

                    Collection<string> providerPaths =
                        Globber.GetGlobbedProviderPathsFromMonadPath(
                            path,
                            false,
                            context,
                            out provider,
                            out providerInstance);

                    // Check to be sure we resolved at least one item to move and that the
//.........这里部分代码省略.........
开发者ID:40a,项目名称:PowerShell,代码行数:101,代码来源:SessionStateNavigation.cs

示例12: ValidateRemotePathAndGetRoot

        } // CopyItemDynamicParameters

        // This function validates a remote path, and if it exists, it returns the root path.
        //
        private string ValidateRemotePathAndGetRoot(string path, Runspaces.PSSession session, CmdletProviderContext context, PSLanguageMode? languageMode, bool sourceIsRemote)
        {
            Hashtable op = null;

            using (PowerShell ps = PowerShell.Create())
            {
                ps.Runspace = session.Runspace;

                // Check to see if the remote PSSession is running in constrained or no language mode and if so
                // then also if the path validation function already exists in the session (for the User drive
                // custom endpoint case).  Otherwise error out.
                if (languageMode.HasValue &&
                    (languageMode.Value == PSLanguageMode.ConstrainedLanguage || languageMode.Value == PSLanguageMode.NoLanguage))
                {
                    var psRemoteUtilsName = CopyFileRemoteUtils.PSCopyRemoteUtilsName;
                    ps.Runspace = session.Runspace;
                    ps.AddCommand("Get-Command").AddArgument(psRemoteUtilsName);
                    var result = ps.Invoke<bool>();

                    if (result.Count == 0)
                    {
                        context.WriteError(new ErrorRecord(
                            new InvalidOperationException(
                                String.Format(
                                    System.Globalization.CultureInfo.InvariantCulture,
                                    SessionStateStrings.CopyItemSessionProperties,
                                    "LanguageMode",
                                    session.Runspace.SessionStateProxy.LanguageMode)
                                ),
                                "SessionIsNotInFullLanguageMode",
                                ErrorCategory.InvalidOperation,
                                session.Availability
                            )
                        );

                        return null;
                    }

                    ps.Commands.Clear();
                    ps.Streams.ClearStreams();
                    ps.AddCommand(psRemoteUtilsName);
                }
                else
                {
                    string remoteScript = CopyFileRemoteUtils.PSValidatePathDefinition;
                    ps.AddScript(remoteScript);
                }

                ps.AddParameter("pathToValidate", path);

                if (sourceIsRemote)
                {
                    ps.AddParameter("sourceIsRemote", true);
                }

                op = Microsoft.PowerShell.Commands.SafeInvokeCommand.Invoke(ps, null, context);
            }

            if (op == null)
            {
                context.WriteError(new ErrorRecord(
                                new InvalidOperationException(
                                    String.Format(
                                    System.Globalization.CultureInfo.InvariantCulture, SessionStateStrings.CopyItemValidateRemotePath, path)),
                                    "FailedToValidateRemotePath",
                                    ErrorCategory.InvalidOperation,
                                    path));
                return null;
            }

            // If the remote path is not absolute, display an error to the user.
            if (op["IsAbsolute"] != null)
            {
                bool isAbsolute = (bool)op["IsAbsolute"];
                if (!isAbsolute)
                {
                    context.WriteError(new ErrorRecord(
                                        new ArgumentException(
                                            String.Format(
                                            System.Globalization.CultureInfo.InvariantCulture, SessionStateStrings.CopyItemRemotelyPathIsNotAbsolute, path)),
                                            "RemotePathIsNotAbsolute",
                                            ErrorCategory.InvalidArgument,
                                            path));
                    return null;
                }
            }

            bool pathExist = false;
            string root = null;

            if (op["Exists"] != null)
                pathExist = (bool)op["Exists"];

            if (op["Root"] != null)
                root = (string)op["Root"];

//.........这里部分代码省略.........
开发者ID:dfinke,项目名称:powershell,代码行数:101,代码来源:SessionStateContainer.cs

示例13: CopyItem

        } // CopyItem

        /// <summary>
        /// Copies an item at the specified path to an item at the <paramref name="copyPath" />.
        /// </summary>
        /// 
        /// <param name="paths">
        /// The path(s) of the item(s) to copy.
        /// </param>
        /// 
        /// <param name="copyPath">
        /// The path of the item to copy to.
        /// </param>
        /// 
        /// <param name="recurse">
        /// Tells the provider to recurse sub-containers when copying.
        /// </param>
        /// 
        /// <param name="copyContainers">
        /// Determines how the source container is used in the copy operation.
        /// </param>
        ///
        /// <param name="context">
        /// The context which the core command is running.
        /// </param>
        /// 
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="path"/> is null.
        /// </exception>
        /// 
        /// <exception cref="ProviderNotFoundException">
        /// If the <paramref name="path"/> refers to a provider that could not be found.
        /// </exception>
        /// 
        /// <exception cref="DriveNotFoundException">
        /// If the <paramref name="path"/> refers to a drive that could not be found.
        /// </exception>
        /// 
        /// <exception cref="NotSupportedException">
        /// If the provider that the <paramref name="path"/> refers to does
        /// not support this operation.
        /// </exception>
        /// 
        /// <exception cref="ProviderInvocationException">
        /// If the provider threw an exception.
        /// </exception>
        /// 
        /// <exception cref="ItemNotFoundException">
        /// If <paramref name="path"/> does not contain glob characters and
        /// could not be found.
        /// </exception>
        /// 
        internal void CopyItem(
            string[] paths,
            string copyPath,
            bool recurse,
            CopyContainers copyContainers,
            CmdletProviderContext context)
        {
            if (paths == null)
            {
                throw PSTraceSource.NewArgumentNullException("paths");
            }

            if (copyPath == null)
            {
                copyPath = String.Empty;
            }

            // Get the provider specific path for the destination

            PSDriveInfo unusedDrive = null;
            ProviderInfo destinationProvider = null;
            Microsoft.PowerShell.Commands.CopyItemDynamicParameters dynamicParams = context.DynamicParameters as Microsoft.PowerShell.Commands.CopyItemDynamicParameters;
            bool destinationIsRemote = false;
            bool sourceIsRemote = false;
            string providerDestinationPath;
            Runspaces.PSSession session = null;

            if (dynamicParams != null)
            {
                if (dynamicParams.FromSession != null)
                {
                    sourceIsRemote = true;
                    session = dynamicParams.FromSession;
                }
                if (dynamicParams.ToSession != null)
                {
                    destinationIsRemote = true;
                    session = dynamicParams.ToSession;
                }
            }

            if (sourceIsRemote && destinationIsRemote)
            {
                context.WriteError(new ErrorRecord(
                           new ArgumentException(
                               String.Format(System.Globalization.CultureInfo.InvariantCulture, SessionStateStrings.CopyItemFromSessionToSession, "FromSession", "ToSession")),
                               "InvalidInput",
                                ErrorCategory.InvalidArgument,
//.........这里部分代码省略.........
开发者ID:dfinke,项目名称:powershell,代码行数:101,代码来源:SessionStateContainer.cs

示例14: RenameItem

        } // RenameItem

        /// <summary>
        /// Renames the item at the specified path to the new name provided.
        /// </summary>
        /// 
        /// <param name="path">
        /// The path to the item to rename.
        /// </param>
        /// 
        /// <param name="newName">
        /// The name to which the item should be renamed. This name should always be 
        /// relative to the parent container.
        /// </param>
        /// 
        /// <param name="context">
        /// The context which the core command is running.
        /// </param>
        /// 
        /// <returns>
        /// Nothing. All items that are renamed are written into the context object.
        /// </returns>
        /// 
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="path"/> or <paramref name="propertyToClear"/> is null.
        /// </exception>
        /// 
        /// <exception cref="ProviderNotFoundException">
        /// If the <paramref name="path"/> refers to a provider that could not be found.
        /// </exception>
        /// 
        /// <exception cref="DriveNotFoundException">
        /// If the <paramref name="path"/> refers to a drive that could not be found.
        /// </exception>
        /// 
        /// <exception cref="NotSupportedException">
        /// If the provider that the <paramref name="path"/> refers to does
        /// not support this operation.
        /// </exception>
        /// 
        /// <exception cref="ProviderInvocationException">
        /// If the provider threw an exception.
        /// </exception>
        /// 
        /// <exception cref="ItemNotFoundException">
        /// If <paramref name="path"/> does not contain glob characters and
        /// could not be found.
        /// </exception>
        /// 
        internal void RenameItem(
            string path,
            string newName,
            CmdletProviderContext context)
        {
            if (path == null)
            {
                throw PSTraceSource.NewArgumentNullException("path");
            }

            ProviderInfo provider = null;
            CmdletProvider providerInstance = null;

            Collection<string> providerPaths =
                Globber.GetGlobbedProviderPathsFromMonadPath(
                    path,
                    false,
                    context,
                    out provider,
                    out providerInstance);

            // Can only rename one item at a time, so if we glob more than 
            // one item write out an error.

            if (providerPaths.Count == 1)
            {
                RenameItem(providerInstance, providerPaths[0], newName, context);
            }
            else
            {
                ArgumentException argException =
                    PSTraceSource.NewArgumentException(
                        "path",
                        SessionStateStrings.RenameMultipleItemError);

                context.WriteError(
                    new ErrorRecord(
                        argException,
                        "RenameMultipleItemError",
                        ErrorCategory.InvalidArgument,
                        providerPaths));
            }
        } // RenameItem
开发者ID:dfinke,项目名称:powershell,代码行数:92,代码来源:SessionStateContainer.cs

示例15: NormalizeRelativePath

 internal string NormalizeRelativePath(string path, string basePath, CmdletProviderContext context)
 {
     string str2;
     if (path == null)
     {
         throw PSTraceSource.NewArgumentNullException("path");
     }
     CmdletProviderContext context2 = new CmdletProviderContext(context);
     try
     {
         PSDriveInfo drive = null;
         ProviderInfo provider = null;
         string str = this.Globber.GetProviderPath(path, context2, out provider, out drive);
         if (context2.HasErrors())
         {
             context2.WriteErrorsToContext(context);
             return null;
         }
         if ((str == null) || (provider == null))
         {
             Exception exception = PSTraceSource.NewArgumentException("path");
             context.WriteError(new ErrorRecord(exception, "NormalizePathNullResult", ErrorCategory.InvalidArgument, path));
             return null;
         }
         if (drive != null)
         {
             context.Drive = drive;
             if (((this.GetProviderInstance(provider) is NavigationCmdletProvider) && !string.IsNullOrEmpty(drive.Root)) && path.StartsWith(drive.Root, StringComparison.OrdinalIgnoreCase))
             {
                 str = path;
             }
         }
         str2 = this.NormalizeRelativePath(provider, str, basePath, context);
     }
     finally
     {
         context2.RemoveStopReferral();
     }
     return str2;
 }
开发者ID:nickchal,项目名称:pash,代码行数:40,代码来源:SessionStateInternal.cs


注:本文中的System.Management.Automation.CmdletProviderContext.WriteError方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。