对模块中的函数进行排序

时间:2016-04-26 18:33:30

标签: powershell

我有一个大约有50个函数的powershell模块,但它们没有按字母顺序排序:

function CountUsers
{
    code
}

function TestAccess
{
    code
}

function PingServer
{
    code
}

我想按字母顺序对它们进行排序,例如:

function CountUsers
{
    code
}

function PingServer
{
    code
}

function TestAccess
{
    code
}

我找不到办法做到这一点,任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

您可以执行此操作using a regex,其中捕获整个功能名称的功能: (?s)(function (.*?){[^}]*})现在您可以使用名称对捕获进行排序并打印整个函数:

$x = @'
function CountUsers
{
    code
}

function TestAccess
{
    code
}

function PingServer
{
    code
}

'@

$regex = '(?s)(function (.*?){[^}]*})'    
[regex]::Matches($x, $regex) | sort { $_.Groups[2].Value } | % { $_.Groups[0].Value }

输出

function CountUsers
{
    code
}
function PingServer
{
    code
}
function TestAccess
{
    code
}