如何从CMD运行PowerShell脚本,以便 *.ps1将十六进制值正确转换为十进制

发布于 2025-01-22 18:20:29 字数 1093 浏览 0 评论 0原文

我有简单的脚本来检测Regedit的GPU内存。

$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可以正常工作。在我的情况下,输出是:

GeForce GT 710
2048

但是,如果我从cmd拨打上述脚本,

powershell -file "D:\PS-script\gpi_id.ps1"

则输出

GeForce GT 710
N/A or DVMT

无论我是否以管理员的方式运行。我猜问题是,当从CMD PowerShell调用时,无法转换“硬件信息”。QWMemorySize(以十六进制格式存储)为十进制。

我可以从CMD运行PowerShell脚本,以便能够正确地将十六进制值转换为十进制吗?或否则解决问题。

I have simple script to detect GPU memory from regedit.

$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
}

It works fine in ISE. The output in my case is:

GeForce GT 710
2048

But if I call above script from cmd

powershell -file "D:\PS-script\gpi_id.ps1"

The output is

GeForce GT 710
N/A or DVMT

No matter if I run as Admin or not. I guess the problem is that when called from CMD powershell can not convert "HardwareInformation.qwMemorySize" (which stored in hex format) to decimal.

Can I run powershell script from CMD so that it would be capable to convert hex value to decimal properly? Or solve the problem otherwise.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜灵血窟げ 2025-01-29 18:20:29

对我感到羞耻。我的逻辑错误。如果在比较需要的变量之前进行陈述。下面适当的代码

$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
}

Shame on me. I had wrong logic. IF statment before the variable which it takes for comparison. Proper code below

$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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文