-
Notifications
You must be signed in to change notification settings - Fork 0
/
DriveExpansion.ps1
56 lines (47 loc) · 2.27 KB
/
DriveExpansion.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#############################################################
#### This will expand the Hard Drive of Disk 1 in VMware ####
#############################################################
# Setting the vCenter variables
$vCenter = 'vCenterServerName'
# Setting Drive Size in GB
$DriveSizeGB = 75
# List of servers to expand
$ServerList = Get-Content 'C:\temp\disk\serverlist.csv'
# Importing the PowerCLI Module
Import-Module VMware.VimAutomation.Vds -ErrorAction Stop
# Connecting to vCenter
Connect-VIServer $VCenter -Credential (Get-Credential) | Out-Null
# Loop to set Hard Drive to $DriveSize variable in VMware. Will output status to a file
ForEach ($Server in $ServerList) {
Try {
Get-HardDisk -vm $Server | where {$_.Name -eq "Hard Disk 1"} |
Set-HardDisk -CapacityGB $DriveSizeGB -Confirm:$false
Write-Output "Set drive to $DriveSizeGB on $Server" | Out-File 'C:\temp\disk\disksuccess.csv'
}
Catch {
Write-Output "Could not expand drive on $Server" | Out-File 'C:\temp\disk\diskerror.csv'
}
}
# Closing the vCenter session
Disconnect-VIServer -Server $vCenter -Confirm:$false
#######################################################################################
#### This expands the C: drive of a Windows box to the maximum free space avaiable ####
#### PowerShell v3 and above is required ####
#######################################################################################
# List of servers to expand
$ServerList = Get-Content 'C:\temp\disk\serverlist.csv'
# ForEach loop to xpand the C: drive for all servers in $ServerList
ForEach ($Server in $ServerList) {
Try {
Invoke-Command -ComputerName $Server -ScriptBlock {
# Scanning the disk bus
"rescan" | diskpart;
# Querying what the max allowed size of the drive is set to
$MaxSize = (Get-PartitionSupportedSize -DriveLetter C).sizeMax;
# Setting the C:\ drive to the maximum size allocated
Resize-Partition -DiskNumber 0 -PartitionNumber 2 -Size $MaxSize }
Write-Output "Set drive to $DriveSizeGB on $Server" | Out-File 'C:\temp\diskexpand\disksuccess.csv'
}
Catch {
Write-Output "Could not expand drive on $Server" | Out-File 'C:\temp\diskexpand\diskerror.csv'
}