Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require('dotenv').config()
- const Promise = require('bluebird')
- const WmiClient = require('wmi-client')
- // let's boiler plate this to use later in the #map
- const promisifiedClient = opts => Promise.promisifyAll(new WmiClient(opts))
- /* wmi queries functions */
- async function getWMI(wmi) {
- var osVersion = await wmi.queryAsync('SELECT SerialNumber,Caption,Version FROM Win32_OperatingSystem')
- var services = await wmi.queryAsync('select Caption, Status, Started, ProcessId, DisplayName, Name, PathName from Win32_Service')
- return { osVersion, services }
- }
- // @todo should be getting this from a config file or something
- let servers = [
- { host:'10.0.10.95', name:'server1', username:process.env.ad_user, password:process.env.ad_pass },
- { host:'10.0.10.10', name:'server2', username:process.env.ad_user, password:process.env.ad_pass }
- ]
- /* pass in the servers array so this function itself does not mutate. */
- function populateWmiClients(servers) {
- // i was wrong, this is not async and doesn't make the connection
- // also ALWAYS RETURN THE THING
- return servers.map((server) => {
- const wmiOptions = {
- host: server.host,
- username: server.username,
- password: server.password,
- }
- // return a *new* object, not mutating the current ones in place
- return Object.assign({}, server, { wmi: promisifiedClient(wmiOptions) })
- })
- }
- /* this will be repeating every 5s */
- // again, PASS IN THE THING YOU'RE REPLACING, don't mutate in the function
- function getWmiData(servers) {
- // ALWAYS RETURN YOUR PROMISES
- return Promise.map(servers, s => getWMI(s.wmi).then(data => Object.assign({}, s, { data })))
- }
- function updateLoop(oldServers) => {
- return getWmiData(oldServers).then((newServers) => {
- // @TODO need a function to set these instead of replacing global in place
- servers = newServers
- // starting at 10 seconds, can always change it
- // Object.assign clones servers so you aren't mutating the elements in place
- return Promise.delay(10000).then(() => updateLoop(Object.assign({}, servers)))
- })
- })
- Promise.try(() => {
- return populateWmiClients(servers)
- }).then((connectedServers) => {
- // connectedServers here is a *new array*, with the clients on each object.. now that we have them all, replace the global state
- // @TODO create a state setter function instead of just overwriting it here
- servers = connectedServers
- }).then(() => {
- updateLoop(servers)
- })
Add Comment
Please, Sign In to add comment