简体   繁体   中英

How to target multiple remote nodes in WQL?

I'm creating a service in C# that polls remote hosts in an Active Directory domain on a regular interval with WMI. I've been using the System.Management.ManagementObjectSearcher but so far I've only gotten it to work with the local host.

Example:

private List<Disk> GetDisks()
{
    var disks = new List<Disk>();

    var searcher = new ManagementObjectSearcher("select deviceid, volumename, description, filesystem, freespace, size from win32_logicaldisk");
    foreach (var obj in searcher.Get())
    {
        var id = obj.Properties["deviceid"].Value.ToString();
        var name = obj.Properties["volumename"].Value.ToString();
        var description = obj.Properties["description"].Value.ToString();
        var fileSystem = obj.Properties["filesystem"].Value.ToString();
        var freeSpace = Convert.ToUInt64(obj.Properties["freespace"].Value);
        var totalSpace = Convert.ToUInt64(obj.Properties["size"].Value);

        var disk = new Disk(id, name, description, fileSystem, freeSpace, totalSpace);

        disks.Add(disk);
    }

    return disks;
}

However, I have a list of machine hostnames that I need to poll. This can easily be done with WMIC like so:

wmic /node:"hostname1","hostname2","hostname3" logicaldisk get deviceid, volumename, description, filesystem, freespace, size /format:list

but I cannot figure out how to do it in C#.

Any help greatly appreciated.

You can use ORMi to create multiple WMI connections and get the info you need.

You could do something like this:

//Supposing you got the info on a list of computers

List<Computer> computers = new List<Computers>();

foreach(Computer c in computers)
{
    WMIHelper helper = new WMIHelper("root\\CimV2", c.WorkstationName);

    var info = helper.Query("select deviceid, volumename, description, filesystem, freespace, size from win32_logicaldisk").ToList();
}

Refer to the documentation for more specific data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM