我有一個管道,用于從其他設備型號的 excel 電子表格中獲取資訊,但對于此設備型號,該值是十六進制,而十六進制是不尋常的,因為 0x10 = 0x00010,所以我需要比較這些值在管線中。
這是我在回傳電子表格內容后用于非十六進制值的管道:
$deviceErrDescMap = Process_ErrorDescMap -errorCodeListFilePath $errorCodeListFile #excel is returned
$deviceErrDescRow = $deviceErrDescMap | Where-Object 'Value' -eq $sdkNum
在此,$deviceErrDescMap 保存如下電子表格值:
Name Value Description
A 0x00000010 Check Material A
B 0x00000100 Check Interlock
C 0x00000020 Check Material C
這就是我獲取 excel 內容的方式,以防萬一:
Function Process_ErrorDescMap{
[cmdletbinding()]
Param ([string]$errorCodeListFilePath)
Process
{
if(Test-Path $errorCodeListFilePath)
{
#Excel method
#Install-Module -Name ImportExcel -Scope CurrentUser -Force (dependency - 1. Close all instances of PowerShell (console, ISE, VSCode), 2. Move the PackageManagement folder from OneDrive, 3. Open a PowerShell Console (Run As Administrator), 4. Run Install-Module ImportExcel)
if(($errorCodeListFilePath -match "DeviceA") -or ($errorCodeListFilePath -match "DeviceB"))
{
$startRow = 1
}
else
{
$startRow = 2
}
$importedExcel = Import-Excel -Path $errorCodeListFilePath -StartRow $startRow
return $importedExcel #list of error desc
}
else {
Write-Host "Invalid path given: $($errorCodeListFilePath)"
return "***Invalid Path $($errorCodeListFilePath)"
}
} #end Process
}# End of Function Process_ErrorDescMap
The spreadsheet's first line, with Value 0x00000010 should compare with $sdkNum=0x10, which is the first one. So 0x10 (or 0x010) needs to match, or be equal to this spreadsheet value of 0x0000010 and grab it from the map. I'm at a bit of a loss as to how to accomplish this. I'm not sure how to convert 'Value' to hex, and compare it with the hex value of $sdkNum in this pipeline. I was thinking of using a regex to get the 10 from $sdkNum, and use match to get any rows containing 10 from the spreadsheet content, and then further compare. I feel like there's an easier way, plus I'm not sure how I'd get just the non-zero number and 0's to the right of that out of the hex string.
如果您對這個十六進制比較感到困惑,請隨意使用十六進制到十進制轉換網頁,您會看到 0x10 = 0x000010。我也覺得很奇怪。重要的是1之后的0。
這適用于 PowerShell 5.1 和 VSCode。
uj5u.com熱心網友回復:
當您將字串轉換為整數型別時,PowerShell 將本機決議有效的十六進制數字:
PS ~> [int]'0x10'
16
由于 PowerShell 的所有多載比較運算子 ( -eq/ -ne/ -gt/ -ge/ -lt/ -le) 都會自動將右側運算元轉換為左側運算元的型別,因此您需要做的就是確保作為第一個運算元提供的運算式是已經是[int]:
$sdkNum = 0x10 # notice no quotation marks
# Option 1: cast the hex string to `[int]` explicitly
... |Where-Object { [int]$_.Value -eq $sdkNum }
# Option 2: $sdkNum is already an [int], PowerShell automatically converts hex string to int
... |Where-Object { $sdkNum -eq $_.Value }
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/437604.html
