I am working with a powershell script that automates some long running deployment tasks.
I'm using
$scripts = Get-ChildItem -r -Path server-deploy | select -expand fullname
to get an array that looks like this
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
I've left some 'holes' in there to add extra scripts if the requirements change on me. The scripts will always be named "install-someword-int.ps1" and ordering is determined by the int.
I know that Get-ChildItem has sorted them in the correct order, but i'd like to be able to create a function that can always order them so if I need to add a script for instance, install-builder-15.ps1, I know it will run after 10 and before 20. Being a good custodian, I don't inherently trust Get-ChildItem to ALWAYS return them in the correct order.
I'm looking for the proper way to sort $scripts, or use a for loop of some kind to iterate the array, but always in the correct order.
EDIT: @boxdog Using the files:
install-builder-10.ps1
install-builder-20.ps1
install-builder-30.ps1
install-builder-4.ps1
install-builder-5.ps1
if I run Sort-Object @{e={$_ -match ".*install-.*-(?<number>\d+).ps1"; $matches.number}} I see they are in the same order. I would expect
install-builder-4.ps1
install-builder-5.ps1
install-builder-10.ps1
install-builder-20.ps1
install-builder-30.ps1
