public static Collection <PSObject> InvokeScript(this HttpRequestMessage request, PowerShell powershell, string script, params object[] arguments) { powershell.AddScript(script); powershell.AddArguments(arguments); Collection <PSObject> psData = null; try { psData = powershell.Invoke(); } catch (RemoteException ex) { request.ThrowHttpException(HttpStatusCode.InternalServerError, ex); } catch (Exception) { // connections was aborted or has invalid state request.CloseSharedConnection(); throw; } return(psData); }