WMI Macro

From NSIS Wiki
Jump to navigationJump to search
Author: Zinthose (talk, contrib)


About

Macro that uses the nsExec::ExecToStack plugin to execute the WMIC command line utility to easily collect WMI data.

Example

    ${WMIC} $0 ComputerSystem Model
    ${WMIC} $1 ComputerSystem Manufacturer
 
    DetailPrint `Model: $0`
    DetailPrint `Manufacturer: $1`

Macro

/*  Macro to remove leading and trailing white spaces from a string.
    Derived from the function originally posted by Iceman_K at: 
    http://nsis.sourceforge.net/Remove_leading_and_trailing_whitespaces_from_a_string
    --------------------------------------------------------------------------------- */
        !ifmacrondef _Trim
            !macro _Trim _UserVar _OriginalString
                !define Trim_UID ${__LINE__}
 
                Push $R1
                Push $R2
                Push `${_OriginalString}`
                Pop $R1
 
                Loop_${Trim_UID}:
                    StrCpy $R2 "$R1" 1
                    StrCmp "$R2" " " TrimLeft_${Trim_UID}
                    StrCmp "$R2" "$\r" TrimLeft_${Trim_UID}
                    StrCmp "$R2" "$\n" TrimLeft_${Trim_UID}
                    StrCmp "$R2" "$\t" TrimLeft_${Trim_UID}
                    GoTo Loop2_${Trim_UID}
                TrimLeft_${Trim_UID}:   
                    StrCpy $R1 "$R1" "" 1
                    Goto Loop_${Trim_UID}
 
                Loop2_${Trim_UID}:
                    StrCpy $R2 "$R1" 1 -1
                    StrCmp "$R2" " " TrimRight_${Trim_UID}
                    StrCmp "$R2" "$\r" TrimRight_${Trim_UID}
                    StrCmp "$R2" "$\n" TrimRight_${Trim_UID}
                    StrCmp "$R2" "$\t" TrimRight_${Trim_UID}
                    GoTo Done_${Trim_UID}
                TrimRight_${Trim_UID}:  
                    StrCpy $R1 "$R1" -1
                    Goto Loop2_${Trim_UID}
 
                Done_${Trim_UID}:
                    Pop $R2
                    Exch $R1
                    Pop ${_UserVar}
                !undef Trim_UID
            !macroend
            !ifndef Trim
                !define Trim `!insertmacro _Trim`
            !endif
        !endif
 
/*  WMIC - Retrieves a single property value from a WMI Class
    --------------------------------------------------------- */
    !ifmacrondef _WMIC
        !macro _WMIC _USERVAR _CLASSNAME _PROPERTY
            !define WMIC_UID ${__LINE__}
            ClearErrors
 
            Push $0
            Push $1
            Push $2
 
            Push ${_USERVAR}
            Push ${_CLASSNAME}
            Push ${_PROPERTY}
 
            Pop $2 ; _PROPERTY
            Pop $1 ; _CLASSNAME
            Pop $0 ; _USERVAR
 
            nsExec::ExecToStack /OEM 'WMIC $1 Get $2 /FORMAT:textvaluelist.xsl'
            Pop $0
 
            StrCmp $0 0 0 Else_${WMIC_UID}
                Pop $0
                ${Trim} $0 $0
                StrLen $1 $2
                IntOp $1 $1 + 1
                StrCpy $0 $0 ${NSIS_MAX_STRLEN} $1
                Goto End_${WMIC_UID}
            Else_${WMIC_UID}:
                SetErrors
                StrCpy $0 ""
            End_${WMIC_UID}:
 
            Pop  $2
            Pop  $1
            Exch $0
            Pop ${_USERVAR}
 
            !undef WMIC_UID
        !macroend
        !ifndef WMIC
            !define WMIC "!insertmacro _WMIC"
        !endif
    !endif