All of our servers are getting their Disk allocations increased. I have no desire to type:
Select disk 6
Select Partition 1
Extend
Select disk 7
Select Partitio
Since diskpart
reads commands from STDIN you could do something like this:
'list disk' | diskpart | Where-Object {
$_ -match 'disk (\d+)\s+online\s+\d+ .?b\s+\d+ [gm]b'
} | ForEach-Object {
$disk = $matches[1]
"select disk $disk", "list partition" | diskpart | Where-Object {
$_ -match 'partition (\d+)'
} | ForEach-Object { $matches[1] } | ForEach-Object {
"select disk $disk", "select partition $_", "extend" | diskpart | Out-Null
}
}
The first regular expression selects only disks that have free space in the MB or GB range ([gm]b
). Adjust as required.
Wrap the diskpart
calls into functions to make them a little more "digestible":
function List-Disks {
'list disk' | diskpart |
Where-Object { $_ -match 'disk (\d+)\s+online\s+\d+ .?b\s+\d+ [gm]b' } |
ForEach-Object { $matches[1] }
}
function List-Partitions($disk) {
"select disk $disk", "list partition" | diskpart |
Where-Object { $_ -match 'partition (\d+)' } |
ForEach-Object { $matches[1] }
}
function Extend-Partition($disk, $part) {
"select disk $disk","select partition $part","extend" | diskpart | Out-Null
}
List-Disks | ForEach-Object {
$disk = $_
List-Partitions $disk | ForEach-Object {
Extend-Partition $disk $_
}
}