[英]How can I remove a dot sourced script in PowerShell?
If I have dot sourced : 如果我有点源:
. "\foo-bar.ps1"
How can I see obtain the list of all dot sourced scripts and how can I remove "foo-bar/ps1" from the dot sourced scripts? 如何查看获取所有点源脚本的列表以及如何从点源脚本中删除“foo-bar / ps1”?
As far as I know, you can't remove a dot sourced script. 据我所知,您无法删除点源脚本。 That is why modules where introduced in PowerShell 2.0.
这就是PowerShell 2.0中引入的模块的原因。 See About_Modules
请参阅About_Modules
You can convert your "foo-bar.ps1" to a module. 您可以将“foo-bar.ps1”转换为模块。 A module can be imported (Import-Module) and removed (Remove-Module).
可以导入模块(Import-Module)并删除(Remove-Module)。
I agree with @JPBlanc that you cannot remove a dot-sourced script in general but depending on your own coding conventions you may be able to do this in practice . 我同意@JPBlanc一般不能删除点源脚本,但根据您自己的编码约定,您可以在实践中执行此操作 。
First a couple observations about why you cannot do this in general : 首先是几个关于为什么你不能这样做的观察:
(1) PowerShell has no notion of a dot-sourced script as a separate entity. (1)PowerShell没有将点源脚本作为单独的实体的概念。 Once you dot-source it, its contents becomes part of your current context, just as if you had manually typed each line at the prompt.
一旦你点源它,它的内容就成了你当前上下文的一部分,就像你在提示符下手动输入每一行一样。 Thus you cannot remove it because it does not actually exist:-)
因此,你无法删除它,因为它实际上并不存在:-)
(2) For the very same reason as (1) you cannot list dot-sourced scripts. (2)由于与(1)相同的原因,您无法列出点源脚本。
Now, how to do it anyway: 现在,无论如何都要这样做:
Depending on the contents and conventions of your dot-sourced script, though, you may be able to do what you want. 但是,根据点源脚本的内容和约定,您可以执行所需的操作。 If, for example, the script simply defines three functions--call these
func-a
, func-b
, and func-c
--then you can both list and remove those functions. 例如,如果脚本只是定义了三个函数 - 调用这些
func-a
, func-b
和func-c
那么你可以 列出并删除这些函数。
List assimilated functions: 列出同化功能:
Get-ChildItem function:func-*
Remove assimilated functions: 删除同化功能:
Remove-Item function:func-*
You can remove sourced functions if you know the path of the dot-sourced file: 如果您知道点源文件的路径,则可以删除源函数:
Get-ChildItem function: |
Where-Object {$_.ScriptBlock.File -eq $path} |
Remove-Item
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.