我正在使用一个 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
这是“按计算值排序”想法的另一种变体。这会在对它们进行排序之前将结尾数字转换为
[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
如果您感兴趣的是按字符串末尾的 int 排序,那么您可以使用
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