获取IISAPPOOL |选择对象状态在.NET中为空

获取IISAPPOOL |选择对象状态在.NET中为空,.net,powershell,.net,Powershell,我想通过.net从IIS获取应用程序池的状态 我正在使用以下powershell命令:Get iisappool |选择对象状态 [Route("api/getstates")] [HttpGet] public HttpResponseMessage GetStates() { string commandGetState = "Get-IISAppPool | Select-Object State";

我想通过.net从IIS获取应用程序池的状态

我正在使用以下powershell命令:
Get iisappool |选择对象状态

[Route("api/getstates")]
    [HttpGet]
    public HttpResponseMessage GetStates()
    {
        string commandGetState = "Get-IISAppPool | Select-Object State";
        HttpResponseMessage request;
        try
        {
            var stat = ExecuteCommandDefault(commandGetState);
            request = Request.CreateResponse(HttpStatusCode.OK, stat);
        }
        catch (Exception exception)
        {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, exception.Message);
        }
        return request;
    }

 private string ExecuteCommandDefault(string command)
    {
        Runspace runspace = RunspaceFactory.CreateRunspace();
        runspace.Open();
        Pipeline pipeline = runspace.CreatePipeline();
        pipeline.Commands.AddScript(command);
        pipeline.Commands.Add("Out-String");
        Collection<PSObject> result = pipeline.Invoke();
        runspace.Close();
        StringBuilder stringBuilder = new StringBuilder();
        foreach (PSObject psObject in result)
        {
            stringBuilder.AppendLine(psObject.ToString());
        }
        return stringBuilder.ToString();
    }
我得到的状态后面有一行和5个空的新行

当我使用命令运行以下代码以获取所有名称时:
get iisappool | Select Object Name
i获取5个应用程序池

"\r\nName                   \r\n----                   \r\nClr4IntegratedAppPool  \r\nClr4ClassicAppPool     \r\nClr2IntegratedAppPool  \r\nClr2ClassicAppPool     \r\nUnmanagedClassicAppPool\r\n\r\n\r\n"
所以我认为它实际上是在它们上面循环,但它就是看不到它的价值

当我在powershell中运行这两个命令时,它们都可以正常工作

那么,当我运行命令获取.NET项目中的状态时,有人知道为什么我看不到状态吗

"\r\nName                   \r\n----                   \r\nClr4IntegratedAppPool  \r\nClr4ClassicAppPool     \r\nClr2IntegratedAppPool  \r\nClr2ClassicAppPool     \r\nUnmanagedClassicAppPool\r\n\r\n\r\n"