Ste*_*ler 5 windows powershell
我有一个 Powershell 脚本,我想将其设为“公开”,这意味着我希望能够从任何文件夹执行该脚本,就像从命令提示符执行一样。
这可能吗?
小智 6
您还可以在本地 powershell 配置文件中添加别名
设置别名 hello C:\scriptlocation\script.ps1
现在,只要您输入 hello,script.ps1 就会运行。
有关可以保存别名的各种配置文件的更多信息。 https://devblogs.microsoft.com/scripting/understanding-the-six-powershell-profiles/
您可以探索如何使用 powershell 配置文件来实现此目的。请参阅: https: //learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles。
如果脚本只是一个函数或一些变量,您可以将内容复制并粘贴到您的配置文件中。
或者,如果脚本代表您想要保持独立的独立代码单元,您可以将其导入到您的主配置文件中,如下所示:
get-content -path C:\blahblahblah\scriptName.ps1 -raw | invoke-expression
Run Code Online (Sandbox Code Playgroud)
最后,如果您正在编写“高级”powershell 函数,或者尝试正式执行操作,您可以研究使用 powershell 模块作为导出函数的方式。请参阅:https ://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_modules
@Lee_Dailey 将脚本添加到路径的答案也是可行的。如果您想添加大量脚本,一种方法是将C:/PowershellScripts/, 之类的文件夹添加到您的路径中,将脚本保存在那里,然后您就可以从任何地方调用 .PS1 文件。
| 归档时间: |
|
| 查看次数: |
3271 次 |
| 最近记录: |