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; }
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); }