Beispiel #1
0
        public static void ImportTestSettings(
            SettingsCmdletBase cmdlet,
            string path,
            string[] variableNames)
        {
            XElement wholeXML =
                XElement.Load(path);

            // default result
            Func<IEnumerable<string>, XElement, bool> query = (variableNamesCollection, variableElement) => true;

            cmdlet.WriteVerbose(cmdlet, "checking the VariableName list");
            if (null != variableNames && 0 < variableNames.Count()) {
                cmdlet.WriteVerbose(cmdlet, "the VariableName list is not empty");
                query = (variableNamesCollection, variableElement) => variableNamesCollection.Contains(variableElement.Attribute((XName)"name").Value);
            }

            cmdlet.WriteVerbose(cmdlet, "getting the variables collection");
            var variablesCollection =
                from variableElement in wholeXML.Elements()
                where query(((IEnumerable<string>)variableNames), variableElement)
                    select variableElement;

            cmdlet.WriteVerbose(cmdlet, "collection created");

            if (null == variablesCollection || 0 == variablesCollection.Count()) {
                cmdlet.WriteVerbose(cmdlet, "there are no variables to import");
                return;
            }

            TMXHelper.ImportVariables(cmdlet, variablesCollection);
        }
Beispiel #2
0
        public static bool ImportVariables(
            SettingsCmdletBase cmdlet,
            IEnumerable<XElement> variablesCollection)
        {
            bool result = false;

            foreach (XElement element in variablesCollection) {

                string variableName = string.Empty;

                try {

                    variableName =
                            element.Attribute((XName)"name").Value;

                    cmdlet.WriteVerbose(cmdlet, "importing the '" + variableName + "' variable");

                    string variableValue =
                        string.Empty;
                    try {
                        variableValue =
                            element.Attribute((XName)"value").Value;
                    }
                    catch {
                        // nothing to do
                    }

                    //ScopedItemOptions.AllScope
                    //ScopedItemOptions.Constant
                    //ScopedItemOptions.None
                    //ScopedItemOptions.Private
                    //ScopedItemOptions.ReadOnly
                    //ScopedItemOptions.Unspecified

                    PSVariable variable =
                        new PSVariable(
                            variableName,
                            variableValue);
                    cmdlet.SessionState.PSVariable.Set(variable);
                }
                catch (Exception eLoadingVariable) {
                    cmdlet.WriteError(
                        cmdlet,
                        "Unable to load variable '" +
                        variableName +
                        "'. " +
                        eLoadingVariable.Message,
                        "FailedToLoadVariable",
                        ErrorCategory.InvalidData,
                        false);
                }
            }

            return result;
        }
Beispiel #3
0
        public static void ExportTestSettings(
            SettingsCmdletBase cmdlet,
            string path,
            string[] variableNames)
        {
            try {
                XElement rootElement =
                    new XElement("variables");

                foreach (string variableName in variableNames) {
                    PSVariable variable =
                        cmdlet.SessionState.PSVariable.Get(variableName);
                    try {
                        if (null != variable.Name && string.Empty != variable.Name) {
                            XElement variableElement =
                                new XElement("variable",
                                             new XAttribute("name", variable.Name),
                                             new XAttribute("value", variable.Value));
                            rootElement.Add(variableElement);
                        }
                    }
                    catch (Exception eVariable) {
                        cmdlet.WriteError(
                            cmdlet,
                            "Unable to export variable '" +
                            variableName +
                            "'.",
                            "FailedToExportVariable",
                            ErrorCategory.InvalidArgument,
                            false);
                    }
                }

                System.Xml.Linq.XDocument document =
                    new System.Xml.Linq.XDocument();
                document.Add(rootElement);
                document.Save(path);
            }
            catch (Exception eCreateDocument) {
                cmdlet.WriteError(
                    cmdlet,
                    "Unable to save XML report to the file '" +
                    path +
                    "'. " +
                    eCreateDocument.Message,
                    "FailedToSaveReport",
                    ErrorCategory.InvalidOperation,
                    true);
            }
        }