Run a test in PowerShell with $PSScriptRoot raises an error

174 Views Asked by At

I want to create a test with Pester. I have to create some files and then read them with a filter.

The problem I get is when I use $PSScriptRoot. If I run the test in the Windows PowerShell ISE, it is working.

enter image description here

The same test in a PowerShell using

Invoke-Pester -Output Detailed .\MyTest.Tests.ps1

returns an error:

ParameterBindingValidationException: Cannot bind argument to parameter 'Path' because it is null.

The $path is defined $targetPath = $PSScriptRoot. I tried to use $psISE like in this post with the same result. Also I noticed that $psISE returns the path of Pester and not the local path (in the screenshot below C:\Program Files\WindowsPowerShell\Modules\Pester\5.4.1).

enter image description here

How can I fix the script to run it with Windows PowerShell ISE, PowerShell and also in an Azure pipeline?

0

There are 0 best solutions below