Powershell returns negative exit code while script results are correct

Ruben Jonkers picture Ruben Jonkers · May 12, 2017 · Viewed 8.9k times · Source

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 -Format HHmm) + $_.Extension};

The goal is to rename a file by adding loadtime at position 19 and a time stamp. While the results of running the scripts the following error message is returned:

Rename-Item : The input to the script block for parameter 'NewName' failed. 
Exception calling "Insert" with "2" argument(s): "Specified argument was out 
of the range of valid values.
Parameter name: startIndex"
At D:\folder1\folder2\folder3\folder4\folder5\RenameFile.ps1:2 
char:38
+ Get-ChildItem | Rename-Item -NewName {$_.BaseName.insert(19,'loadtime') + 
(Get-D ...
+                                      
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (Archive:PSObject) [Rename-
Item], ParameterBindingException
    + FullyQualifiedErrorId : 
ScriptBlockArgumentInvocationFailed,
Microsoft.PowerShell.Commands.RenameItemCommand

When I run the script from my SSIS package I get the following error:

[Execute Process Task] Error: In Executing "PowerShell.exe" "-F D:\folder1\folder2\folder3\folder4\folder5\exec RenameFile.ps1" at "", The process exit code was "-196608" while the expected was "0".

I have tried to search for this error message but I could not find a page talking about this specific exit code.

The problem is though that while running the script commands from a PowerShell window I would still get the error message but the filename does change as expected. However since an error is returned the Execute Process Task in SSIS will fail.

My question is, what is the cause of this error? I assume it has to do with:

{$_.BaseName.insert(19,'loadtime') + (Get-Date -Format HHmm) + $_.Extension};

Wherein $.BaseName.insert(19,'loadtime') and (Get-Date -Format HHmm) and + $.Extension are seen as separate arguments. My knowledge of PowerShell is not too great so this is just speculation on my part.

What would help me get rid of the error?

Thanks in advance!

Answer

vonPryz picture vonPryz · May 12, 2017

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.