PowerShell - unexpected output when converting number

I have a PowerShell script that finds the last run result of a scheduled task. In this instance, the last run result is -2147024895

Now, in the PowerShell Window, if I enter the command:-

"0x{0:x}" -f -2147024895

The output shows:-


which is EXACTLY as I want.

Likewise, if I assign the value to a variable called lastResult as such:-

$lastResult = -2147024895

and then enter:-

"0x{0:x}" -f $lastResult

again I get an output of 0x80070001

HOWEVER, I cannot get the same output if I use a script! I have a .ps1 script that I want to run as a scheduled task that will send me an email with this last run result.

I have the following code ($lastResult is already set earlier on)

$lastResult = "0x{0:x}" -f $lastResult
$emailbody = "Last run result was " + $lastResult

When the email arrives, it reads:-

Last run result was 0x-2147024895

So I tried the line:-

$lastResult = [convert]::ToString($lastResult, 16)

which resulted in an email which read:-

Last run result was FFFFFFFF80070001

All I want is the 80070001 (preferably without using Trim)
LVL 17
Chris MillardAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

This should work for you:
$emailbody = "Last run result was " + ( "0x{0:x}" -f $lastResult )
Chris MillardAuthor Commented:
Sadly not. I still get:-

Last run result was 0x-2147024895
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
$lastResult seems to be of type [String], and the format operator doesn't do anything with that. themrrobert's change won't help then either.
In general you should avoid mixing types in the same variable. $lastResult is thought to be an integer at one time, and a string another time.

Anyway, I would use
$emailbody = "Last run result was 0x$('{0:x}' -f [Int] $lastResult)"

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris MillardAuthor Commented:
Spot on  - thanks.
Hopefully you get it, my solution worked perfectly on my machine, so not sure why it wasn't working for you.

Good luck :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.