Powershell 属性?
是否可以将一些元数据属性分配给powershell宏?我有一组宏,我想将它们封装到逻辑组中。我想象这样的事情:
[UnitTest]
function Do-Something()
{
...
}
然后在运行时传递所有加载的宏并过滤掉它们,例如:
$macrosInRuntime = Get-Item function:
$unitTestMacros = $macrosInRuntime |
? {$_.Attributes -contain "UnitTest"} # <-- ???
foreach ($macro in $unitTestMacros)
{
....
}
我将不胜感激任何帮助
is it possible to assign some metadata Attributes to powershell macros? I have the set of macros and I want to encapsulate them to logical groups. I imagine something like:
[UnitTest]
function Do-Something()
{
...
}
and then pass all loaded macros in runtime and filter them out like:
$macrosInRuntime = Get-Item function:
$unitTestMacros = $macrosInRuntime |
? {$_.Attributes -contain "UnitTest"} # <-- ???
foreach ($macro in $unitTestMacros)
{
....
}
I will be greatful for any help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有趣的问题......据我所知,函数没有这样的属性。但我认为有一种半黑客的方式使用基于注释的帮助属性(也许根本不是黑客,但我不太确定)。
输出:
也许这种方法在某些情况下可能有用。
另请参阅帮助中的基于注释的帮助关键字部分:
更新
虽然上面的答案被接受了,但我仍然不太满意。这是另一种绝对不是 hacky 的方法。它还有一个优点,请参阅评论。此方法使用带有常规名称的额外别名。
Interesting question... There are no such attributes of functions, AFAIK. But I think there is a half-hacky way that uses comment-based help attributes (perhaps not even hacky at all but I am not quite sure).
Output:
Perhaps this approach might be useful in some scenarios.
See also the section COMMENT-BASED HELP KEYWORDS in help:
UPDATE
Though the answer above is accepted, I am still not quite happy with it. Here is yet another approach that is definitely not hacky. It also has an advantage, see the comments. This approach uses extra aliases with conventional names.
组织和分组命令是 PowerShell 中持续存在的难题。这是始终需要管理的事情。不过,如果您勤奋的话,有一些关于命名 cmdlet 和函数的最佳实践是可行的。您可能已经注意到,所有 cmdlet 均采用动词-名词格式。 IE
Get-Process
、Set-Item
等。很多人所做的就是在命令中添加第三部分,将名词分组在一起。例如,在 Active Directory 世界中,您没有get-user
,而是get-aduser
。您可以做的一件事(可能不是最漂亮的事情)是用您选择的 2 或 3 个字母序列来命名您的单元测试函数。假设您选择了一些令人难以置信的原创内容(例如 UT)来进行单元测试。然后你的函数将是
一旦你拥有了所有的 UT 函数,你可以使用 Get-Command cmdlet 来迭代它们,就像这样
另外,如果你更进一步并将它们打包到一个模块中,你可以做得更好并按那个模块。
您可以使用以下方式获取模块的各种信息
Organizing and grouping commands is an ongoing dilemma in PowerShell. It is something that will always have to be managed. However, there are some best practices around naming cmdlets and functions that can work if you are diligent. You probably have noticed that all cmdlets are in the verb-noun format. IE
Get-Process
,Set-Item
, etc. What a lot of people do is add a third part to the command which groups the nouns together. For example, in the world of Active Directory, you don't haveget-user
, but ratherget-aduser
.One thing you could do, and it may not be the prettiest thing, is to name your unit test functions with some 2 or 3 letter sequence of your choosing. Let's say you chose something incredibly original like UT for unit test. Your function would then be
Once you have all your UT functions, you can use the Get-Command cmdlet to iterate through them like so
Also, if you go a bit further and package them into a module, you could do one better and sort by that module.
You can get all kinds of information on modules by using