我正在使用Powershell脚本来自动执行一些长期运行的部署任务。
我正在使用
$scripts = Get-ChildItem -r -Path server-deploy | select -expand fullname
得到一个看起来像这样的数组
D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-builder-10.ps1
D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-builder-20.ps1
D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-builder-30.ps1
D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-builder-40.ps1
D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-builder-50.ps1
如果需求发生变化,我在其中留下了一些“漏洞”以添加额外的脚本。这些脚本将始终被命名为“ install-someword-int.ps1”,并且顺序由int确定。
我知道Get-ChildItem已经按照正确的顺序对它们进行了排序,但是我希望能够创建一个始终可以对它们进行排序的函数,因此如果我需要添加脚本(例如install-builder-15) .ps1,我知道它将在10点之后和20点之前运行。作为一个好的托管人,我并不固有地相信Get-ChildItem总是以正确的顺序返回它们。
我正在寻找对$ scripts进行排序的正确方法,或者使用某种for循环来迭代数组,但始终以正确的顺序进行。
编辑:@boxdog 使用文件:
install-builder-10.ps1
install-builder-20.ps1
install-builder-30.ps1
install-builder-4.ps1
install-builder-5.ps1
如果我运行Sort-Object @{e={$_ -match ".*install-.*-(?<number>\d+).ps1"; $matches.number}}
,我会看到它们的顺序相同。我希望
install-builder-4.ps1
install-builder-5.ps1
install-builder-10.ps1
install-builder-20.ps1
install-builder-30.ps1
答案 0 :(得分:3)
如果您只想对字符串末尾的整数进行排序,则可以使用Sort-Object
对其进行排序,如下所示:
$paths = "D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-builder-30.ps1",
"D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-sortofbuilder-20.ps1",
"D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-builder-10.ps1",
"D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-builder-20.ps1",
"D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-builder-50.ps1",
"D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-notbuilder-10.ps1",
"D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-builder-15.ps1"
$paths | Sort-Object @{e={$_ -match ".*install-.*-(?<number>\d+).ps1" | Out-Null; [int]$matches.number}}
哪个输出如下:
D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-builder-10.ps1
D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-notbuilder-10.ps1
D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-builder-15.ps1
D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-sortofbuilder-20.ps1
D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-builder-20.ps1
D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-builder-30.ps1
D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-builder-50.ps1
答案 1 :(得分:3)
这是“按计算值排序”概念的另一个变体。这会将结尾数字转换为[int]
,然后再对它们进行排序。 [咧嘴]
$FakeFileList = @(
[System.IO.FileInfo]'install-builder-10.ps1'
[System.IO.FileInfo]'install-builder-20.ps1'
[System.IO.FileInfo]'install-builder-30.ps1'
[System.IO.FileInfo]'install-builder-4.ps1'
[System.IO.FileInfo]'install-builder-5.ps1'
)
$FakeFileList |
Sort-Object {[int]$_.BaseName.Split('-')[-1]}
输出...
Mode LastWriteTime Length Name
---- ------------- ------ ----
darhsl 1600-12-31 6:00 PM install-builder-4.ps1
darhsl 1600-12-31 6:00 PM install-builder-5.ps1
darhsl 1600-12-31 6:00 PM install-builder-10.ps1
darhsl 1600-12-31 6:00 PM install-builder-20.ps1
darhsl 1600-12-31 6:00 PM install-builder-30.ps1