Example #1
0
        /// <summary>
        /// </summary>
        protected override void ProcessRecord()
        {
            ProviderInfo        provider = null;
            Collection <string> filePaths;

            try
            {
                if (this.Context.EngineSessionState.IsProviderLoaded(Context.ProviderNames.FileSystem))
                {
                    filePaths = SessionState.Path.GetResolvedProviderPathFromPSPath(_path, out provider);
                }
                else
                {
                    filePaths = new Collection <string>();
                    filePaths.Add(_path);
                }
            }
            catch (ItemNotFoundException)
            {
                string message            = StringUtil.Format(RemotingErrorIdStrings.PSSessionConfigurationFileNotFound, _path);
                FileNotFoundException fnf = new FileNotFoundException(message);
                ErrorRecord           er  = new ErrorRecord(fnf, "PSSessionConfigurationFileNotFound",
                                                            ErrorCategory.ResourceUnavailable, _path);
                WriteError(er);
                return;
            }

            // Make sure that the path is in the file system - that's all we can handle currently...
            if (!provider.NameEquals(this.Context.ProviderNames.FileSystem))
            {
                // "The current provider ({0}) cannot open a file"
                throw InterpreterError.NewInterpreterException(_path, typeof(RuntimeException),
                                                               null, "FileOpenError", ParserStrings.FileOpenError, provider.FullName);
            }

            // Make sure at least one file was found...
            if (filePaths == null || filePaths.Count < 1)
            {
                string message            = StringUtil.Format(RemotingErrorIdStrings.PSSessionConfigurationFileNotFound, _path);
                FileNotFoundException fnf = new FileNotFoundException(message);
                ErrorRecord           er  = new ErrorRecord(fnf, "PSSessionConfigurationFileNotFound",
                                                            ErrorCategory.ResourceUnavailable, _path);
                WriteError(er);
                return;
            }

            if (filePaths.Count > 1)
            {
                // "The path resolved to more than one file; can only process one file at a time."
                throw InterpreterError.NewInterpreterException(filePaths, typeof(RuntimeException),
                                                               null, "AmbiguousPath", ParserStrings.AmbiguousPath);
            }

            string             filePath   = filePaths[0];
            ExternalScriptInfo scriptInfo = null;
            string             ext        = System.IO.Path.GetExtension(filePath);

            if (ext.Equals(StringLiterals.PowerShellDISCFileExtension, StringComparison.OrdinalIgnoreCase))
            {
                // Create a script info for loading the file...
                string scriptName;
                scriptInfo = DISCUtils.GetScriptInfoForFile(this.Context, filePath, out scriptName);

                Hashtable configTable = null;

                try
                {
                    configTable = DISCUtils.LoadConfigFile(this.Context, scriptInfo);
                }
                catch (RuntimeException e)
                {
                    WriteVerbose(StringUtil.Format(RemotingErrorIdStrings.DISCErrorParsingConfigFile, filePath, e.Message));
                    WriteObject(false);
                    return;
                }

                if (configTable == null)
                {
                    WriteObject(false);
                    return;
                }

                DISCUtils.ExecutionPolicyType = typeof(ExecutionPolicy);
                WriteObject(DISCUtils.VerifyConfigTable(configTable, this, filePath));
            }
            else
            {
                string message = StringUtil.Format(RemotingErrorIdStrings.InvalidPSSessionConfigurationFilePath, filePath);
                InvalidOperationException ioe = new InvalidOperationException(message);
                ErrorRecord er = new ErrorRecord(ioe, "InvalidPSSessionConfigurationFilePath",
                                                 ErrorCategory.InvalidArgument, _path);
                ThrowTerminatingError(er);
            }
        }
Example #2
0
        protected override void ProcessRecord()
        {
            ProviderInfo        provider = null;
            Collection <string> resolvedProviderPathFromPSPath;

            try
            {
                if (base.Context.EngineSessionState.IsProviderLoaded(base.Context.ProviderNames.FileSystem))
                {
                    resolvedProviderPathFromPSPath = base.SessionState.Path.GetResolvedProviderPathFromPSPath(this.path, out provider);
                }
                else
                {
                    resolvedProviderPathFromPSPath = new Collection <string> {
                        this.path
                    };
                }
            }
            catch (ItemNotFoundException)
            {
                FileNotFoundException exception   = new FileNotFoundException(StringUtil.Format(RemotingErrorIdStrings.PSSessionConfigurationFileNotFound, this.path));
                ErrorRecord           errorRecord = new ErrorRecord(exception, "PSSessionConfigurationFileNotFound", ErrorCategory.ResourceUnavailable, this.path);
                base.WriteError(errorRecord);
                return;
            }
            if (!provider.NameEquals(base.Context.ProviderNames.FileSystem))
            {
                throw InterpreterError.NewInterpreterException(this.path, typeof(RuntimeException), null, "FileOpenError", ParserStrings.FileOpenError, new object[] { provider.FullName });
            }
            if ((resolvedProviderPathFromPSPath != null) && (resolvedProviderPathFromPSPath.Count >= 1))
            {
                if (resolvedProviderPathFromPSPath.Count > 1)
                {
                    throw InterpreterError.NewInterpreterException(resolvedProviderPathFromPSPath, typeof(RuntimeException), null, "AmbiguousPath", ParserStrings.AmbiguousPath, new object[0]);
                }
                string             path       = resolvedProviderPathFromPSPath[0];
                ExternalScriptInfo scriptInfo = null;
                if (System.IO.Path.GetExtension(path).Equals(".pssc", StringComparison.OrdinalIgnoreCase))
                {
                    string str5;
                    scriptInfo = DISCUtils.GetScriptInfoForFile(base.Context, path, out str5);
                    Hashtable table = null;
                    try
                    {
                        table = DISCUtils.LoadConfigFile(base.Context, scriptInfo);
                    }
                    catch (RuntimeException exception3)
                    {
                        base.WriteVerbose(StringUtil.Format(RemotingErrorIdStrings.DISCErrorParsingConfigFile, path, exception3.Message));
                        base.WriteObject(false);
                        return;
                    }
                    if (table == null)
                    {
                        base.WriteObject(false);
                    }
                    else
                    {
                        DISCUtils.ExecutionPolicyType = typeof(ExecutionPolicy);
                        base.WriteObject(DISCUtils.VerifyConfigTable(table, this, path));
                    }
                }
                else
                {
                    InvalidOperationException exception4 = new InvalidOperationException(StringUtil.Format(RemotingErrorIdStrings.InvalidPSSessionConfigurationFilePath, path));
                    ErrorRecord record3 = new ErrorRecord(exception4, "InvalidPSSessionConfigurationFilePath", ErrorCategory.InvalidArgument, this.path);
                    base.ThrowTerminatingError(record3);
                }
            }
            else
            {
                FileNotFoundException exception2 = new FileNotFoundException(StringUtil.Format(RemotingErrorIdStrings.PSSessionConfigurationFileNotFound, this.path));
                ErrorRecord           record2    = new ErrorRecord(exception2, "PSSessionConfigurationFileNotFound", ErrorCategory.ResourceUnavailable, this.path);
                base.WriteError(record2);
            }
        }