Extend volumes with free space using Powershell an

2020-04-21 04:17发布

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 Partition 1
Extend
.....

For 10 volumes per server, for 100 servers.....

Is there a way to have powershell scan the disk, looking for disks with free space greater than 100MB. Then have it extend the partition on said Disk?

Server running 2008R2

$psVersion Table 
Name                           Value
----                           -----
CLRVersion                     2.0.50727.5485
BuildVersion                   6.1.7601.17514
PSVersion                      2.0
WSManStackVersion              2.0
PSCompatibleVersions           {1.0, 2.0}
SerializationVersion           1.1.0.1
PSRemotingProtocolVersion      2.1

标签: powershell
3条回答
仙女界的扛把子
2楼-- · 2020-04-21 04:36

Same solution but more simple, and it works with other locales too:

function Extend-Partition($disk, $part)
{
  "select disk $disk","select partition $part","extend" | diskpart | Out-Null
}

$disks = ((wmic diskdrive get Index | Select-String "[0-9]+") -replace '\D','')

ForEach ($disk in $disks) {

    $parts = ((wmic partition where DiskIndex=$diskId get Index | Select-String "[0-9]+") -replace '\D','' | %{[int]$_ + 1})

    ForEach ($part in $parts) {
        Extend-Partition $disk $part
    }
}
查看更多
闹够了就滚
3楼-- · 2020-04-21 04:46

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 $_
    }
}
查看更多
一夜七次
4楼-- · 2020-04-21 04:51

For VMs inside vSphere/ESX

After expanding a VMDK in vSphere that expansion is not always immediately apparent to a Windows guest OS. Sometimes it never sees it at all until the following steps are taken.

Typically you have to open Disk Management and do a Refresh for the system to then see the additional space that has been added on. Only after that can you then Expand the drive in Windows.

The two scripts above worked fine if you had already gone in and done that refresh, but obviously the point of all this is to take as much manual labor out as possible. I tested and added onto the first script. Works exactly as needed for me.

Update-Disk -Number $matches[1] is the key line.

function List-Disks {
'list disk' | diskpart |
? { $_ -match 'disk (\d+)\s+online\s+\d+ .?b\s+\d+ [gm]b' } |
% { $matches[1] } 
Update-Disk -Number $matches[1]
}

function List-Partitions($disk) {
"select disk $disk", "list partition" | diskpart |
? { $_ -match 'partition (\d+)' } |
% { $matches[1] }
}

function Extend-Partition($disk, $part) {
"select disk $disk","select partition $part","extend" | diskpart | 
Out-Null
}

List-Disks | % {
$disk = $_
List-Partitions $disk | % {
Extend-Partition $disk $_
}
}

Sorry for the ugly formatting.

查看更多
登录 后发表回答