Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在另一个powershell脚本中包含另一个powershell脚本文件?_Powershell - Fatal编程技术网

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

如何在另一个powershell脚本中包含另一个powershell脚本文件?,powershell,Powershell,假设我有一个powershell脚本,它在我当前路径的相对路径上包含一个powershell哈希。我们将其命名为“name.ps1”,它包含: $names = @{ "bob" = "bobby" "john" = "jones" "danial" = "davis" } 等等 现在,我有另一个名为“dosomething.ps1”的powershell脚

假设我有一个powershell脚本,它在我当前路径的相对路径上包含一个powershell哈希。我们将其命名为“name.ps1”,它包含:

$names = @{
   "bob" = "bobby"
   "john" = "jones"
   "danial" = "davis"
}
等等

现在,我有另一个名为“dosomething.ps1”的powershell脚本,它希望将此文件“./db/names.ps1”导入到当前的powerscript文件中,就像老式的c语言include一样,如下所示:

#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"
只需将其放在对“include”文件中的函数的任何调用之前


注意:如果要从包含文件传递对象,请确保在该包含文件中正确设置这些对象的范围。

使用点源:
/db/names.ps1