2016-04-26 101 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

當使用{}嵌套代碼塊時,您的正則表達式示例不起作用。 –