Exemplo n.º 1
0
        public Collection <object> Execute(params string[] commands)
        {
            if (LastRunspace != null)
            {
                LastRunspace.Close();
            }
            LastRunspace = RunspaceFactory.CreateRunspace();
            LastRunspace.Open();
            LoadCmdletBinary();

            return(ExecuteInExistingRunspace(commands));
        }
Exemplo n.º 2
0
        public Collection <object> ExecuteInExistingRunspace(params string[] commands)
        {
            Collection <PSObject> results = null;

            LastResults = new Collection <object>();
            LastErrors  = new Collection <ErrorRecord>();

            using (var pipeline = LastRunspace.CreatePipeline())
            {
                var script = JoinCommands(_preExecutionCmds) +
                             JoinCommands(commands) +
                             JoinCommands(_postExecutionCmds);
                pipeline.Commands.AddScript(script, false);
                results    = pipeline.Invoke();
                LastErrors = new Collection <ErrorRecord>(
                    (from errObj in pipeline.Error.NonBlockingRead()
                     where errObj is PSObject
                     select((PSObject)errObj).BaseObject as ErrorRecord
                    ).ToList()
                    );
            }

            foreach (var curPSObject in results)
            {
                if (curPSObject == null)
                {
                    LastResults.Add(null);
                }
                else
                {
                    LastResults.Add(curPSObject.BaseObject);
                }
            }

            if (LastErrors.Count > 0)
            {
                throw new ShellExecutionHasErrorsException(LastErrors);
            }
            return(LastResults);
        }
Exemplo n.º 3
0
        private void LoadCmdletBinary()
        {
            bool   isWindows = Environment.OSVersion.Platform == PlatformID.Win32NT;
            string path      = new Uri(typeof(CmisCmdlets.CmisCommandBase).Assembly.CodeBase).LocalPath;

            // with real Powershell we use Import-Module to avoid installation of the SnapIn
            string cmd = isWindows ? "Import-Module '{0}'" : "Add-PSSnapIn '{0}'";

            using (var pipeline = LastRunspace.CreatePipeline())
            {
                pipeline.Commands.AddScript(String.Format(cmd, path));
                try
                {
                    pipeline.Invoke();
                }
                catch (MethodInvocationException e)
                {
                    throw new RuntimeException(String.Format(
                                                   "Failed to import module '{0}'. Didn't you build it?", path), e);
                }
            }
        }