powershell函数返回

标签 powershell

我尝试从另一个脚本启动一个脚本的函数。 我想将返回值保存到变量中,但这不起作用。

脚本1.ps1:

function test
{
  return "hallo"
}

脚本2.ps1:

./script1.ps1; $p=test

or
$p = ./script1.ps1; test

似乎$p为空,但我不知道出了什么问题。 有人可以帮我吗? 谢谢

最佳答案

试试这个:

. ./script1.ps1; $p=test

原因:您必须将函数加载到当前范围(即开头的句点 - 点源运算符)。

如果使用“;”,则全新的语句开始。所以从你的例子来看 $p = ./script.ps1;测试,将 script.ps1 的输出分配给 $p,然后运行该函数。

关于powershell函数返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3371405/

相关文章:

windows - 如何在 PowerShell 中提供包含变量值和引号的命令行参数?

powershell - 使用 get-childItem 进行 PSRemoting 性能开销

windows - 删除报表服务器(2014 native 模式)加密 key 和数据 [PowerShell]

powershell - Powershell并写入csv对象数组

string - 尝试替换文件中的字符串,但替换太多

windows - Powershell 为局部变量设置环境变量

powershell - Powershell配置文件 “on exit”事件?

powershell - 如何使用&运算符从PowerShell调用MSBuild?

.net - 如何在 PowerShell 中获取本地主机名称?

powershell - Azure AD V2 PowerShell - 在输出中排除属性