Ejemplo n.º 1
0
        private FileSystemInfo ResolveDestination()
        {
            using (var ps = Runspace.DefaultRunspace.Dynamic())
            {
                DynamicPowershellResult output = ps.GetItem(OutputPath ?? FilePath[0]);
                if (output.Errors.Any())
                {
                    if (output.Errors.Any(e => e.Exception is ItemNotFoundException))
                    {
                        //the destination didn't exist, probably a file
                        var lastSlash = OutputPath.LastIndexOf('\\');
                        var hasASlash = lastSlash >= 0;
                        var probablyDirectoryDestination = hasASlash ? OutputPath.Substring(0, lastSlash) : ".";

                        output = ps.GetItem(probablyDirectoryDestination);
                        if (output.Errors.Any())
                        {
                            var ex = output.Errors.FirstOrDefault(e => e.Exception is ItemNotFoundException);
                            this.WriteErrors(output.Errors.Where(e => e != ex));

                            ThrowTerminatingError(new ErrorRecord(new Exception("{0} does not exist, nor does {1}".format(OutputPath, probablyDirectoryDestination), ex.Exception), "", ErrorCategory.InvalidData, null));
                        }

                        if (output.Count != 1)
                        {
                            ThrowTerminatingError(new ErrorRecord(new Exception("OutputPath may not be a wildcard"), "", ErrorCategory.InvalidData, null));
                        }

                        var dir = output.First() as FileSystemInfo;

                        return
                            (new FileInfo(Path.Combine(dir.FullName,
                                                       hasASlash ? OutputPath.Substring(lastSlash + 1) : OutputPath)));
                    }

                    this.WriteErrorsAndThrowOnLast(output.Errors);
                }

                if (output.Count != 1)
                {
                    ThrowTerminatingError(new ErrorRecord(new Exception("OutputPath may not be a wildcard"), "", ErrorCategory.InvalidData, null));
                }

                return(output.First() as FileSystemInfo);
            }
        }