zoukankan      html  css  js  c++  java
  • 获取远程机器上的windows服务 完全有效已测试

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Management;
    using System.ServiceProcess;
    using System.Text;
    using System.Threading.Tasks;
     
    namespace ConsoleApp2
    {
    class Program
    {
    static void Main(string[] args)
    {
    //try
    //{
    // ServiceController ctrl = new ServiceController();
    // ctrl.MachineName = "192.168.1.10";
    // ctrl.ServiceName = "BFE";
    // Console.WriteLine(ctrl.Status.ToString());
    //}
    //catch(Exception ex)
    //{
    // Console.WriteLine(ex.Message);
    //}
     
    //Console.Read();
     
    ConnectionOptions op = new ConnectionOptions();
    //op.Username = "WIN-50QHRP5SDUI\administrator"; //Remote Mangerment User
    //op.Password = "RongDa2018";
    op.Username = "WIN-50QHRP5SDUI\administrator";
    op.Password = "RongDa2018";
    ManagementScope scope = new ManagementScope(@"\WIN-50QHRP5SDUI ootcimv2", op);
    scope.Connect();
     
    ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_Service ");//Where Name like 'Windows%'
    ManagementObjectSearcher query1 = new ManagementObjectSearcher(scope, oq);
    //获取WMI操作内容
    ManagementObjectCollection queryCollection1 = query1.Get();
    //根据使用者选择执行相应的远程操作
    int i = 0;
    foreach (ManagementObject service in queryCollection1)
    {
    Console.WriteLine(i++ + " " + service.GetPropertyValue("Name").ToString() + " " + service.GetPropertyValue("Caption").ToString() + " ab" + service.GetPropertyValue("State").ToString());
    }
     
     
    ManagementPath path = new ManagementPath("Win32_Service");
    ManagementClass services;
    services = new ManagementClass(scope, path, null);
     
    foreach (ManagementObject service in services.GetInstances())
    {
    //Console.WriteLine(i++ +" ab"+ service.GetPropertyValue("Name").ToString() + " ab" + service.GetPropertyValue("State").ToString());
    //if (service.GetPropertyValue("State").ToString().ToLower().Equals("running"))
    //{ // Do something }
     
    //}
     
    //return;
     
    //try
    //{
    // string pcname = "WIN-50QHRP5SDUI";
    // ConnectionOptions connectionOptions = new ConnectionOptions();
    // connectionOptions.Username = pcname + "administrator";
    // connectionOptions.Password = "RongDa2018";
    // //connectionOptions.Authority = "ntlmdomain:DOMAIN";
    // ManagementScope managementScope = new ManagementScope("\\" + pcname + "\root\cimv2", connectionOptions);
    // managementScope.Connect();
     
    // try
    // {
    // ServiceController ctrl = new ServiceController();
    // ctrl.MachineName = pcname;
    // ctrl.ServiceName = "BFE";
    // Console.WriteLine(ctrl.Status.ToString());
    // }
    // catch (Exception ex)
    // {
    // Console.WriteLine(ex.Message);
    // }
     
    // ////////string pcname = "192.168.1.81";
    // ////////ImpersonationUtil.Impersonate("rd","9527", pcname); //DESKTOP-4VQ2TCU
    // ////////// code you want to execute as impersonated user.....
     
    // ServiceController[] Services = ServiceController.GetServices(pcname); //192.168.1.20
    // foreach (ServiceController sc in Services)
    // {
    // Console.WriteLine(sc.DisplayName);
    // }
     
    // ImpersonationUtil.UnImpersonate();
    //}
    //catch (Exception ex)
    //{
    // Console.WriteLine(ex.Message);
    //}
     
    //Console.Read();
    }
    Console.Read();
     
    /////
    ///// 获取windows 服务状态
    /////
    /////服务名称
    /////服务器IP
    /////
    //public ServiceControllerStatus GetServiceState(string serviceName, string serverIP)
    //{
    // ServiceControllerStatus serviceSate;
    // using (ServiceController sc = ServiceController.GetServices(serverIP)
    // .FirstOrDefault(x => x.ServiceName == serviceName))
    // {
    // if (sc == null)
    // {
    // throw new Exception($"{serviceName}不存在于{serverIP}");
    // }
    // serviceSate = sc.Status;
    // sc.Close();
    // }
    // return serviceSate;
    //}
    }
     
     
    public String getServices()
    {
    string s = "";
    ManagementObjectCollection itemCollection2 = new ManagementObjectSearcher("SELECT * FROM Win32_Service").Get();
    foreach (ManagementObject MO in itemCollection2)
    {
    String displayName = (String)MO["Caption"];
    s += displayName + " ";
    }
    return s;
    }
    }
    }
     
  • 相关阅读:
    mockm remote 不能正常使用的解决方案
    uviewui 的 bug 当 url 的 query 参数中有 http://127.0.0.1/ 并且省略请求前缀的时候, 就会导致请求无法发送出去
    检查 nodmeon 是否可以修改文件后重启应用
    mocha 如何延迟指定时间后再运行所有用例
    使用 babel 编译 js 为 es5 支持 ie
    git 摘取提交
    Makefile学习笔记
    使用 Service Worker 缓解网站 DDOS 攻击
    如何热更新长缓存的 HTTP 资源
    网站图片无缝兼容 WebP/AVIF
  • 原文地址:https://www.cnblogs.com/devgis/p/14180821.html
Copyright © 2011-2022 走看看