Skip to content

Instantly share code, notes, and snippets.

@awsvpc
Forked from maxautomation/InitializeEc2Disk.ps1
Created April 26, 2024 00:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save awsvpc/b8ae9e51c6c5c92ee0010d9f33fe243d to your computer and use it in GitHub Desktop.
Save awsvpc/b8ae9e51c6c5c92ee0010d9f33fe243d to your computer and use it in GitHub Desktop.
Function Initialize-EC2Disk {
param(
[parameter (mandatory=$true)][string] $InstanceId
)
$Commands = @(
'Get-Disk | `
Where partitionstyle -eq "raw" | `
Initialize-Disk -PartitionStyle MBR -PassThru | `
New-Partition -AssignDriveLetter -UseMaximumSize | `
Format-Volume -FileSystem NTFS -Confirm:$false -force'
)
$Parameter = @{
commands = $Commands
}
$Document = 'AWS-RunPowerShellScript'
Write-Host ""
Write-Host "Initializing disk..." -ForegroundColor Green
Try {
$Cmd = Send-SSMCommand -DocumentName $Document -Parameter $Parameter -InstanceId $InstanceId
While ($Cmd.Status -ne 'Success')
{
$Cmd = Get-SSMCommand -CommandId $Cmd.CommandId
Start-Sleep 10
}
Write-Host ""
Write-Host "Disk is initialized & formatted" -ForegroundColor Green
}
Catch {
Write-Host ""
Write-Host "Failed to initialize disk" -ForegroundColor Red
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment