Attach EBS volume to Windows EC2 with Powershell

I saw a lot of questions about adding EBS Linux volumes, but not Windows. Say you find that your drive is running in a small space (possibly through CloudWatch) and wants to add another EBS volume. Can this be done with Powershell?

I would prefer not to use diskpart.exe, since it’s more difficult to parse its results (not being Powershell’s own command).

+5
source share
1 answer

Hoping this helps someone. Everything was simple, but it took me a while to track everything that was needed for Windows.

This answer has been shortened for brevity, so be sure to:

  • you handled AWS Powershell API exceptions
  • your volumes are “accessible” before trying to connect them to EC2
  • volume shows "use" after attaching it.

2 and 3 can be done using the Get-EC2Volume .

Create an EBS volume:

 $volume = New-EC2Volume -Size $sizeInGB -AvailabilityZone $az -VolumeType $vType 

Connect the volume to EC2:

 Add-EC2Volume -InstanceId $toInstanceId -VolumeId $volume.Id -Device $devId -Region $region 

Windows side:

find the just added ebs volume

 $diskNumber = (Get-Disk | ? { ($_.OperationalStatus -eq "Offline") -and ($_."PartitionStyle" -eq "RAW") }).Number 

initialize disk

 Initialize-Disk -Number $diskNumber -PartitionStyle "MBR" 

create a max-space partition, assign a drive letter, make it "active"

 $part = New-Partition -DiskNumber $diskNumber -UseMaximumSize -IsActive -AssignDriveLetter 

format new drive

 Format-Volume -DriveLetter $part.DriveLetter -Confirm:$FALSE 

Enjoy it!

+8
source

Source: https://habr.com/ru/post/1262042/


All Articles