在 windows 10 上使用 powershell v7 调用 Powershell 函数导致缺少函数错误

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

我写了一个脚本来构建文件夹中的所有 .net 项目。

问题

问题是我在调用 Build-Sollution 时遇到函数丢失错误。

我试过的

我确保该函数在我使用它之前已声明,所以我不太确定为什么它说它未定义。

我是 powershell 的新手,但我认为调用另一个函数的函数应该像这样工作?

提前致谢!

请参阅下面的错误消息和代码。

错误信息

线 | 3 |构建解决方案 $_ | ~~~~~~~~~~~~~~~ 术语 'Build-Sollution' 未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。 检查名称的拼写,或者如果包含路径,请验证路径是否正确并重试。 构建解决方案:

代码

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

 $plugin_path = 'path/to/sollutions/'

function Get-Sollutions {

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

}

function Build-Sollution($solution) {

  dotnet build $solution.fullname

}

function Build-Sollutions($solutions) {

  $solutions | ForEach-Object -Parallel {

    Build-Sollution $_

  }

}


$solutions_temp = Get-Sollutions
Build-Sollutions $solutions_temp

至于这个...

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

... 在将代码加载到内存中之前,您无法使用这些函数。您需要 运行 代码才能使用这些功能。

如果您在 ISE 中或 VSCode,如果脚本未保存,Select 全部并使用 运行 键。在 ISE 中使用 F8 Selected,F5 运行 全部。在VSCode,F8运行选中,crtl+F5运行全部。您也可以单击菜单选项。

如果您从控制台主机执行此操作,运行 脚本使用点源。

. .\UncToYourScript.ps1

新人没关系,我们都是从某个地方开始的,但重要的是您首先要提高水平。因此,除了我在这里提到的内容之外,请务必花时间在 Youtube 上搜索初级、中级和高级 PowerShell 视频以供观看。网络上有大量的免费培训资源,使用内置的帮助文件也会给您答案。

about_Scripts

SCRIPT SCOPE AND DOT SOURCING Each script runs in its own scope. The functions, variables, aliases, and drives that are created in the script exist only in the script scope. You cannot access these items or their values in the scope in which the script runs.

To run a script in a different scope, you can specify a scope, such as Global or Local, or you can dot source the script.

The dot sourcing feature lets you run a script in the current scope instead of in the script scope. When you run a script that is dot sourced, the commands in the script run as though you had typed them at the command prompt. The functions, variables, aliases, and drives that the script creates are created in the scope in which you are working. After the script runs, you can use the created items and access their values in your session.

To dot source a script, type a dot (.) and a space before the script path.

另请参阅:

'powershell .net projects build run scripts'

'powershell build all .net projects in a folder'

Simple build script using Power Shell

更新

根据您的以下评论:

确定应该保存脚本,使用您选择的任何编辑器。

ISE 没有设计使用 PSv7,而是使用 WPSv5x 和更早版本。

PSv7 的编辑器是 VSCode。如果你 运行 一个包含另一个函数的函数,你已经明确地加载了那个调用中的所有东西,因此它是可用的。

但是,您是说,您使用的是 PSv7,因此,您需要 运行 PSv7 控制台主机或 VSCode 中的代码,而不是 ISE。

Windows PowerShell(powershell.exe 和 powershell_ise.exe)和 PowerShell Core(pwsh.exe)是两个不同的环境,具有两个不同的可执行文件,旨在 运行 并排 Windows,但您必须明确选择要使用的代码或编写代码以分支到代码段以相对于您启动的主机执行。

例如,假设我想 运行 一个控制台命令并且我在 ISE 中,但我需要 运行 在 Pwsh 中。我在通过我的 PowerShell 配置文件自动加载的自定义模块中使用了这样的函数:

# Call code by console executable
Function Start-ConsoleCommand
{
    [CmdletBinding(SupportsShouldProcess)]

    [Alias('scc')]

    Param  
    ( 
        [string]$ConsoleCommand,
        [switch]$PoSHCore
    )

    If ($PoSHCore)
    {Start-Process pwsh -ArgumentList "-NoExit","-Command  &{ $ConsoleCommand }" -PassThru -Wait}
    Else {Start-Process powershell -ArgumentList "-NoExit","-Command  &{ $ConsoleCommand }" -PassThru -Wait}
}

这段代码所做的就是接受我发送的任何命令,如果我使用 PoSHCore 开关...

scc -ConsoleCommand 'SomeCommand' -PoSHCore

...它将 shell 输出到 PSCore,运行 代码,否则,它只是 运行 来自 ISE>

如果您想将 ISE 与 PSv7 一起使用而不执行 shell 输出,您需要强制 ISE 使用 PSv7 到 运行 代码。参见:

Using PowerShell Core 6 and 7 in the Windows PowerShell ISE

来自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.

所以在这种情况下,最简单的解决方案是在Build-Sollutions

中定义Build-Sollution