Search code examples
lispautolisp

AutoLisp - Run PowerShell and Command Prompt Commands


I'm looking to pull some system information into a cad program (DraftSight), using AutoLisp. I have most of what I'm trying to complete, but I'm having trouble in understanding how I'm suppose to pull the information back into the AutoLisp program.

I'm mainly trying to see the available RAM, the percentage of the CPU usage, and the number of enabled cores. I've found several PowerShell and Command Prompt commands to get these values, but the returned value is an object (see below), and it seems to be returning before the remote script is finished.

Commands

;;; ------------------------------------------------------------------------------------ ;;;

;; Machine's RAM Availability and Total

;; PowerShell (fast ~instant)
;; Availability (MiB) : (Get-WmiObject -Class WIN32_OperatingSystem).FreePhysicalMemory/1024
;; Total (MiB)        : (Get-WmiObject -Class WIN32_OperatingSystem).TotalVisibleMemorySize/1024

;; PowerShell (slow ~4s)
;; Setting varialbe   : $General_Info = systeminfo
;; Availability (MiB) : ($General_Info | Select-String 'Available Physical Memory:').ToString().Split(':')[1].Trim()
;; Total (MiB)        : ($General_Info | Select-String 'Total Physical Memory:'    ).ToString().Split(':')[1].Trim()

;; Legacy Software

;; Command Prompt (fast ~instant)
;; Availability (kiB) : wmic OS get FreePhysicalMemory
;; Total(kiB)         : wmic OS get TotalVisibleMemorySize

;; Command Prompt (slow ~8s)
;; Availability (MiB) : systeminfo | find "Available Physical Memory"
;; Total(kiB)         : systeminfo | find "Total Physical Memory"

;;; ------------------------------------------------------------------------------------ ;;;

;; Machine's CPU percentage and count

;; PowerShell (moderate ~0.5s)
;; Load Percentage     : gwmi Win32_PerfFormattedData_PerfOS_Processor | Select -First 1 | %{'{0}%' -f $_.PercentProcessorTime}
;; NumberOfEnabledCore : 

;; PowerShell (slow ~2s)
;; Load Percentage     : (Get-WmiObject win32_processor).LoadPercentage
;; NumberOfEnabledCore : (Get-WmiObject win32_processor).NumberOfEnabledCore

;; Legacy Software

;; Command Prompt (semi-slow ~2s)
;; Load Percentage     : wmic cpu get loadpercentage
;; NumberOfEnabledCore : wmic cpu get NumberOfEnabledCore

Code

(progn
    (setq WSript (vlax-get-or-create-object "WScript.Shell"))
    (setq PShell (vlax-invoke WSript 'exec "Powershell.exe (Get-WmiObject win32_processor).LoadPercentage"))
    (vlax-dump-object PShell)
    (vlax-release-object WSript)
    (vlax-release-object PShell)
    (gc)
)
; IWshExec: WSH Exec Object
; Property values:
;   ExitCode = Parameterized properties not displayed
;   ProcessID = Parameterized properties not displayed
;   Status = Parameterized properties not displayed
;   StdErr = Parameterized properties not displayed
;   StdIn = Parameterized properties not displayed
;   StdOut = Parameterized properties not displayed
(progn
    (setq WSript (vlax-get-or-create-object "WScript.Shell"))
    (setq PShell (vlax-invoke WSript 'exec "cmd.exe wmic cpu get loadpercentage"))
    (vlax-dump-object PShell)
    (vlax-release-object WSript)
    (vlax-release-object PShell)
    (gc)
)
; IWshExec: WSH Exec Object
; Property values:
;   ExitCode = Parameterized properties not displayed
;   ProcessID = Parameterized properties not displayed
;   Status = Parameterized properties not displayed
;   StdErr = Parameterized properties not displayed
;   StdIn = Parameterized properties not displayed
;   StdOut = Parameterized properties not displayed

Solution

  • There's no need to invoke Powershell via Windows Script Host, as you can query WMI directly, e.g.:

    (defun LM:wmiquery ( cls prp / qry rtn srv wmi )
        (if (setq wmi (vlax-create-object "wbemscripting.swbemlocator"))
            (progn
                (setq rtn
                    (vl-catch-all-apply
                       '(lambda ( / rtn )
                            (setq srv (vlax-invoke wmi 'connectserver)
                                  qry (vlax-invoke srv 'execquery (strcat "select " prp " from " cls))
                            )
                            (vlax-for itm qry (setq rtn (cons (vlax-get itm prp) rtn)))
                        )
                    )
                )
                (foreach obj (list qry srv wmi)
                    (if (= 'vla-object (type obj)) (vlax-release-object obj))
                )
                (if (not (vl-catch-all-error-p rtn)) rtn)
            )
        )
    )
    
    _$ (LM:wmiquery "WIN32_OperatingSystem" "FreePhysicalMemory")
    ("23059220")
    _$ (LM:wmiquery "WIN32_OperatingSystem" "TotalVisibleMemorySize")
    ("33343316")