2016-04-26 156 views
1

我有大约50功能的PowerShell的模块,但它们不是按字母顺序排序:排序功能

function CountUsers 
{ 
    code 
} 

function TestAccess 
{ 
    code 
} 

function PingServer 
{ 
    code 
} 

我谨对它们进行排序按字母顺序排列,如:

function CountUsers 
{ 
    code 
} 

function PingServer 
{ 
    code 
} 

function TestAccess 
{ 
    code 
} 

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

+1

你可以写一个脚本来解析每一个函数,然后按函数名称对它们进行排序并输出到文件。 – EBGreen

回答

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 
} 
+1

当使用{}嵌套代码块时,您的正则表达式示例不起作用。 –