replace WMIC with powershell

This commit is contained in:
deffcolony
2025-05-06 20:24:03 +02:00
parent 8d3c46e0e0
commit e93ed7c584
12 changed files with 96 additions and 82 deletions

View File

@@ -516,34 +516,41 @@ if not exist "%st_install_path%" (
goto :no_st_install_path
)
REM Run PowerShell command to retrieve VRAM size and divide by 1GB
REM Initialize variables for VRAM detection
set /a iteration=0
set /a last_UVRAM=0
set "GPU_name=Unknown"
set "last_GPU=Unknown"
REM Detect GPU and store name, excluding integrated GPUs if discrete GPUs are found
for /f "tokens=2 delims==" %%f in ('wmic path Win32_VideoController get name /value ^| find "="') do (
REM Run PowerShell command to retrieve VRAM size and divide by 1GB
for /f "usebackq tokens=*" %%i in (`powershell -Command "$qwMemorySize = (Get-ItemProperty -Path 'HKLM:\SYSTEM\ControlSet001\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}\0*' -Name HardwareInformation.qwMemorySize -ErrorAction SilentlyContinue).'HardwareInformation.qwMemorySize'; if ($null -ne $qwMemorySize -and $qwMemorySize -is [array]) { $qwMemorySize = [double]$qwMemorySize[!iteration!] } else { $qwMemorySize = [double]$qwMemorySize }; if ($null -ne $qwMemorySize) { [math]::Round($qwMemorySize/1GB) } else { 'Property not found' }"`) do (
set "UVRAM=%%i"
)
set /a iteration=!iteration!+1
REM If the VRAM is greater than 0 (it always should be, but just in case...) AND the lastUVRAM is not the same as VRAM
REM (might have to change this in the case where two cards have the same VRAM, but the user wants the first card...
REM in which case we would want to see if it is the first iteration or not... but I'm not here to do all your work!)
REM AND the lastUVRAM is Grt than VRAM... set it to the previous card. We don't have to check the other way, because
REM we already set VRAM. This way takes more builds into account... but not all.
if /i !lastUVRAM! gtr 0 (
if /i !lastUVRAM! neq !UVRAM! (
if /i !lastUVRAM! gtr !UVRAM! (
set "UVRAM=!lastUVRAM!"
@REM set "GPU_name=!last_GPU!"
)
)
) else (
set "lastUVRAM=!UVRAM!"
)
for /f "tokens=*" %%f in ('powershell -Command "Get-CimInstance Win32_VideoController | Select-Object -ExpandProperty Name"') do (
REM Update GPU name and store previous name
if "!GPU_name!"=="" (
set "GPU_name=%%f"
) else if "!GPU_name!" neq "%%f" (
set "last_GPU=!GPU_name!"
set "GPU_name=%%f"
)
REM Run PowerShell command to retrieve VRAM size and divide by 1GB
for /f "usebackq tokens=*" %%i in (`powershell -Command "$qwMemorySize = (Get-ItemProperty -Path 'HKLM:\SYSTEM\ControlSet001\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}\0*' -Name HardwareInformation.qwMemorySize -ErrorAction SilentlyContinue).'HardwareInformation.qwMemorySize'; if ($null -ne $qwMemorySize -and $qwMemorySize -is [array]) { $qwMemorySize = [double]$qwMemorySize[!iteration!] } else { $qwMemorySize = [double]$qwMemorySize }; if ($null -ne $qwMemorySize) { [math]::Round($qwMemorySize/1GB) } else { 'Property not found' }"`) do (
set "UVRAM=%%i"
)
REM Increment iteration for array indexing
set /a iteration=!iteration!+1
REM Update UVRAM and GPU name only if current UVRAM is greater than last_UVRAM
if /i !UVRAM! gtr !last_UVRAM! (
set /a last_UVRAM=!UVRAM!
set "last_GPU=!GPU_name!"
)
)
REM Restore the GPU name and UVRAM to the one with the highest VRAM
set "UVRAM=!last_UVRAM!"
set "GPU_name=!last_GPU!"
REM Change the current directory to 'sillytavern' folder
cd /d "%st_install_path%"

View File

@@ -4,7 +4,6 @@ title STL [VRAM INFO]
chcp 65001 > nul
setlocal enabledelayedexpansion
REM Confirm script start and input arguments
set "UVRAM=%~1"
@@ -17,10 +16,10 @@ if not defined UVRAM (
set "UVRAM=0"
)
REM Get GPU information safely
REM Get GPU information safely using PowerShell
set "gpu_info="
for /f "skip=1 delims=" %%i in ('wmic path win32_videocontroller get caption') do (
if not "%%i"=="" set "gpu_info=!gpu_info! %%i"
for /f "tokens=*" %%i in ('powershell -Command "Get-CimInstance Win32_VideoController | Select-Object -ExpandProperty Name -First 1"') do (
if not "%%i"=="" set "gpu_info=%%i"
)
cls

View File

@@ -14,8 +14,9 @@ echo 0. Cancel
setlocal enabledelayedexpansion
chcp 65001 > nul
REM Get GPU information
for /f "skip=1 delims=" %%i in ('wmic path win32_videocontroller get caption') do (
set "gpu_info=!gpu_info! %%i"
set "gpu_info="
for /f "tokens=*" %%i in ('powershell -Command "Get-CimInstance Win32_VideoController | Select-Object -ExpandProperty Name -First 1"') do (
set "gpu_info=%%i"
)
echo.

View File

@@ -14,8 +14,9 @@ echo 0. Cancel
setlocal enabledelayedexpansion
chcp 65001 > nul
REM Get GPU information
for /f "skip=1 delims=" %%i in ('wmic path win32_videocontroller get caption') do (
set "gpu_info=!gpu_info! %%i"
set "gpu_info="
for /f "tokens=*" %%i in ('powershell -Command "Get-CimInstance Win32_VideoController | Select-Object -ExpandProperty Name -First 1"') do (
set "gpu_info=%%i"
)
echo.

View File

@@ -14,8 +14,9 @@ echo 0. Cancel
setlocal enabledelayedexpansion
chcp 65001 > nul
REM Get GPU information
for /f "skip=1 delims=" %%i in ('wmic path win32_videocontroller get caption') do (
set "gpu_info=!gpu_info! %%i"
set "gpu_info="
for /f "tokens=*" %%i in ('powershell -Command "Get-CimInstance Win32_VideoController | Select-Object -ExpandProperty Name -First 1"') do (
set "gpu_info=%%i"
)
echo.

View File

@@ -14,8 +14,9 @@ echo 0. Cancel
setlocal enabledelayedexpansion
chcp 65001 > nul
REM Get GPU information
for /f "skip=1 delims=" %%i in ('wmic path win32_videocontroller get caption') do (
set "gpu_info=!gpu_info! %%i"
set "gpu_info="
for /f "tokens=*" %%i in ('powershell -Command "Get-CimInstance Win32_VideoController | Select-Object -ExpandProperty Name -First 1"') do (
set "gpu_info=%%i"
)
echo.

View File

@@ -14,8 +14,9 @@ echo 0. Cancel
setlocal enabledelayedexpansion
chcp 65001 > nul
REM Get GPU information
for /f "skip=1 delims=" %%i in ('wmic path win32_videocontroller get caption') do (
set "gpu_info=!gpu_info! %%i"
set "gpu_info="
for /f "tokens=*" %%i in ('powershell -Command "Get-CimInstance Win32_VideoController | Select-Object -ExpandProperty Name -First 1"') do (
set "gpu_info=%%i"
)
echo.

View File

@@ -16,8 +16,9 @@ echo 0. Cancel
setlocal enabledelayedexpansion
chcp 65001 > nul
REM Get GPU information
for /f "skip=1 delims=" %%i in ('wmic path win32_videocontroller get caption') do (
set "gpu_info=!gpu_info! %%i"
set "gpu_info="
for /f "tokens=*" %%i in ('powershell -Command "Get-CimInstance Win32_VideoController | Select-Object -ExpandProperty Name -First 1"') do (
set "gpu_info=%%i"
)
echo.

View File

@@ -16,8 +16,9 @@ echo 0. Cancel
setlocal enabledelayedexpansion
chcp 65001 > nul
REM Get GPU information
for /f "skip=1 delims=" %%i in ('wmic path win32_videocontroller get caption') do (
set "gpu_info=!gpu_info! %%i"
set "gpu_info="
for /f "tokens=*" %%i in ('powershell -Command "Get-CimInstance Win32_VideoController | Select-Object -ExpandProperty Name -First 1"') do (
set "gpu_info=%%i"
)
echo.

View File

@@ -15,8 +15,9 @@ echo 0. Cancel
setlocal enabledelayedexpansion
chcp 65001 > nul
REM Get GPU information
for /f "skip=1 delims=" %%i in ('wmic path win32_videocontroller get caption') do (
set "gpu_info=!gpu_info! %%i"
set "gpu_info="
for /f "tokens=*" %%i in ('powershell -Command "Get-CimInstance Win32_VideoController | Select-Object -ExpandProperty Name -First 1"') do (
set "gpu_info=%%i"
)
echo.

View File

@@ -8,40 +8,37 @@ set "last_GPU=Unknown"
set "GPU_name=Unknown"
set "VRAM=Unknown"
set "primary_gpu_found=false"
REM Detect GPU and store name, excluding integrated GPUs if discrete GPUs are found
for /f "tokens=2 delims==" %%f in ('wmic path Win32_VideoController get name /value ^| find "="') do (
REM If GPU name is blank, set it; else, if it is not blank and does not equal the current value of f,
REM move gpu name to last_gpu and set gpu_name to the new value. This is here so that if the current card has less
REM VRAM than the previous card, we can set the name back properly. I doubt it would be needed... but you never know.
if /i !GPU_name! equ "" (
set "GPU_name=%%f"
) else if !GPU_name! neq %%f (
set "last_GPU=!GPU_name!"
set "GPU_name=%%f"
)
REM Run PowerShell command to retrieve VRAM size and divide by 1GB
for /f "usebackq tokens=*" %%i in (`powershell -Command "$qwMemorySize = (Get-ItemProperty -Path 'HKLM:\SYSTEM\ControlSet001\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}\0*' -Name HardwareInformation.qwMemorySize -ErrorAction SilentlyContinue).'HardwareInformation.qwMemorySize'; if ($null -ne $qwMemorySize -and $qwMemorySize -is [array]) { $qwMemorySize = [double]$qwMemorySize[!iteration!] } else { $qwMemorySize = [double]$qwMemorySize }; if ($null -ne $qwMemorySize) { [math]::Round($qwMemorySize/1GB) } else { 'Property not found' }"`) do (
set "VRAM=%%i"
)
REM iterate so we can move through the objects in the powershell script...
set /a iteration=!iteration!+1
REM If the VRAM is greater than 0 (it always should be, but just in case...) AND the lastVRAM is not the same as VRAM
REM (might have to change this in the case where two cards have the same VRAM, but the user wants the first card...
REM in which case we would want to see if it is the first iteration or not... but I'm not here to do all your work!)
REM AND the lastVRAM is Grt than VRAM... set it to the previous card. We don't have to check the other way, because
REM we already set VRAM. This way takes more builds into account... but not all.
if /i !lastVRAM! gtr 0 (
if /i !lastVRAM! neq !VRAM! (
if /i !lastVRAM! gtr !VRAM! (
set "VRAM=!lastVRAM!"
set "GPU_name=!last_GPU!"
)
)
) else (
set "lastVRAM=!VRAM!"
)
for /f "tokens=*" %%f in ('powershell -Command "Get-CimInstance Win32_VideoController | Select-Object -ExpandProperty Name"') do (
REM If GPU name is blank, set it; else, if it is not blank and does not equal the current value of f,
REM move gpu name to last_gpu and set gpu_name to the new value.
if /i "!GPU_name!" equ "" (
set "GPU_name=%%f"
) else if "!GPU_name!" neq "%%f" (
set "last_GPU=!GPU_name!"
set "GPU_name=%%f"
)
REM Run PowerShell command to retrieve VRAM size and divide by 1GB
for /f "usebackq tokens=*" %%i in (`powershell -Command "$qwMemorySize = (Get-ItemProperty -Path 'HKLM:\SYSTEM\ControlSet001\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}\0*' -Name HardwareInformation.qwMemorySize -ErrorAction SilentlyContinue).'HardwareInformation.qwMemorySize'; if ($null -ne $qwMemorySize -and $qwMemorySize -is [array]) { $qwMemorySize = [double]$qwMemorySize[!iteration!] } else { $qwMemorySize = [double]$qwMemorySize }; if ($null -ne $qwMemorySize) { [math]::Round($qwMemorySize/1GB) } else { 'Property not found' }"`) do (
set "VRAM=%%i"
)
REM Iterate to move through the objects in the PowerShell script
set /a iteration=!iteration!+1
REM Update VRAM and GPU name only if current VRAM is greater than last_VRAM
if /i !VRAM! gtr !last_VRAM! (
set /a last_VRAM=!VRAM!
set "last_GPU=!GPU_name!"
)
)
REM Restore the GPU name and VRAM to the one with the highest VRAM
set "GPU_name=!last_GPU!"
set "VRAM=!last_VRAM!"
REM Display GPU name and VRAM
echo GPU: %cyan_fg_strong%%GPU_name%%reset% - VRAM: %cyan_fg_strong%%VRAM%%reset% GB
endlocal
echo GPU: %cyan_fg_strong%!GPU_name!%reset% - VRAM: %cyan_fg_strong%!VRAM!%reset% GB
endlocal

View File

@@ -454,8 +454,9 @@ echo 0. Cancel install
setlocal enabledelayedexpansion
chcp 65001 > nul
REM Get GPU information
for /f "skip=1 delims=" %%i in ('wmic path win32_videocontroller get caption') do (
set "gpu_info=!gpu_info! %%i"
set "gpu_info="
for /f "tokens=*" %%i in ('powershell -Command "Get-CimInstance Win32_VideoController | Select-Object -ExpandProperty Name -First 1"') do (
set "gpu_info=%%i"
)
echo.
@@ -624,8 +625,9 @@ echo 0. Cancel
setlocal enabledelayedexpansion
chcp 65001 > nul
REM Get GPU information
for /f "skip=1 delims=" %%i in ('wmic path win32_videocontroller get caption') do (
set "gpu_info=!gpu_info! %%i"
set "gpu_info="
for /f "tokens=*" %%i in ('powershell -Command "Get-CimInstance Win32_VideoController | Select-Object -ExpandProperty Name -First 1"') do (
set "gpu_info=%%i"
)
echo.
@@ -779,8 +781,9 @@ echo 0. Cancel install
setlocal enabledelayedexpansion
chcp 65001 > nul
REM Get GPU information
for /f "skip=1 delims=" %%i in ('wmic path win32_videocontroller get caption') do (
set "gpu_info=!gpu_info! %%i"
set "gpu_info="
for /f "tokens=*" %%i in ('powershell -Command "Get-CimInstance Win32_VideoController | Select-Object -ExpandProperty Name -First 1"') do (
set "gpu_info=%%i"
)
echo.