Ejemplo n.º 1
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;
        }
Ejemplo n.º 2
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);
            }
        }