Я пытаюсь написать программу менеджера сервисов Neo4j на C #, которая может запускать и останавливать локальные графовые базы данных, используя текущую версию Neo4j, 3.3.5. Программа должна воспроизводить функциональность прекращенной Neo4j-ce.exe
программы. Программа будет вызывать те же команды PowerShell, что и в файле Neo4j.bat
. Это первый сценарий 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.
}
}
}