3 people like it.

WMI to JSON

Simple fs script to convert a wql statement to a json string

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
#r "System.Management.dll"
#r "System.Web.Extensions.dll"

open System.Collections.Generic
open System.Management
open System.Web.Script.Serialization

let json = new JavaScriptSerializer()
let toTuple (p: PropertyData) = p.Name, p.Value

let fromWmi query =
    let searcher = new ManagementObjectSearcher(ObjectQuery query)

    Seq.cast<ManagementObject>(searcher.Get())
    |> Seq.map (fun x -> x.Properties |> Seq.cast |> Seq.map toTuple)
    |> Seq.map (Map.ofSeq >> Dictionary)

fromWmi "SELECT Name, Size FROM Win32_LogicalDisk"
|> json.Serialize
|> printfn "%s"
namespace System
namespace System.Collections
namespace System.Collections.Generic
namespace System.Management
namespace System.Web
namespace System.Web.Script
namespace System.Web.Script.Serialization
val json : JavaScriptSerializer

Full name: Script.json
Multiple items
type JavaScriptSerializer =
  new : unit -> JavaScriptSerializer + 1 overload
  member ConvertToType<'T> : obj:obj -> 'T + 1 overload
  member Deserialize<'T> : input:string -> 'T + 1 overload
  member DeserializeObject : input:string -> obj
  member MaxJsonLength : int with get, set
  member RecursionLimit : int with get, set
  member RegisterConverters : converters:IEnumerable<JavaScriptConverter> -> unit
  member Serialize : obj:obj -> string + 1 overload

Full name: System.Web.Script.Serialization.JavaScriptSerializer

--------------------
JavaScriptSerializer() : unit
JavaScriptSerializer(resolver: JavaScriptTypeResolver) : unit
val toTuple : p:PropertyData -> string * obj

Full name: Script.toTuple
val p : PropertyData
type PropertyData =
  member IsArray : bool
  member IsLocal : bool
  member Name : string
  member Origin : string
  member Qualifiers : QualifierDataCollection
  member Type : CimType
  member Value : obj with get, set

Full name: System.Management.PropertyData
property PropertyData.Name: string
property PropertyData.Value: obj
val fromWmi : query:string -> seq<Dictionary<string,obj>>

Full name: Script.fromWmi
val query : string
val searcher : ManagementObjectSearcher
Multiple items
type ManagementObjectSearcher =
  inherit Component
  new : unit -> ManagementObjectSearcher + 6 overloads
  member Get : unit -> ManagementObjectCollection + 1 overload
  member Options : EnumerationOptions with get, set
  member Query : ObjectQuery with get, set
  member Scope : ManagementScope with get, set

Full name: System.Management.ManagementObjectSearcher

--------------------
ManagementObjectSearcher() : unit
ManagementObjectSearcher(queryString: string) : unit
ManagementObjectSearcher(query: ObjectQuery) : unit
ManagementObjectSearcher(scope: string, queryString: string) : unit
ManagementObjectSearcher(scope: ManagementScope, query: ObjectQuery) : unit
ManagementObjectSearcher(scope: string, queryString: string, options: EnumerationOptions) : unit
ManagementObjectSearcher(scope: ManagementScope, query: ObjectQuery, options: EnumerationOptions) : unit
Multiple items
type ObjectQuery =
  inherit ManagementQuery
  new : unit -> ObjectQuery + 2 overloads
  member Clone : unit -> obj

Full name: System.Management.ObjectQuery

--------------------
ObjectQuery() : unit
ObjectQuery(query: string) : unit
ObjectQuery(language: string, query: string) : unit
module Seq

from Microsoft.FSharp.Collections
val cast : source:System.Collections.IEnumerable -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.cast
Multiple items
type ManagementObject =
  inherit ManagementBaseObject
  new : unit -> ManagementObject + 6 overloads
  member ClassPath : ManagementPath
  member Clone : unit -> obj
  member CopyTo : path:ManagementPath -> ManagementPath + 7 overloads
  member Delete : unit -> unit + 3 overloads
  member Dispose : unit -> unit
  member Get : unit -> unit + 1 overload
  member GetMethodParameters : methodName:string -> ManagementBaseObject
  member GetRelated : unit -> ManagementObjectCollection + 5 overloads
  member GetRelationships : unit -> ManagementObjectCollection + 5 overloads
  ...

Full name: System.Management.ManagementObject

--------------------
ManagementObject() : unit
ManagementObject(path: ManagementPath) : unit
ManagementObject(path: string) : unit
ManagementObject(path: ManagementPath, options: ObjectGetOptions) : unit
ManagementObject(path: string, options: ObjectGetOptions) : unit
ManagementObject(scope: ManagementScope, path: ManagementPath, options: ObjectGetOptions) : unit
ManagementObject(scopeString: string, pathString: string, options: ObjectGetOptions) : unit
ManagementObjectSearcher.Get() : ManagementObjectCollection
ManagementObjectSearcher.Get(watcher: ManagementOperationObserver) : unit
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
val x : ManagementObject
property ManagementBaseObject.Properties: PropertyDataCollection
Multiple items
module Map

from Microsoft.FSharp.Collections

--------------------
type Map<'Key,'Value (requires comparison)> =
  interface IEnumerable
  interface IComparable
  interface IEnumerable<KeyValuePair<'Key,'Value>>
  interface ICollection<KeyValuePair<'Key,'Value>>
  interface IDictionary<'Key,'Value>
  new : elements:seq<'Key * 'Value> -> Map<'Key,'Value>
  member Add : key:'Key * value:'Value -> Map<'Key,'Value>
  member ContainsKey : key:'Key -> bool
  override Equals : obj -> bool
  member Remove : key:'Key -> Map<'Key,'Value>
  ...

Full name: Microsoft.FSharp.Collections.Map<_,_>

--------------------
new : elements:seq<'Key * 'Value> -> Map<'Key,'Value>
val ofSeq : elements:seq<'Key * 'T> -> Map<'Key,'T> (requires comparison)

Full name: Microsoft.FSharp.Collections.Map.ofSeq
Multiple items
type Dictionary<'TKey,'TValue> =
  new : unit -> Dictionary<'TKey, 'TValue> + 5 overloads
  member Add : key:'TKey * value:'TValue -> unit
  member Clear : unit -> unit
  member Comparer : IEqualityComparer<'TKey>
  member ContainsKey : key:'TKey -> bool
  member ContainsValue : value:'TValue -> bool
  member Count : int
  member GetEnumerator : unit -> Enumerator<'TKey, 'TValue>
  member GetObjectData : info:SerializationInfo * context:StreamingContext -> unit
  member Item : 'TKey -> 'TValue with get, set
  ...
  nested type Enumerator
  nested type KeyCollection
  nested type ValueCollection

Full name: System.Collections.Generic.Dictionary<_,_>

--------------------
Dictionary() : unit
Dictionary(capacity: int) : unit
Dictionary(comparer: IEqualityComparer<'TKey>) : unit
Dictionary(dictionary: IDictionary<'TKey,'TValue>) : unit
Dictionary(capacity: int, comparer: IEqualityComparer<'TKey>) : unit
Dictionary(dictionary: IDictionary<'TKey,'TValue>, comparer: IEqualityComparer<'TKey>) : unit
JavaScriptSerializer.Serialize(obj: obj) : string
JavaScriptSerializer.Serialize(obj: obj, output: System.Text.StringBuilder) : unit
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Raw view Test code New version

More information

Link:http://fssnip.net/sz
Posted:8 years ago
Author:max
Tags: json , wmi , wql