Skip to content Skip to sidebar Skip to footer

Powershell Returns Negative Exit Code While Script Results Are Correct

I have made the following PowerShell script: Set-Location D:\folder1\folder2\folder3\folder4; Get-ChildItem | Rename-Item -NewName {$_.BaseName.insert(19,'loadtime') + (Get-Date -F

Solution 1:

As per the comments, the issue was caused by trying to insert into non-existing part of a string. This will raise an exception.

As a solution, make sure the indexed location exists, or just concatenate at the end. Like so,

$_.basename + 'loadtime' + (get-date -format hhMM) + $_.extension

The weird error code -196608 is actually a result of an error code represented as decimal (base 10) integer instead of hex value (base 16). Consider this:

[int]$i = -196608
$i.ToString('x')
fffd0000

What happens here is that the real error code is, in hex format, 0xFFFD0000. Because of Two's Compliment, large enough hex values actually represent negative decimal numbers.

As for this particular error code, it pops up every here and there without proper documentation. Should I hazard a guess, it has something to do with the fact that Powershell itself works fine, but the script it was told to run didn't.

Post a Comment for "Powershell Returns Negative Exit Code While Script Results Are Correct"