简体   繁体   English

如何在PowerShell中删除点源脚本?

[英]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-afunc-bfunc-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.

相关问题 我是否可以使在导入模块内点源到父脚本的函数成为必需? - Can I make a function acessable that was dot-sourced within an imported-module to the parent script? 如何在PowerShell中删除所有导入的模块? - How can I remove all imported modules in PowerShell? 如何验证Powershell脚本中的密钥? - How can I verify my keys in my powershell script? 如何在ToString()中转义点 - How can I escape a dot in ToString() 如何调用需要PowerShell脚本需要双引号参数的控制台应用程序? - How can I call a console app requiring a parameter that needs double-quotes from PowerShell script? 如何修复我的Powershell脚本,该脚本读取Assembly属性但留下打开的文件句柄? - How can I fix my Powershell script which reads Assembly attributes but leaves an open file handle? 如何使用 PowerShell 脚本卸载旧版本的 .NET SDK 和运行时? - How can I uninstall older versions of .NET SDKs and runtimes with a PowerShell script? 如何强制Powershell脚本等待重写文件,直到另一个进程完成读取它为止? - How can I force a Powershell script to wait overwriting a file until another process is finished reading it? 如何将带有点表示法和 arrays 的字符串转换为 json? - How can I convert a string with dot notation and arrays to json? 如何使用DOT文件导出数据? - How can I use DOT files to export data's?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM