如何为Neo4j服务管理器编程?

时间:2018-05-15 15:22:59

标签: c# powershell neo4j

我正在尝试用C#编写一个Neo4j服务管理器程序,它可以使用当前的Neo4j版本3.3.5启动和停止本地图形数据库。该程序应该复制已停止的Neo4j-ce.exe程序的功能。该程序将调用Neo4j.bat文件中的相同PowerShell命令。这是我尝试编写的第一个PowerShell脚本。

到目前为止,在启动​​时,程序会发出许多以“无法找到或打开PDB文件”结尾的异常错误。当我运行PowerShell实例代码时,会抛出安全异常......

Exception thrown: 'System.DllNotFoundException' in System.Management.Automation.dll
Exception thrown: 'System.Security.SecurityException' in mscorlib.dll

......然而PowerShellInstance.Streams.Error.Count为零。

Neo4j.bat包含对PowerShell的调用:

PowerShell -NoProfile -NonInteractive -NoLogo -ExecutionPolicy Bypass -Command "try { Unblock-File -Path '%~dp0Neo4j-Management\*.*' -ErrorAction 'SilentlyContinue' } catch {};Import-Module '%~dp0Neo4j-Management.psd1'; Exit (Invoke-Neo4j %*)"

Neo4jServiceManager.exe包含此按钮事件:

private void btnNeo4jStart_Click(object sender, EventArgs e)
{
    // Create runspace to be used by PowerShell instance.
    Runspace myRunspace = RunspaceFactory.CreateRunspace();
    myRunspace.Open();

    // Start PowerShell instance.
    using (PowerShell PowerShellInstance = PowerShell.Create())
    {
        // Add runspace to PowerShell instance.
        PowerShellInstance.Runspace = myRunspace;

        // Add command to pipeline.
        // Import module: Neo4j-Management.psd1 and execute Invoke-Neo4j cmdlet.
        PowerShellInstance.AddScript(@"C:\; cd C:\neo4j-community-3.3.5; SETLOCAL; try { Unblock-File -Path 'C:\neo4j-community-3.3.5\bin\Neo4j-Management\*.*' -ErrorAction 'SilentlyContinue' } catch {}; Exit ('C:\neo4j-community-3.3.5\bin\Neo4j-Management\Invoke-Neo4j.psd1 install-service')");

        // Invoke execution on the pipeline (and collect output).
        Collection<PSObject> PSOutput = PowerShellInstance.Invoke();

        // Loop through each output object item.
        foreach (PSObject outputItem in PSOutput)
        {
            // if null object was dumped to the pipeline during the script then a null
            // object may be present here. check for null to prevent potential NRE.
            if (outputItem != null)
            {
                //TODO: do something with the output item 
                // outputItem.BaseOBject
            }
        }

        // Check the other output streams (for example, the error stream).
        // If PowerShellInstance.Streams.Error.Count = 1, then
        // PowerShellInstance.Streams.Error[0].CategoryInfo returns information.
        if (PowerShellInstance.Streams.Error.Count > 0)
        {
            // error records were written to the error stream.
            // do something with the items found.
        }
    }
}

0 个答案:

没有答案