public IEnumerable <dynamic> Execute(ServerConfig server, string commandOrScript,
                                             Action <PowerShellModulesToLoad> modulesToLoad = null, IEnumerable <CommandParameter> parameters = null,
                                             bool logOutput = true)
        {
            var folders = new RemoteScriptFolders(server);
            var modules = new PowerShellModulesToLoad();

            if (modulesToLoad != null)
            {
                modulesToLoad(modules);
            }

            var remoteCredential = new PSCredential(server.DeploymentUser.UserName,
                                                    GetPasswordAsSecString(server.DeploymentUser.Password));
            var connectionInfo = new WSManConnectionInfo(server.PowerShell.SSL, server.Name, ResolvePort(server), "/wsman",
                                                         SHELL_URI,
                                                         remoteCredential);

            if (UseCredSSP)
            {
                using (new CredSSPHandler(connectionInfo, server))
                {
                    return(ExecuteCommand(commandOrScript, connectionInfo, modules, folders, parameters, logOutput));
                }
            }

            return(ExecuteCommand(commandOrScript, connectionInfo, modules, folders, parameters, logOutput));
        }
 private void ConfigureConDepNodeModule(Pipeline pipeline, RemoteScriptFolders folders,
                                        PowerShellModulesToLoad modules)
 {
     if (modules.LoadConDepNodeModule)
     {
         var conDepNodeModule = string.Format(@"Import-Module {0}", folders.NodeScriptFolder);
         Logger.Verbose(conDepNodeModule);
         pipeline.Commands.AddScript(conDepNodeModule);
     }
 }
 private void ConfigureConDepDotNetLibrary(Pipeline pipeline, RemoteScriptFolders folders,
                                           PowerShellModulesToLoad modules)
 {
     if (modules.LoadConDepDotNetLibrary)
     {
         var netLibraryCmd =
             $@"Add-Type -Path ""{Path.Combine(folders.PSTempFolder, "ConDep.Dsl.Remote.Helpers.dll")}""";
         Logger.Verbose(netLibraryCmd);
         pipeline.Commands.AddScript(netLibraryCmd);
     }
 }
        internal IEnumerable <dynamic> ExecuteCommand(string commandOrScript, WSManConnectionInfo connectionInfo,
                                                      PowerShellModulesToLoad modules, RemoteScriptFolders folders, IEnumerable <CommandParameter> parameters = null,
                                                      bool logOutput = true)
        {
            var host = new ConDepPSHost();

            using (var runspace = RunspaceFactory.CreateRunspace(host, connectionInfo))
            {
                runspace.Open();

                var ps = PowerShell.Create();
                ps.Runspace = runspace;

                using (var pipeline =
                           ps.Runspace.CreatePipeline(
                               "set-executionpolicy remotesigned -force; $VerbosePreference = 'continue'; $DebugPreference = 'continue'"))
                {
                    ConfigureConDepModule(pipeline, folders, modules);
                    ConfigureConDepNodeModule(pipeline, folders, modules);
                    ConfigureConDepDotNetLibrary(pipeline, folders, modules);

                    ConfigureCommand(commandOrScript, parameters, pipeline);

                    dynamic result = pipeline.Invoke();

                    if (pipeline.Error.Count > 0)
                    {
                        var errorCollection = new PowerShellErrors();
                        foreach (var exception in pipeline.Error.NonBlockingRead().OfType <ErrorRecord>())
                        {
                            errorCollection.Add(exception.Exception);
                        }
                        throw errorCollection;
                    }

                    if (logOutput && result.Count > 0)
                    {
                        Logger.WithLogSection("Script output:", () =>
                        {
                            foreach (var psObject in result)
                            {
                                if (psObject.ConDepResult == null)
                                {
                                    Logger.Info(psObject.ToString());
                                }
                            }
                        });
                    }

                    return(result);
                }
            }
        }
        public IEnumerable <dynamic> ExecuteLocal(ServerConfig localServer, string commandOrScript, Action <PowerShellModulesToLoad> modulesToLoad = null, IEnumerable <CommandParameter> parameters = null, bool logOutput = true)
        {
            var connectionInfo = new WSManConnectionInfo();
            var modules        = new PowerShellModulesToLoad();

            if (modulesToLoad != null)
            {
                modulesToLoad(modules);
            }

            var folders = new RemoteScriptFolders(localServer);

            return(ExecuteCommand(commandOrScript, connectionInfo, modules, folders, parameters, logOutput));
        }