如何在远程PowerShell脚本完成后保持进程存活

本文关键字:进程 脚本 PowerShell | 更新日期: 2023-09-27 18:09:02

我试图通过PowerShell scriptblock远程启动Zookeeper和Solr。然后我看到这个进程是在远程机器上创建的(通过检查Zookeeper的2181端口)。在脚本完成时,它被终止。我如何在游戏完成后保持它的活力?

下面的代码在脚本完成时停止远程进程。script.ps1做了很多事情,包括启动Zookeeper和Solr asJob。

int iRemotePort = 5985;
string strShellURI = @"http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
string strAppName = @"/wsman";
WSManConnectionInfo ci = new WSManConnectionInfo(
    false,
    machineName,
    iRemotePort,
    strAppName,
    strShellURI,
    new PSCredential(userName, secure));
Runspace runspace = RunspaceFactory.CreateRunspace(ci);
runspace.Open();
using (PowerShell ps = PowerShell.Create())
{
    session.Log(@"c:'temp'script.ps1 -serverID " + counter + " -copySolrConfig $" + status + " -currentHost '"" + machineName + "'" -IP_PORT_List '"" + String.Join(", ", machineWithPort) + "'"");
    ps.Runspace = runspace;
    ps.AddScript(@"c:'temp'script.ps1 -serverID " + counter + " -copySolrConfig $" + status + " -currentHost '"" + machineName + "'" -IP_PORT_List '"" + String.Join(", ", machineWithPort) + "'"");
    var results = ps.Invoke();
    foreach (var result in results)
    {
        session.Log(result.ToString());
    }
}
runspace.Close();

如何在远程PowerShell脚本完成后保持进程存活

因此,经过长时间的尝试,我得出结论,使用power-shell脚本并不是保持zookeeper和solr有效运行的合适方法。因此将其作为windows服务移动,并通过WiX将其安装在远程机器上。