Exemple #1
0
        /// <summary>
        /// get-item cmdlet callback
        /// </summary>
        /// <param name="path"></param>
        protected override void GetItem(string path)
        {
            WriteVerbose(string.Format("XmlItemProvider::GetItem(Path = '{0}')", path));
            string npath = XmlProviderUtils.NormalizePath(path);
            string xpath = XmlProviderUtils.PathNoDrive(npath);

            XmlDriveInfo drive = XmlProviderUtils.GetDriveFromPath(path, base.ProviderInfo);

            if (drive == null)
            {
                ErrorRecord error = new ErrorRecord(new InvalidProgramException("Unable to retrieve the drive for this path"),
                                                    "drive", ErrorCategory.InvalidData, null);
                WriteError(error);
            }

            XmlDocument xml   = drive.XmlDocument;
            XmlNodeList nodes = xml.SelectNodes(xpath, drive.NamespaceManager);

            // ------------------------------
            // NOTE: We could throw an ItemNotFoundException here if the nodelist returned is null
            // or empty. In this case I decided not to because the fact that get-item returns
            // nothing indicates that case. For other operations such as clear-item or set-item
            // it is a good idea to throw that exception to indicate specifically why you're unable to
            // perform the action.
            // ------------------------------

            foreach (XmlNode node in nodes)
            {
                WriteItemObject(node, path, false);
            }
        }
Exemple #2
0
        //clear-item, get-item, invoke-item, set-item
        // resolve-path, test-path
        #region ItemCmdletProvider overrides

        /// <summary>
        /// clear-item cmdlet callback
        /// </summary>
        /// <param name="path"></param>
        protected override void ClearItem(string path)
        {
            WriteVerbose(string.Format("XmlItemProvider::ClearItem(Path = '{0}')", path));

            string npath = XmlProviderUtils.NormalizePath(path);
            string xpath = XmlProviderUtils.PathNoDrive(npath);

            XmlNodeList nodes = GetXmlNodesFromPath(xpath);

            // throw terminating error if we can't find any items at path
            // ------------------------------------------------
            if (nodes == null || nodes.Count == 0)
            {
                ErrorRecord error = new ErrorRecord(new ItemNotFoundException(),
                                                    "ItemNotFound", ErrorCategory.ObjectNotFound, null);
                ThrowTerminatingError(error);
            }

            foreach (XmlNode node in nodes)
            {
                // ShouldProcess() enables use of -whatif & -confirm flags for clear-item
                // If path returns more than a single XMLNode, we call ShouldProcess() for each
                // node not one call to ShouldProcess for the entire operation
                // -----------------------------------------------------------
                if (base.ShouldProcess(node.Name))
                {
                    node.RemoveAll();
                }
            }
        }
Exemple #3
0
        public XmlNode GetSingleXmlNodeFromPath(string path)
        {
            string       npath = XmlProviderUtils.NormalizePath(path);
            string       xpath = XmlProviderUtils.PathNoDrive(npath);
            XmlDriveInfo drive = base.PSDriveInfo as XmlDriveInfo;

            if (drive == null)
            {
                ErrorRecord error = new ErrorRecord(new InvalidProgramException("Unable to retrieve the drive for this path"),
                                                    "drive", ErrorCategory.InvalidData, null);
                ThrowTerminatingError(error);
            }
            XmlDocument xml = drive.XmlDocument;

            return(xml.SelectSingleNode(xpath));
        }
Exemple #4
0
        private XmlNodeList GetXmlNodesFromPath(string path)
        {
            string npath = XmlProviderUtils.NormalizePath(path);
            string xpath = XmlProviderUtils.PathNoDrive(npath);

            XmlDriveInfo drive = XmlProviderUtils.GetDriveFromPath(path, base.ProviderInfo);

            if (drive == null)
            {
                ErrorRecord error = new ErrorRecord(new InvalidProgramException("Unable to retrieve the drive for this path"),
                                                    "drive", ErrorCategory.InvalidData, null);
                WriteError(error);
                return(null);
            }
            XmlDocument xml = drive.XmlDocument;

            return(xml.SelectNodes(xpath));
        }
Exemple #5
0
        /// <summary>
        /// test-path cmdlet callback
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        protected override bool ItemExists(string path)
        {
            WriteVerbose(string.Format("XmlItemProvider::ItemExists(Path = '{0}')", path));

            string npath = XmlProviderUtils.NormalizePath(path);
            string xpath = XmlProviderUtils.PathNoDrive(npath);

            XmlDriveInfo drive = XmlProviderUtils.GetDriveFromPath(path, base.ProviderInfo);

            if (drive == null)
            {
                return(false);
            }
            XmlDocument xml = drive.XmlDocument;

            if (xml.SelectSingleNode(xpath, drive.NamespaceManager) == null)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }