AutoLisp - 运行 PowerShell 和命令提示符命令

问题描述 投票:0回答:1

我希望使用 AutoLisp 将一些系统信息提取到 cad 程序 (DraftSight) 中。我已经完成了我想要完成的大部分工作,但我很难理解我应该如何将信息拉回到 AutoLisp 程序中。

我主要是想查看可用内存、CPU 使用百分比和启用的内核数。我找到了几个 PowerShell 和命令提示符命令来获取这些值,但返回值是一个对象(见下文),它似乎在远程脚本完成之前返回。

命令

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

;; 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

代码

(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
lisp autolisp
1个回答
0
投票

无需通过 Windows Script Host 调用 Powershell,因为您可以直接查询 WMI,例如:

(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")
© www.soinside.com 2019 - 2024. All rights reserved.