function - 如何使公共(public)功能

标签 function powershell import

我想在文件 functions.ps1 中定义函数,然后从另一个脚本调用它。像这样:

函数.ps1:

Function Hi()
{
"hi"
}

从另一个脚本 (Call.ps1) 调用它。

调用.ps1:

invoke-expression -Command .\functions.ps1
Hi 

但是函数是在脚本 functions.ps1 的局部范围内定义的,我得到错误:

The term 'hi' 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, v erify that the path is correct and try
again.

有没有简单的方法可以解决这个问题?

最佳答案

您必须dotsource您的脚本才能将其加载到您当前的运行空间:

. .\functions.ps1
Hi

关于function - 如何使公共(public)功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44042129/

相关文章:

c++ - 使用父类函数作为回调订阅主题

visual-studio - Visual Studio : Is it possible to define custom functions for use in one's own Code Snippets?

azure - 如何获取在 Azure 中使用基本 SKU 的资源列表?

java - Android DownloadManager 不知道文件下载到哪里

css - 即使来源正确,@font-face 也不起作用

excel - 提取然后将 Csv.gz 文件导入 Excel/SPSS...识别文本限定符有问题吗?

c - 我如何从C调用以self作为参数的lua函数?

php - @mysqli_select_db -- 访问被拒绝

powershell - AzureAD powershell 新服务主体

deployment - 如何从 psake 执行 .bat 文件?