寻找服务和流程

时间:2018-05-09 15:43:37

标签: powershell

下午好,

我在这里寻求帮助,因为我已经耗尽了所有的资源。看来我的powershell技能对我这项任务没有好处。

我必须完成以下四项任务:

  1. 列出正在运行的进程。
  2. 列出正在运行的服务及其PID。
  3. 对于找到的每个进程,检查是否有服务并列出服务和PID。
  4. 列出没有服务的进程及其PID。
  5. 对于1和2,它非常简单,我已经使用过:

    1. get-wmiobject -class Win32_process | select Name, ProcessID
    2. In addition the server throw an error: java.io.IOException: Write timeout exceeded when trying to flush the data at org.glassfish.grizzly.http.io.OutputBuffer.blockAfterWriteIfNeeded(OutputBuffer.java:1023) at org.glassfish.grizzly.http.io.OutputBuffer.write(OutputBuffer.java:725) at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:336) at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:323) at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:154) at java.nio.channels.Channels$WritableByteChannelImpl.write(Channels.java:458) at com.sun.faces.application.resource.ResourceHandlerImpl.handleResourceRequest(ResourceHandlerImpl.java:351) at javax.faces.application.ResourceHandlerWrapper.handleResourceRequest(ResourceHandlerWrapper.java:178) at org.primefaces.application.resource.PrimeResourceHandler.handleResourceRequest(PrimeResourceHandler.java:87) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:667) at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1580) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:338) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:100) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:250) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.glassfish.tyrus.servlet.TyrusServletFilter.doFilter(TyrusServletFilter.java:305) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:250) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:652) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:591) at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:371) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:238) at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:463) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:168) at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206) at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180) at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:242) at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119) at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284) at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201) at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133) at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112) at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77) at org.glassfish.grizzly.http2.Http2Session.sendMessageUpstream(Http2Session.java:1309) at org.glassfish.grizzly.http2.Http2Session.sendMessageUpstream(Http2Session.java:1285) at org.glassfish.grizzly.http2.Http2ServerFilter$2.run(Http2ServerFilter.java:1033) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:593) at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:573) at java.lang.Thread.run(Thread.java:748) java.io.IOException: Connection is closed at org.glassfish.grizzly.nio.NIOConnection.assertOpen(NIOConnection.java:460) at org.glassfish.grizzly.http.io.OutputBuffer.write(OutputBuffer.java:687) at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:336) at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:323) at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:154) at java.nio.channels.Channels$WritableByteChannelImpl.write(Channels.java:458) at com.sun.faces.application.resource.ResourceHandlerImpl.handleResourceRequest(ResourceHandlerImpl.java:351) at javax.faces.application.ResourceHandlerWrapper.handleResourceRequest(ResourceHandlerWrapper.java:178) at org.primefaces.application.resource.PrimeResourceHandler.handleResourceRequest(PrimeResourceHandler.java:87) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:667) at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1580) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:338) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:100) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:250) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.glassfish.tyrus.servlet.TyrusServletFilter.doFilter(TyrusServletFilter.java:305) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:250) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:652) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:591) at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:371) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:238) at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:463) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:168) at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206) at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180) at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:242) at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119) at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284) at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201) at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133) at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112) at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77) at org.glassfish.grizzly.http2.Http2Session.sendMessageUpstream(Http2Session.java:1309) at org.glassfish.grizzly.http2.Http2Session.sendMessageUpstream(Http2Session.java:1285) at org.glassfish.grizzly.http2.Http2ServerFilter$2.run(Http2ServerFilter.java:1033) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:593) at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:573) at java.lang.Thread.run(Thread.java:748) Caused by: java.io.IOException: Locally closed
    3. 困难在3和4,因为我不知道如何将正在运行的进程与其现有/不存在的服务联系起来。

      提前感谢您提供任何帮助,建议或代码。

1 个答案:

答案 0 :(得分:0)

也许是这样的事情:

$processes = Get-Process
$services = Get-WmiObject Win32_Service
foreach ( $process in $processes ) {
  if ( $process.Id -eq 0 ) {
    continue
  }
  $service = $services | Where-Object { $_.ProcessId -eq $process.Id }
  if ( $service ) {
    $isService = $true
    $serviceNames = $service.Name -as [Array]
  }
  else {
    $isService = $false
    $serviceNames = @($null)
  }
  foreach ( $serviceName in $serviceNames ) {
    [PSCustomObject] @{
      "ProcessId"   = $process.Id
      "ProcessName" = $process.Name
      "Service"     = $isService
      "ServiceName" = $serviceName
    }
  }
}

(可能有更快的方法,但这应该会给你一个想法。)