gpt4 book ai didi

powershell - 如何在另一个 powershell 脚本中包含另一个 powershell 脚本文件?

转载 作者:行者123 更新时间:2023-12-03 16:32:08 28 4
gpt4 key购买 nike

假设我有一个 powershell 脚本,它在我当前路径的相对路径中包含一个 Powershell 哈希。让我们称之为“name.ps1”,它包含:

$names = @{
"bob" = "bobby"
"john" = "jones"
"danial" = "davis"
}
等等...
现在,我有另一个名为“dosomething.ps1”的 powershell 脚本,它想将此文件“../db/names.ps1”导入当前的 powerscript 文件中,就像旧式 c 语言包含一样,如下所示:
#include "../db/names.ps1"

write-host ("BOB: {0}" -f $names.bob)
write-host ("JOHN: {0}" -f $names.john)
我怎样才能在powershell中完成这个?

最佳答案

这在 PowerShell 中称为“点源”,因为您只需使用“.Path/to/.ps1”即可运行该文件。通常,这些文件将包含您要在父脚本中使用的自定义函数。
在下面的示例中,“include”文件与正在执行的 PowerShell 脚本位于同一文件夹中。

. "$PSScriptRoot\MyCoolFunctionsLibrary.ps1"
只需将其放在对“包含”文件中的任何函数调用之前
注意:如果要从包含文件传递对象,请确保在包含文件中正确设置这些对象的范围。

关于powershell - 如何在另一个 powershell 脚本中包含另一个 powershell 脚本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65149351/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com