Powershell 属性?

发布于 2024-10-11 07:16:28 字数 391 浏览 2 评论 0原文

是否可以将一些元数据属性分配给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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

找个人就嫁了吧 2024-10-18 07:16:28

有趣的问题......据我所知,函数没有这样的属性。但我认为有一种半黑客的方式使用基于注释的帮助属性(也许根本不是黑客,但我不太确定)。

<#
.FUNCTIONALITY
    TEST1
#>
function Do-Something1
{}

<#
.FUNCTIONALITY
    TEST2
#>
function Do-Something2
{}

Get-ChildItem Function: | %{
    $fun = $_.Name
    try {
        Get-Help $fun -Functionality TEST* | %{
            switch($_.Functionality) {
                'TEST1' { "$fun is for test 1" }
                'TEST2' { "$fun is for test 2" }
            }
        }
    }
    catch {}
}

输出:

Do-Something1 is for test 1
Do-Something2 is for test 2

也许这种方法在某些情况下可能有用。

另请参阅帮助中的基于注释的帮助关键字部分:

man about_Comment_Based_Help

更新
虽然上面的答案被接受了,但我仍然不太满意。这是另一种绝对不是 hacky 的方法。它还有一个优点,请参阅评论。此方法使用带有常规名称的额外别名。

# Functions to be used in tests, with any names
function Do-Something1 { "Something1..." }
function Do-Something2 { "Something2..." }

# Aliases that define tests, conventional names are UnitTest-*.
# Note: one advantage is that an alias can be defined anywhere,
# right where a function is defined or somewhere else. The latter
# is suitable in scenarios when we cannot modify the source files
# (or just do not want to).
Set-Alias UnitTest-Do-Something1 Do-Something1
Set-Alias UnitTest-Do-Something2 Do-Something2

# Get UnitTest-* aliases and extract function names for tests.
Get-Alias UnitTest-* | Select-Object -ExpandProperty Definition

# Or we can just invoke aliases themselves.
Get-Alias UnitTest-* | % { & $_}

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).

<#
.FUNCTIONALITY
    TEST1
#>
function Do-Something1
{}

<#
.FUNCTIONALITY
    TEST2
#>
function Do-Something2
{}

Get-ChildItem Function: | %{
    $fun = $_.Name
    try {
        Get-Help $fun -Functionality TEST* | %{
            switch($_.Functionality) {
                'TEST1' { "$fun is for test 1" }
                'TEST2' { "$fun is for test 2" }
            }
        }
    }
    catch {}
}

Output:

Do-Something1 is for test 1
Do-Something2 is for test 2

Perhaps this approach might be useful in some scenarios.

See also the section COMMENT-BASED HELP KEYWORDS in help:

man about_Comment_Based_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.

# Functions to be used in tests, with any names
function Do-Something1 { "Something1..." }
function Do-Something2 { "Something2..." }

# Aliases that define tests, conventional names are UnitTest-*.
# Note: one advantage is that an alias can be defined anywhere,
# right where a function is defined or somewhere else. The latter
# is suitable in scenarios when we cannot modify the source files
# (or just do not want to).
Set-Alias UnitTest-Do-Something1 Do-Something1
Set-Alias UnitTest-Do-Something2 Do-Something2

# Get UnitTest-* aliases and extract function names for tests.
Get-Alias UnitTest-* | Select-Object -ExpandProperty Definition

# Or we can just invoke aliases themselves.
Get-Alias UnitTest-* | % { & $_}
摘星┃星的人 2024-10-18 07:16:28

组织和分组命令是 PowerShell 中持续存在的难题。这是始终需要管理的事情。不过,如果您勤奋的话,有一些关于命名 cmdlet 和函数的最佳实践是可行的。您可能已经注意到,所有 cmdlet 均采用动词-名词格式。 IE Get-ProcessSet-Item 等。很多人所做的就是在命令中添加第三部分,将名词分组在一起。例如,在 Active Directory 世界中,您没有 get-user,而是 get-aduser

您可以做的一件事(可能不是最漂亮的事情)是用您选择的 2 或 3 个字母序列来命名您的单元测试函数。假设您选择了一些令人难以置信的原创内容(例如 UT)来进行单元测试。然后你的函数将是

function Do-UTSomething { "Something was done" }

一旦你拥有了所有的 UT 函数,你可以使用 Get-Command cmdlet 来迭代它们,就像这样

Get-Command *UT* -commandtype function

另外,如果你更进一步并将它们打包到一个模块中,你可以做得更好并按那个模块。

Get-Command -module MyUnitTest

您可以使用以下方式获取模块的各种信息

help about_modules

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 have get-user, but rather get-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

function Do-UTSomething { "Something was done" }

Once you have all your UT functions, you can use the Get-Command cmdlet to iterate through them like so

Get-Command *UT* -commandtype function

Also, if you go a bit further and package them into a module, you could do one better and sort by that module.

Get-Command -module MyUnitTest

You can get all kinds of information on modules by using

help about_modules
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文