多次打开运行空间
Opening runspaces multiple times
我想通过 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;
}
}
我第一次能够执行此操作。但是当我第二次尝试执行这个函数时它给了我错误,运行空间是 closed.actually 我最后确实关闭了它但是为什么在调用函数时它没有重新打开。
您关闭了它但没有处理它:
using (var powershell = PowerShell.Create())
using (Runspace runspace = RunspaceFactory.CreateRunspace(connection))
{
runspace.Open();
powershell.Runspace = runspace();
powershell.AddScript(Command);
results = powershell.Invoke();
runspace.Close();
}
我想通过 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;
}
}
我第一次能够执行此操作。但是当我第二次尝试执行这个函数时它给了我错误,运行空间是 closed.actually 我最后确实关闭了它但是为什么在调用函数时它没有重新打开。
您关闭了它但没有处理它:
using (var powershell = PowerShell.Create())
using (Runspace runspace = RunspaceFactory.CreateRunspace(connection))
{
runspace.Open();
powershell.Runspace = runspace();
powershell.AddScript(Command);
results = powershell.Invoke();
runspace.Close();
}