我是powershell的新手,所以这可能是我不理解某些东西的原因。
我正在用powershell编写一个工具,并让该工具在c:\Users\Documents\WindowsPowerShell\Modules中工作。
我想要的是能够将工具复制到U盘,并从U盘运行它。
如果工具不在PSModulePath环境变量path中,我如何让它运行?
或者,如何将U盘添加到PSModulePath路径?
发布于 2020-01-17 01:52:06
如果你希望你的脚本是可移植的,你也必须随身携带依赖模块。但是,在脚本中,您可以确保将U盘上模块父目录的完整路径作为进程级环境变量添加到PSModulePath中:
$portableModuleDirectory = [System.IO.DirectoryInfo]'./relative/path/to/dependent/module'
$env:PSModulePath += ";$(Split-Path -Parent $portableModuleDirectory.FullName)"
Import-Module ModuleName然而,这是一个繁琐的工作。这里最好的解决方案是将你的包推送到host your own internal nuget feed,这样如果你不能上传到公共PowerShell Gallery上,你可以从你的网络将模块安装在你需要它的任何机器上。
发布于 2020-01-17 17:15:19
谢谢你的帮助。我有一个解决方案,它的工作方式是将以下内容放在.ps1脚本中,并使用该脚本导入相关模块并调用第一个函数。
#find local directory
$LocalDir = Split-Path $MyInvocation.MyCommand.Path -Parent
#prove correct directory found.
Write-Host "Starting module load: $($MyInvocation.MyCommand) in directory = $LocalDir"
#load modules
Import-Module $LocalDir/Module –Force
#call first function
firstfunction我不能让$PSScriptRoot工作,但Split-Path $MyInvocation.MyCommand.Path –Parent代码的优势在于它也可以在ISE中工作。然后,我将依赖模块放在.ps1脚本下面的目录中。
https://stackoverflow.com/questions/59766723
复制相似问题