[英]How to run powershell script from CMD, so that *.PS1 would properly convert HEX values to decimal
我有簡單的腳本來從注冊表中檢測 GPU memory。
$mem_devid = get-itemproperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\Class\{4D36E968-E325-11CE-BFC1-08002BE10318}\????" -Name HardwareInformation.AdapterString, MatchingDeviceId, HardwareInformation.qwMemorySize
$GPU_Info = Get-CIMInstance Win32_VideoController
foreach ($i in $GPU_Info.PNPDeviceID) {
$gpu_id_tmp = $i -Split "&"
$gpu_id = $gpu_id_tmp[1]
$current_gpu = $mem_devid | where { $_ -match $gpu_id }
$gpu_name = $current_gpu."HardwareInformation.AdapterString"
IF ($gpu_mem -lt 1) {
$gpu_mem = "N/A or DVMT"
} ELSE {
$gpu_mem = $current_gpu."HardwareInformation.qwMemorySize"/1048576
}
Write-Host $gpu_name
Write-Host $gpu_mem
}
它在 ISE 中運行良好。 我的 output 是:
GeForce GT 710
2048
但是如果我從 cmd 調用上面的腳本
powershell -file "D:\PS-script\gpi_id.ps1"
output 是
GeForce GT 710
N/A or DVMT
不管我是否以管理員身份運行。 我想問題是當從 CMD 調用時 powershell 無法將“HardwareInformation.qwMemorySize”(以十六進制格式存儲)轉換為十進制。
我可以從 CMD 運行 powershell 腳本,以便它能夠正確地將十六進制值轉換為十進制值嗎? 或者以其他方式解決問題。
我真丟人。 我有錯誤的邏輯。 用於比較的變量之前的 IF 語句。 下面的正確代碼
$mem_devid = get-itemproperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\Class\{4D36E968-E325-11CE-BFC1-08002BE10318}\????" -Name HardwareInformation.AdapterString, MatchingDeviceId, HardwareInformation.qwMemorySize
$GPU_Info = Get-CIMInstance Win32_VideoController
foreach ($i in $GPU_Info.PNPDeviceID) {
$gpu_id_tmp = $i -Split "&"
$gpu_id = $gpu_id_tmp[1]
$current_gpu = $mem_devid | where { $_ -match $gpu_id }
$gpu_name = $current_gpu."HardwareInformation.AdapterString"
$gpu_mem = $current_gpu."HardwareInformation.qwMemorySize"
IF ($gpu_mem -lt 1) {
$gpu_mem = "N/A or DVMT"
} ELSE {
$gpu_mem_fnl = $gpu_mem/1048576
}
Write-Host $gpu_name
Write-Host $gpu_mem_fnl
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.