示例#1
0
        public Command CreateCommand(IPowerShellCommandParameterProvider commandParameterProvider)
        {
            if (_expressionSpecified && _fileSpecifed)
            {
                throw new ArgumentException(Resources.ExpressionAndFileParametersCannotBeUsedSimultaneously);
            }

            if (_expressionSpecified)
            {
                return CreateExpressionCommand();
            }

            if (_fileSpecifed)
            {
                return CreateFileCommand(commandParameterProvider);
            }

            throw new ArgumentException(Resources.ExpressionOrFileParameterMustBeSpecified);
        }
示例#2
0
        private Command CreateFileCommandWithAutoParameters(
            string filePath,
            IPowerShellCommandParameterProvider commandParameterProvider)
        {
            var command = new Command(filePath, isScript: false);
            foreach (var kvp in GetCommandParameterValuesThatMatchAutoParameters(filePath, commandParameterProvider))
            {
                command.Parameters.Add(kvp.Key, kvp.Value);
            }

            return command;
        }
示例#3
0
        private IEnumerable<KeyValuePair<string, string>> GetCommandParameterValuesThatMatchAutoParameters(
            string commandName,
            IPowerShellCommandParameterProvider commandParameterProvider)
        {
            var parameterNames = commandParameterProvider.GetDefaultParameterSetParameterNames(commandName);
            var autoParametersLookup = AutoParameters
                .Select(x => new
                {
                    ParameterName = x.ItemSpec,
                    ParameterValue = x.GetMetadata("Value")
                })
                .Where(x => !string.IsNullOrEmpty(x.ParameterValue))
                .ToLookup(x => x.ParameterName, x => x.ParameterValue, StringComparer.OrdinalIgnoreCase)
                .ToDictionary(x => x.Key, x => x.Last());

            foreach (var parameterName in parameterNames)
            {
                string parameterValue;
                if (autoParametersLookup.TryGetValue(parameterName, out parameterValue))
                {
                    yield return new KeyValuePair<string, string>(parameterName, parameterValue);
                }
            }
        }
示例#4
0
        private Command CreateFileCommand(IPowerShellCommandParameterProvider commandParameterProvider)
        {
            if (_argumentsSpecified && _autoParametersSpecified)
            {
                throw new ArgumentException(Resources.ArgumentsAndAutoParametersParametersCannotBeUsedSimultaneously);
            }

            if (!_file.EndsWith(".ps1", StringComparison.OrdinalIgnoreCase))
            {
                var error = string.Format(CultureInfo.CurrentCulture,
                    Resources.PowerShellScriptFileMustBeSpecifiedFormat,
                    _file);

                throw new ArgumentException(error);
            }

            var filePath = ConvertPowerShellFileParameterValueToFullPath.Execute(_file, _fileSystem);

            return _autoParametersSpecified
                ? CreateFileCommandWithAutoParameters(filePath, commandParameterProvider)
                : CreateFileCommand(filePath);
        }