Skip to content

Commit 1a2d7f3

Browse files
authored
Create Attach-SharedDisks.ps1 (Azure#334)
1 parent 6cd925a commit 1a2d7f3

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<#
2+
.NAME
3+
attach-SharedDisks.ps1
4+
5+
.DESCRIPTION
6+
The script will scan a resource group for machines. It will then show a list of VMs available.
7+
Once a VM (or more) is selected, the data disks will be offered. From the selected disks, shared disks will be attached to all the VMs.
8+
9+
.PERMISSIONS REQUIRED
10+
Contributor
11+
.USE CASES
12+
13+
.Parameters
14+
$ResourceGroupName - resource group containing VMs with cluster disks
15+
$startingLunNumber - number to use as a starting LUN to attach disks. Default is 0 (assuming VM has no data disks).
16+
$DisksResourceGroup - Resource group containing the disks to be attached
17+
$linux - uses a linux compatible out-consolegridview to show the options.
18+
.NOTES
19+
AUTHOR: Microsoft Services
20+
AUTHOR DATE: 2020-Dec-16
21+
.CHANGE LOG
22+
#>
23+
param(
24+
[Parameter(Mandatory=$true)]
25+
[string]
26+
$ResourceGroupName,
27+
[Parameter(Mandatory=$true)]
28+
[string]
29+
$DisksResourceGroup,
30+
[Parameter(Mandatory=$false)]
31+
[int]
32+
$startingLunNumber=0,
33+
[Parameter(Mandatory=$false)]
34+
[switch]$linux)
35+
if ($linux)
36+
{
37+
if (!(get-module Microsoft.PowerShell.ConsoleGuiTools))
38+
{
39+
Install-Module Microsoft.PowerShell.ConsoleGuiTools
40+
$VMs=get-azvm -ResourceGroupName $ResourceGroupName | Out-ConsoleGridView -OutputMode Multiple -Title "Select VM from resource group."
41+
}
42+
}
43+
else {
44+
$VMs=get-azvm -ResourceGroupName $ResourceGroupName | Out-GridView -passthru -Title "Select VMs from resource group."
45+
}
46+
Write-Output "$($VMs.Count) VMs selected."
47+
if ($VMs)
48+
{
49+
"Selecting Disks..."
50+
if ($DisksRG -eq "" -or $null -eq $DisksRG) {$DisksRG=$ResourceGroupName} #if no disks RG is specified, assumes same resource group for disks and VMs.
51+
"Looking for disks in $DisksRG RG."
52+
if ($linux)
53+
{
54+
if (!(get-module Microsoft.PowerShell.ConsoleGuiTools))
55+
{
56+
Install-Module Microsoft.PowerShell.ConsoleGuiTools
57+
$datadisks=Get-AzDisk -ResourceGroupName $DisksRG | Where-Object{$_.MaxShares -gt 0} | Select-Object Name, Id, MaxShares | Out-ConsoleGridView -OutputMode Multiple -Title "Select shared disks to be attached:"
58+
}
59+
}
60+
else {
61+
$datadisks=Get-AzDisk -ResourceGroupName $DisksRG | Where-Object{$_.MaxShares -gt 0} | Select-Object Name, Id,MaxShares | Out-GridView -PassThru -Title "Select shared disks to be attached:"
62+
}
63+
"$($datadisks.count) selected."
64+
try
65+
{
66+
if ($datadisks)
67+
{
68+
foreach ($VM in $VMs)
69+
{
70+
$i=$startingLunNumber
71+
"Working on $($VM.Name) VM. Disk LUNs starting at $i."
72+
foreach ($dd in $datadisks)
73+
{
74+
#attach disk to VM
75+
Add-AzVMDataDisk -VM $VM -Name $dd.Name -Lun $i -ManagedDiskId $dd.Id -CreateOption Attach
76+
$i++;
77+
}
78+
Update-AzVm -VM $VM -ResourceGroupName $ResourceGroupName
79+
}
80+
}
81+
}
82+
catch {
83+
Write-Error "Error attaching new disks."
84+
}
85+
}

0 commit comments

Comments
 (0)