Пример #1
0
        private ScriptBlock GetScriptBlockFromFile(string filePath, PSCmdlet psCmdlet)
        {
            if (WildcardPattern.ContainsWildcardCharacters(filePath))
            {
                throw new ArgumentException(Properties.Resources.ResourceManager.GetString("FilePathWildcards"));
            }

            if (!filePath.EndsWith(".ps1", StringComparison.OrdinalIgnoreCase))
            {
                throw new ArgumentException(Properties.Resources.ResourceManager.GetString("FilePathExt"));
            }

            ProviderInfo provider     = null;
            string       resolvedPath = psCmdlet.GetResolvedProviderPathFromPSPath(filePath, out provider).FirstOrDefault();

            if (!string.IsNullOrEmpty(resolvedPath))
            {
                Token[]        tokens;
                ParseError[]   errors;
                ScriptBlockAst scriptBlockAst = Parser.ParseFile(resolvedPath, out tokens, out errors);
                if (scriptBlockAst != null && errors.Length == 0)
                {
                    return(scriptBlockAst.GetScriptBlock());
                }

                foreach (var error in errors)
                {
                    this.Error.Add(
                        new ErrorRecord(
                            new ParseException(error.Message), "ThreadJobError", ErrorCategory.InvalidData, this));
                }
            }

            return(null);
        }
Пример #2
0
        public static List <string> ResolvePaths(this PSCmdlet cmdlet, bool shouldExpandWildcards, params string[] paths)
        {
            var resolvedPaths = new List <string>();

            foreach (var path in paths)
            {
                // this contains the paths to process for this iteration of the
                // loop to resolve and optionally expand wildcards.
                var filePaths = new List <string>();
                try
                {
                    // This will hold information about the provider containing
                    // the items that this path string might resolve to.
                    ProviderInfo provider;
                    if (shouldExpandWildcards)
                    {
                        // Turn *.txt into foo.txt,foo2.txt etc.
                        // if path is just "foo.txt," it will return unchanged.
                        filePaths.AddRange(cmdlet.GetResolvedProviderPathFromPSPath(path, out provider));
                    }
                    else
                    {
                        // no wildcards, so don't try to expand any * or ? symbols.
                        filePaths.Add(cmdlet.SessionState.Path.GetUnresolvedProviderPathFromPSPath(
                                          path, out provider, out _));
                    }

                    // ensure that this path (or set of paths after wildcard expansion)
                    // is on the filesystem. A wildcard can never expand to span multiple
                    // providers.
                    if (provider.ImplementingType != typeof(FileSystemProvider))
                    {
                        // create a .NET exception wrapping our error text
                        var ex = new ArgumentException(path +
                                                       " does not resolve to a path on the FileSystem provider.");
                        // wrap this in a powershell errorrecord
                        var error = new ErrorRecord(ex, "InvalidProvider",
                                                    ErrorCategory.InvalidArgument, path);
                        // write a non-terminating error to pipeline
                        cmdlet.WriteError(error);

                        // no, so skip to next path in _paths.
                        continue;
                    }
                }
                catch (Exception exception)
                {
                    // wrap this in a powershell errorrecord
                    var error = new ErrorRecord(exception, "InvalidPath",
                                                ErrorCategory.InvalidArgument, path);
                    // write a non-terminating error to pipeline
                    cmdlet.WriteError(error);
                    continue;
                }

                resolvedPaths.AddRange(filePaths);
            }

            return(resolvedPaths);
        }
Пример #3
0
        internal static IEnumerable <string> GetFileSystemPaths(this PSCmdlet cmdlet, string path, string literalPath)
        {
            ProviderInfo provider;

            if (!string.IsNullOrEmpty(path))
            {
                Collection <string> providerPaths = cmdlet.GetResolvedProviderPathFromPSPath(path, out provider);

                if (provider.ImplementingType == typeof(FileSystemProvider))
                {
                    return(providerPaths);
                }
            }

            if (!string.IsNullOrEmpty(literalPath))
            {
                PSDriveInfo drive;

                string providerPath = cmdlet.SessionState.Path.GetUnresolvedProviderPathFromPSPath(literalPath,
                                                                                                   out provider, out drive);
                if (provider.ImplementingType == typeof(FileSystemProvider))
                {
                    return new[] { providerPath }
                }
                ;
            }

            return(new string[0]);
        }
Пример #4
0
        internal static IEnumerable <string> GetFileSystemPaths(this PSCmdlet cmdlet, string?path, string?literalPath)
        {
            ProviderInfo provider;

            if (!string.IsNullOrEmpty(path))
            {
                var providerPaths = cmdlet.GetResolvedProviderPathFromPSPath(path, out provider);
                if (provider.ImplementingType == typeof(FileSystemProvider))
                {
                    return(providerPaths);
                }
            }

            // ReSharper disable once InvertIf
            if (!string.IsNullOrEmpty(literalPath))
            {
                var providerPath =
                    cmdlet.SessionState.Path.GetUnresolvedProviderPathFromPSPath(literalPath, out provider, out _);
                if (provider.ImplementingType == typeof(FileSystemProvider))
                {
                    return new[] { providerPath }
                }
                ;
            }

            return(Array.Empty <string>());
        }