0

I have a primary .ps1 script which executes a number of child .ps1 scripts. Various parameters are passed to each of these child scripts in order to perform a certain function.

For one of the child scripts, I pass in a folder path. The object of this child script is to loop through any .sql scripts in this specified path & execute it. Simple.

But somehow, this path is "chopped off" from the time it enters the script, to when it's called by the Get-ChildItem function.

Extract from the script:

param (
  [string]$MigrationDirectory
)

Write-Host $MigrationDirectory;

foreach ($Filename in Get-ChildItem -Path $MigrationDirectory -filter "*.sql") {
    $FirstLine = Get-Content $Filename -First 1;

When I output this $MigrationDirectory value, I get the following: C:\temp\NewFolder\Subfolder\1.4.4.71\Migrations\PreDeploy

But my script fails with this error: Get-Content : Cannot find path 'C:\temp\NewFolder\Subfolder\1.4.4.71\001_PreTest1.sql' because it does not exist.

For some reason, the "Migrations\PreDeploy" folder disappears from my path.

This executes fine when running on other servers. The only difference here is that this runs on Powershell Desktop (Windows) whereas on other servers, it will be Powershell 7. I have no idea if that will have any impact?

Changing the folder path does not have any impact. It continuously drops the last two folders from this path.

Hardcoding the path instead of using it in a variable, works fine.

zett42
  • 25,437
  • 3
  • 35
  • 72
  • 2
    `Get-Content $Filename -First 1` -> `Get-Content $Filename.FullName -First 1`. See the linked duplicate for details. – mklement0 Mar 01 '23 at 16:12

0 Answers0