Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PowerShell convert Int to Hex and write info to text file

I’m working on PowerShell Module Library that we want to use in our environment to simplify some coding.
I’m having small module that read registry values, and I want to do the following
Module reads DWORD value, but it should show not only Decimal but also Hex value

Log "Operating System UBR: $(GetDWORDValue "HKLM" "SOFTWARE\Microsoft\Windows NT\CurrentVersion" "UBR" "64")"

Log "Operating System UBR (hex): ($(GetDWORDValue "HKLM" "SOFTWARE\Microsoft\Windows NT\CurrentVersion" "UBR" "64")).ToString('X')"

Output
[06/21/2024 14:15:34 – TemplateScript.ps1] Operating System UBR: 3737
[06/21/2024 14:15:34 – TemplateScript.ps1] Operating System UBR (hex): (3737).ToString(‘X’)
item in Bold is not correct and should show ‘E99’

I’ve tried various combinations:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  • Tried with separate variable which works is overkill in my opinion
  • using the {0} -f $ubrvalue.ToString(‘X’) does not work either when using the log module it does work if I create separate variable, and when trying from console I’m able to make it work also just not when trying to use the Log functionality.

Which is basically module that’s using Add-Content and as param the text that I’m passing.

Log module

function Log {
    param (
        [string]$sText
    )

    $logFolderPath = "C:\Logs"
    if ($sAppName -eq $null) {
          $sAppName = $scriptName
        }
    $logFilePath = Join-Path -Path $logFolderPath -ChildPath "$sAppName.log"

    if (-not (Test-Path -Path $logFolderPath)) {
        New-Item -Path $logFolderPath -ItemType Directory
    }

    $sCurTime = Get-Date -Format "MM/dd/yyyy HH:mm:ss"
    $logEntry = "[$sCurTime - $scriptName] $sText"

    Add-Content -Path $logFilePath -Value $logEntry
}

Any idea/suggestions are welcome
Thx!

>Solution :

You need to surround the whole (...).ToString('X') invocation in a subexpression $(...):

Log "Operating System UBR (hex): $((GetDWORDValue "HKLM" "SOFTWARE\Microsoft\Windows NT\CurrentVersion" "UBR" "64").ToString('X'))"

You can also use a single pipeline by having ForEach-Object invoke the ToString method, like so:

Log "Operating System UBR (hex): $(GetDWORDValue "HKLM" "SOFTWARE\Microsoft\Windows NT\CurrentVersion" "UBR" "64" |ForEach-Object ToString 'X')"

Makes it slightly more readable without the double parens 🙂

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading