Version 3 of Remote exec with tcom and WMI

Updated 2014-09-13 10:43:14 by Janni

Often one needs to execute a command on a remote Windows computer, WMI & tcom can be used to do just that.

Thanks to Jaf who helped a bit in sorting it out.

proc remote_exec {command machine} {
    set    wmistr "winmgmts:\{impersonationLevel=impersonate\}!"
    append wmistr "\\\\$machine\\root\\cimv2:Win32_Process"
    # get the Win32_Process class
    if {[catch {::tcom::ref getobject $wmistr} wmiproc]} {
        return -code error -1
      }
    # there is a method called Methods_, get it
    set wmiprocmeth [$wmiproc Methods_]
    # it contains an array, get the Create method out of it
    set wmiprocmethcreat [$wmiprocmeth Item "Create"]
    # get the inparameters
    set inparams [[$wmiprocmethcreat InParameters] SpawnInstance_]
    # The properties_ of inparams are what we are after
    set cmdline [[$inparams Properties_] Item "CommandLine"]
    set cwd     [[$inparams Properties_] Item "CurrentDirectory"] 
    set pstart  [[$inparams Properties_] Item "ProcessStartupInformation"] 
    # Set the properties
    $cmdline Value $command 
    # Now exec
    set result [$wmiproc ExecMethod_ "Create" $inparams]
    return [[[$result Properties_] Item "ReturnValue"] Value]
  }