• 获取远程机器上的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;
    }
    }
    }
     
  • 相关阅读:
    老司机心得之时间管理"入坑"
    shell的字符串和数字的转化(数字自动做字符串处理,变量名做字符串输出用单引号)
    shell的date命令:使用方法,以及小时、分钟的计算
    一篇详细的linux中shell语言的字符串处理
    linux的string操作(字符串截取,长度计算)
    linux下数学运算器:expr命令(shell中完成数学运算)
    shell脚本格式的几点注意:格式严格,空格不能随便出现(一写就记不住)
    vim的颜色修改,高亮设置。
    pyhton exit
    python判断类型:想知道一个对象(实例或者变量)是什么类型,什么结构的
  • 原文地址:https://www.cnblogs.com/devgis/p/14180821.html
Copyright © 2020-2023  润新知