繁体   English   中英

Dot采购不以.ps1结尾的PowerShell脚本

[英]Dot sourcing a PowerShell script not ending with .ps1

从PowerShell程序,我可以“点源”另一个PowerShell程序。 即我可以执行它,就像它写在第一个内部一样。
例:

Write-Host 'before'
. MyOtherProgram.ps1
Write-Host 'after'

主程序中“包含”的MyOtherProgram,就像其内容已被复制/粘贴一样。

问题是:我只能点源一个用.ps1完成的文件名
我不能用MyOtherProgram.libMyOtherProgram.whatever

任何人都有一种方法来点源PowerShell脚本而不是以.ps1结尾?

另一种方法是使用Invoke-Expression

$code = Get-Content ./MyOtherProgram.lib | Out-String
Invoke-Expression $code

我不知道这是否编译成PowerShell或者它是否可配置但是一种方法是让你的脚本暂时重命名它,导入它然后重命名它。

Rename-Item C:\Path\MyScript.whatever C:\Path\MyScript.ps1
. C:\Path\MyScript.ps1
Rename-Item C:\Path\MyScript.ps1 C:\Path\MyScript.whatever

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM