0
我想在C#的遠程計算機上執行powershell命令。我已經實現了相同的使用多次打開運行空間
public Collection<PSObject> RunScript(String Command)
{
Collection<PSObject> results = null;
using (var powershell = PowerShell.Create())
{
Runspace runspace = RunspaceFactory.CreateRunspace(connection);
runspace.Open();
powershell.Runspace = runspace();
powershell.AddScript(Command);
results = powershell.Invoke();
runspace.Close();
}
return results;
}
}
我能夠執行此第一次。但是當我第二次嘗試執行這個函數時,它給了我一個錯誤,runspace被關閉了。實際上我最後關閉了它,但是爲什麼當函數被調用時不能重新打開。
抱歉,我不是上面的帖子很清楚。實際上我只想創建一次運行空間。那麼我想打開n多次關閉相同的運行空間。通過處置我將釋放當地的資源。我不想那樣做。 –
@ kaustubh93在這種情況下,我認爲你需要在你的函數範圍之外聲明並實例化'runspace'。 –