如何从CMD运行PowerShell脚本,以便 *.ps1将十六进制值正确转换为十进制
我有简单的脚本来检测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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对我感到羞耻。我的逻辑错误。如果在比较需要的变量之前进行陈述。下面适当的代码
Shame on me. I had wrong logic. IF statment before the variable which it takes for comparison. Proper code below