GetVariable() private method

private GetVariable ( string name ) : PSVariable
name string
return System.Management.Automation.PSVariable
Esempio n. 1
0
        public object GetVariable(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new NullReferenceException("Variable name can't be empty.");
            }

            return(ExecutionContext.GetVariable(name));
        }
Esempio n. 2
0
        private bool?GetPSForceSynchronizeProcessOutput()
        {
            var variable = ExecutionContext.GetVariable(PashVariables.ForceSynchronizeProcessOutput) as PSVariable;

            if (variable == null)
            {
                return(null);
            }

            var value    = variable.Value;
            var psObject = value as PSObject;

            if (psObject == null)
            {
                return(value as bool?);
            }
            else
            {
                return(psObject.BaseObject as bool?);
            }
        }
Esempio n. 3
0
    private static PSPropertyInfo EvaluateProperty(object property, PSObject psobj, ExecutionContext executionContext)
	{
        property = PSObject.Unwrap(property);
		var propName = property as string;
		if (propName != null)
		{
			var prop = psobj.Properties[propName];
			return prop ?? new PSNoteProperty(propName, "");
		}
		// check if it's a Hashtable. We can then have expression=<ScriptBlock> and label=<string> or name=<string>
		var hashtable = property as Hashtable;
		if (hashtable != null)
		{
            // we have a strange logice here, but we'll just do it as Powershell
            // if label and name is contained in the hashset, "name" member will be set (and throw, if already existing)
			if (hashtable.ContainsKey("label"))
			{
				hashtable["name"] = hashtable["label"];
                hashtable.Remove("label");
			}
            // also, we really care that we only have "name" and "expression" keys, otherwise we throw an error...
            foreach (var key in hashtable.Keys)
            {
                var keyStr = key.ToString();
                if (keyStr.Equals("name", StringComparison.InvariantCultureIgnoreCase))
                {
                    propName = hashtable[key].ToString();
                }
                else if (keyStr.Equals("expression", StringComparison.InvariantCultureIgnoreCase))
                {
                    // should be a script block, we will check this later
                    property = PSObject.Unwrap(hashtable[key]);
                }
                else
                {
                    throw new NotSupportedException(String.Format("Invalid key '{0}'", keyStr));
                }
            }
		}
		// now it must be a scriptblock that can be evaluated (either from the hastable or the property itself)
		var scriptBlock = property as ScriptBlock;
		if (scriptBlock == null)
		{
			var msg = String.Format("The property '{0}' couldn't be converted to a string, Hashtable, or a ScriptBlock",
			                        property.GetType().Name);
			throw new PSInvalidOperationException(msg);
		}
		if (propName == null)
		{
			propName = scriptBlock.ToString();
		}
        var pipeline = executionContext.CurrentRunspace.CreateNestedPipeline();
		pipeline.Commands.Add(new Command(scriptBlock.Ast as ScriptBlockAst, true));
		// TODO: hack? we need to set the $_ variable
        var oldVar = executionContext.GetVariable("_");
        executionContext.SetVariable("_", psobj);
		Collection<PSObject> results = null;
		try
		{
			results = pipeline.Invoke();
		}
		finally
		{
            if (oldVar != null)
            {
                executionContext.SessionState.PSVariable.Set(oldVar);
            }
            else
            {
                executionContext.SessionState.PSVariable.Set("_", null);
            }
		}
		PSObject value;
		if (results.Count == 0)
		{
			value = PSObject.AsPSObject(null);
		}
		else if (results.Count == 1)
		{
			value = results[0];
		}
		else
		{
			value = PSObject.AsPSObject(new List<PSObject>(results).ToArray());
		}
		return new PSNoteProperty(propName, value);
	}