7

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?

1
  • @vonPryz However... While the script when running it from a powershell window, I still get an error code in SSIS. Can I fix this by adding an exit code? so that it just always exits with a 0? Commented May 12, 2017 at 11:12

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.