powershellpowershell-7.0

Powershell function call causes missing function error using powershell v7 on windows 10


I wrote a script to build all .net projects in a folder.

Issue

The issue is I am getting a missing function error when I call Build-Solution.

What I tried

I made sure that function was declared before I used it so I am not really sure why it said that it is not defined.

I am new to powershell but I would think a function calling another functions should work like this?

Thanks in advance!

Please see below for the error message and code.

Error Message

Line |
3 | Build-Solution $_
| ~~~~~~~~~~~~~~~
The term 'Build-Solution' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Code for Build-Solution:

param (
    #[Parameter(Mandatory=$true)][string]$plugin_path,
    [string]$depth = 5
 )

 $plugin_path = 'path/to/solutions/'

function Get-Solutions {

  Get-ChildItem -File -Path $plugin_path -Include *.sln -Recurse  

}

function Build-Solution($solution) {

  dotnet build $solution.fullname

}

function Build-Solutions($solutions) {

  $solutions | ForEach-Object -Parallel {

    Build-Solution $_

  }

}


$solutions_temp = Get-Solutions
Build-Solutions $solutions_temp


Solution

  • From PowerShell ForEach-Object Parallel Feature | PowerShell

    Script blocks run in a context called a PowerShell runspace. The runspace context contains all of the defined variables, functions and loaded modules.

    ...

    And each runspace must load whatever module is needed and have any variable be explicitly passed in from the calling script.

    So in this case, the easiest solution is to define Build-Solution inside Build-Solutions