PowerShell:确保函数名称唯一性的最佳方法? [英] PowerShell: best way to ensure function name uniqueness?

查看:28
本文介绍了PowerShell:确保函数名称唯一性的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确保 PowerShell 函数名称唯一的最佳方法是什么?从版本 1 开始的标准是在动词破折号之后和名词之前放置一个简短的唯一 id.例如,使用我的姓名首字母,我可以创建函数 Get-DWServer;这很好,直到有人在不同的模块中创建一个函数来获取对数据仓库的对象引用并使用相同的函数名称.两个或三个字母是不够的,但更多的字母读起来很难看.

What's the best way to ensure your PowerShell function name is unique? The standard since version 1 is to put in a short unique id after the verb dash and before the noun. For example, with my initials I could create function Get-DWServer; this is fine until someone creates a function in a different module for getting an object reference to a datawarehouse and uses the same function name. Two or three letters just isn't sufficient but more than that gets ugly to read.

我更喜欢有一个类似于 .NET 命名空间的唯一前缀*.它更有利于组织,更容易在眼睛上工作,并且可以使用选项卡完成.它可以优雅地扩展,因此您可以将其命名为 DW.Get-Server 或 DW.Network.Get-Server.

I'd prefer to have a unique prefix* similar to .NET namespaces. It's better for organization, easier on the eye and works with tab completion. And it expands gracefully so you could name it DW.Get-Server or DW.Network.Get-Server.

这样做的缺点是它在模块导入/导出模块成员期间与 PowerShell 的正确动词检查相冲突.您可以通过在导入期间指定 DisableNameChecking 来解决这个问题,但我想知道这样做是否草率,如果 PowerShell 3 提供更好的解决方案可能会很糟糕.我知道 PS 动词纯粹主义者(有没有?)会抱怨这可能阻碍发现",但我想不出更好的选择.

The downside of doing this is it runs afoul of PowerShell's proper verb check during module import/Export-ModuleMember. You can get around this by specifying DisableNameChecking during import but I'm wondering if doing this is sloppy and might be bad if PowerShell 3 comes out with a better solution. I know PS verb purists (are there any?) will complain that this probably 'hinders discovery' but I can't think of a better option.

你在做什么?

(*您可以使用 module_name\function_name 表示法引用导出的函数,但这不适用于 Tab 补全,并且仍然无法解决函数名称唯一的问题).

(*You can refer to an exported function using module_name\function_name notation but this won't work with tab completion and still doesn't get around the problem of the function name being unique).

推荐答案

我曾多次听到 Jeffrey Snover(PowerShell 的发明者)谈到这个问题,他将其描述为一个两难问题,而不是问题.困境必须得到管理,但不能完全解决.一个问题是可以解决的.作为 PS 动词纯粹主义者",我想说最好的方法是在名词前加上 2 或 3 个字母的前缀.到目前为止,这对于许多广泛分布的 cmdlet 集来说已经足够了.IE、Quest AD Cmdlets 与 Microsoft 的 AD Cmdlets.Get-ADUser 和 get-qaduser.

I have heard Jeffrey Snover (the inventor of PowerShell) talk about this a few times and he described it as a dilemma, not a problem. A dilemma has to be managed but can't be solved completely. A problem can be solved. As a PS verb "purist" I would say the best way to manage this is to have a 2 or 3 letter prefix to your nouns. This has been sufficient so far for many widely distributed sets of cmdlets. IE, Quest AD Cmdlets vs Microsoft's AD Cmdlets. Get-ADUser and get-qaduser.

如果你正在使用一个模块并且想使用你自己的前缀,你可以用

If you are consuming a module and want to use your own prefix, you can specify one with

import-module mymodule -Prefix myPrefix

我知道这不是唯一的灵丹妙药,但我认为它适用于 95% 的情况.

I know this isn't the one single silver bullet answer, but I would say it works for 95% of the situations.

这篇关于PowerShell:确保函数名称唯一性的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆