Calling Powershell Start-Service from C# with a valid command does not execute

Håkan Magnusson 96 Reputation points
2021-05-30T02:17:32.097+00:00

I have a problem that i have been googling on for hours and hours.

I try to run a remote Powershell script to start up a process. Exit and then let it run. It takes hours. The problem is that the process seem to exit directly after beeing started, without errors or any information in logs.

I know it starts, it create the logfile specified (but empty) and i get a response from Start-Process with process id and other information. If i take the script and paste it inside a Powershell command window on the client in question. It runs without any problems. So the script does not seem to be faulty.

Im new to this so bear with me.

            InitialSessionState initial = InitialSessionState.CreateDefault();
            initial.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Unrestricted;
            Runspace runspace = RunspaceFactory.CreateRunspace(initial);
            runspace.Open();

            var ps = PowerShell.Create();
            ps.Runspace = runspace;
            ps.AddCommand("Invoke-Command");
            ps.AddParameter("ComputerName", Host);
            ps.AddParameter("Credential", CreateCredentials());

            ScriptBlock filter = ScriptBlock.Create(command);
            ps.AddParameter("ScriptBlock", filter);
            Collection<PSObject> results = ps.Invoke();
            return results;

My command line that i pass lookes like this:

Start-Process -FilePath C:\Some\Valid\Path\prog.exe -ArgumentList "Long list of arguments specific to exe" -PassThru -RedirectStandardOutput C:\Log\Path\current.log

Does someone have any ideas, what im i missing?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,199 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,355 questions
0 comments No comments
{count} votes

Accepted answer
  1. Håkan Magnusson 96 Reputation points
    2021-06-01T01:38:55.1+00:00

    @MotoX80 Okay, now i have done some testing and research. It turns out that you are right about it not beeing tied to any window per se. However it is tied to the PS Session. And when that sucker ends it kills the process. This can be solved using the -InDisconnectedSession flag. If i set this in the Invoice-Command call, it does not kill the process. Untill 2 hours :( And i need way more time.

    This limit can be reset by using:

    $TimeOut = New-PSSessionOption -IdleTimeoutMSec (New-TimeSpan -Days 7).TotalMilliSeconds
    -InDisconnectedSession -SessionOption $TimeOut

    But i have not been able to set this yet.

                ps.AddCommand("Invoke-Command");  
                ps.AddParameter("ComputerName", Host);  
                ps.AddParameter("ScriptBlock", filter);  
                ps.AddParameter("Credential", CreateCredentials());  
                ps.AddParameter("InDisconnectedSession");  
                ps.AddParameter("SessionName", "CoolSessionName");  
                ps.AddParameter("SessionOption", "$psOption");  
    

    I dont know how to create this $TimeOut variable and tie it to the parameter SessionOption in my script. Since it is a variable that needs to execute before the Invoke-Command.


1 additional answer

Sort by: Most helpful
  1. MotoX80 31,561 Reputation points
    2021-05-30T02:33:38.653+00:00

    My command line that i pass lookes like this:

    Start-Process -FilePath C:\Some\Valid\Path\prog.exe -ArgumentList "Long list of arguments specific to exe" -PassThru -RedirectStandardOutput C:\Log\Path\current.log

    Add "-RedirectStandardError C:\Log\Path\current.err" to capture any errors that it might encounter.