从命令行使用 -NoProfile 自动添加到模块路径

Automaticall adding to the module path using -NoProfile from command line

我的模块不能存在于我们服务器上的标准模块目录中。他们必须住在 E: 驱动器上。

为了开发,我可以修改我的配置文件来修改 $ENV:PSModulePath 变量。但是当我安排一个脚本并从命令行调用 PowerShell 时,我们被指示使用 -NOProfile 开关。一个典型的例子是:

 New-ScheduledTaskAction -Execute 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe' -Argument "-windowstyle hidden -NonInteractive -NoLogo -NoProfile -File `"<Path to Script>`""

如何从命令行修改模块路径,以便我的脚本可以找到它们的模块,而无需在每个脚本中指定该路径?

您可以停止使用 -NoProfile 开关,因为在这种情况下您想要使用它。

您可以使用 -Command 开关而不是 -File(然后点源脚本):

powershell.exe -windowstyle hidden -NonInteractive -NoLogo -NoProfile -Command "$env:PSModulePath = (($env:PSModulePath -split ';') + 'E:\Your\Module\Path') -join ';' ; . '<Path to Script>'