private void ConvertCommand(CommandAst commandAst, bool isTrustedInput) { // First need command name. var commandName = GetCommandName(commandAst.CommandElements[0], isTrustedInput); var command = new Command(commandName, isScript: false, useLocalScope: _createLocalScope); // Handle redirections, if any (there can really be just 0 or 1). if (commandAst.Redirections.Count > 0) { Diagnostics.Assert(commandAst.Redirections.Count == 1, "only 1 kind of redirection is supported"); Diagnostics.Assert(commandAst.Redirections[0] is MergingRedirectionAst, "unexpected redirection type"); PipelineResultTypes toType = PipelineResultTypes.Output; PipelineResultTypes fromType; switch (commandAst.Redirections[0].FromStream) { case RedirectionStream.Error: fromType = PipelineResultTypes.Error; break; case RedirectionStream.Warning: fromType = PipelineResultTypes.Warning; break; case RedirectionStream.Verbose: fromType = PipelineResultTypes.Verbose; break; case RedirectionStream.Debug: fromType = PipelineResultTypes.Debug; break; case RedirectionStream.Information: fromType = PipelineResultTypes.Information; break; case RedirectionStream.All: fromType = PipelineResultTypes.All; break; default: // Default to Error->Output to be compatible with V2. fromType = PipelineResultTypes.Error; break; } command.MergeMyResults(fromType, toType); } _powershell.AddCommand(command); // Now the parameters and arguments. foreach (var ast in commandAst.CommandElements.Skip(1)) { var exprAst = ast as ExpressionAst; if (exprAst != null) { VariableExpressionAst variableAst = null; var usingExprAst = ast as UsingExpressionAst; if (usingExprAst != null) { string usingAstKey = PsUtils.GetUsingExpressionKey(usingExprAst); object usingValue = _usingValueMap[usingAstKey]; variableAst = usingExprAst.SubExpression as VariableExpressionAst; if (variableAst != null && variableAst.Splatted) { // Support the splatting of a dictionary var parameters = usingValue as System.Collections.IDictionary; if (parameters != null) { _powershell.AddParameters(parameters); } else { // Support the splatting of an array var arguments = usingValue as System.Collections.IEnumerable; if (arguments != null) { foreach (object argument in arguments) { _powershell.AddArgument(argument); } } else { // Splat the object directly. _powershell.AddArgument(usingValue); } } } else { _powershell.AddArgument(usingValue); } continue; } variableAst = ast as VariableExpressionAst; if (variableAst != null && variableAst.Splatted) { GetSplattedVariable(variableAst); } else { var constantExprAst = ast as ConstantExpressionAst; object argument; if (constantExprAst != null && LanguagePrimitives.IsNumeric(LanguagePrimitives.GetTypeCode(constantExprAst.StaticType))) { var commandArgumentText = constantExprAst.Extent.Text; argument = constantExprAst.Value; if (!commandArgumentText.Equals(constantExprAst.Value.ToString(), StringComparison.Ordinal)) { // The wrapped number will actually return a PSObject which could end holding a reference to // a typetable, making the object runspace specific. We should find a better way to avoid // any possibility of sharing problems, though this is unlikely to cause problems. argument = ParserOps.WrappedNumber(argument, commandArgumentText); } } else { if (!isTrustedInput) { try { argument = GetSafeValueVisitor.GetSafeValue(exprAst, _context, GetSafeValueVisitor.SafeValueContext.GetPowerShell); } catch (System.Exception) { throw new ScriptBlockToPowerShellNotSupportedException( "CantConvertWithDynamicExpression", null, AutomationExceptions.CantConvertWithDynamicExpression, exprAst.Extent.Text); } } else { argument = GetExpressionValue(exprAst, isTrustedInput); } } _powershell.AddArgument(argument); } } else { AddParameter((CommandParameterAst)ast, isTrustedInput); } } }
private void ConvertCommand(CommandAst commandAst) { Command command = new Command(this.GetCommandName(commandAst.CommandElements[0]), false, this._createLocalScope); if (commandAst.Redirections.Count > 0) { PipelineResultTypes all; PipelineResultTypes output = PipelineResultTypes.Output; switch (commandAst.Redirections[0].FromStream) { case RedirectionStream.All: all = PipelineResultTypes.All; break; case RedirectionStream.Error: all = PipelineResultTypes.Error; break; case RedirectionStream.Warning: all = PipelineResultTypes.Warning; break; case RedirectionStream.Verbose: all = PipelineResultTypes.Verbose; break; case RedirectionStream.Debug: all = PipelineResultTypes.Debug; break; default: all = PipelineResultTypes.Error; break; } command.MergeMyResults(all, output); } this._powershell.AddCommand(command); foreach (CommandElementAst ast in commandAst.CommandElements.Skip <CommandElementAst>(1)) { ExpressionAst exprAst = ast as ExpressionAst; if (exprAst != null) { VariableExpressionAst variableAst = null; UsingExpressionAst ast4 = ast as UsingExpressionAst; if (ast4 != null) { variableAst = ast4.SubExpression as VariableExpressionAst; if ((variableAst != null) && variableAst.Splatted) { IDictionary parameters = this._usingValues[ast4.RuntimeUsingIndex] as IDictionary; if (parameters != null) { this._powershell.AddParameters(parameters); } else { IEnumerable enumerable = this._usingValues[ast4.RuntimeUsingIndex] as IEnumerable; if (enumerable != null) { foreach (object obj2 in enumerable) { this._powershell.AddArgument(obj2); } } else { this._powershell.AddArgument(this._usingValues[ast4.RuntimeUsingIndex]); } } } else { this._powershell.AddArgument(this._usingValues[ast4.RuntimeUsingIndex]); } } else { variableAst = ast as VariableExpressionAst; if ((variableAst != null) && variableAst.Splatted) { this.GetSplattedVariable(variableAst); } else { object expressionValue; ConstantExpressionAst ast5 = ast as ConstantExpressionAst; if ((ast5 != null) && LanguagePrimitives.IsNumeric(LanguagePrimitives.GetTypeCode(ast5.StaticType))) { string text = ast5.Extent.Text; expressionValue = ast5.Value; if (!text.Equals(ast5.Value.ToString(), StringComparison.Ordinal)) { expressionValue = ParserOps.WrappedNumber(expressionValue, text); } } else { expressionValue = this.GetExpressionValue(exprAst); } this._powershell.AddArgument(expressionValue); } } } else { this.AddParameter((CommandParameterAst)ast); } } }