Powershell 5: An error occurred while creating the pipeline
Today I was working with a Server 2016 TP5 server over PSR and I ran into this issue with one of my scripts:
Do-Something
An error occurred while creating the pipeline.
+ CategoryInfo : NotSpecified: (:) [],
RuntimeException
+ FullyQualifiedErrorId : RuntimeException
This script has been working fine on other systems so far so I was a bit confused. Google didn’t bring me much results so I decided to run the script locally on my Windows 10 machine and ended up with this:
Do-Something
Collection is read-only.
At line:1 char:1
+ Do-Something
The weird thing is that this script has been running fine on all other versions of Windows. I tried to run it on Windows 10 with Powershell version 2 and it worked fine as well.
After breaking down code I found the issue. Below is a simplified version of my function
function Do-Something
{
Param
(
[Alias("In")]
[Alias("I")]
[Parameter()]
$Input
)
return $Input
}
The alias definition above is a wrong example of defining an alias. Fortunately lower Powershell versions have been ‘forgiving’ and this command has been running fine due to that. The correct way to define an alias is detailed here by Microsoft. An example is provided below
function Do-Something
{
Param
(
[Alias("In","I")]
[Parameter()]
$Input
)
return $Input
}
Bonus
As a bonus here is an example on how to find your scripts that have the same issue
Get-ChildItem $Home\PowershellTools -Recurse |
select-string -pattern '(?smi)alias\S*\s*alias'