Created
December 21, 2018 14:08
-
-
Save mczerniawski/e1b2c5e907e53e8e8f45c21496589412 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Add-AHVMDisk { | |
<# | |
.SYNOPSIS | |
Adds a new vhdx to Hyper-V VM | |
.DESCRIPTION | |
Creates a new VHDX in VM's default disk location with given size (VHDSize). Attaches to a VM. | |
.PARAMETER ComputerName | |
ComputerName of a HyperV Computer where VM is. | |
.PARAMETER VMName | |
VMName to which attach a new VHDX | |
.PARAMETER VHDSize | |
Size of the VHDX file to create. | |
.PARAMETER VHDType | |
Type of VHDX file. Currently only Dynamic is supported | |
.PARAMETER Credential | |
Alternate credentials to use to connect to ComputerName | |
.EXAMPLE | |
Add-AHVMDisk -ComputerName OBJPLWHV1 -VMName OBJPLWCON0 -VHDSize 100GB -VHDType Dynamic -Credential (Get-Credential) | |
#> | |
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingPlainTextForPassword', '')] | |
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory = $True, | |
ValueFromPipelineByPropertyName = $True)] | |
[string] | |
$ComputerName, | |
[Parameter(Mandatory = $True, | |
ValueFromPipelineByPropertyName = $True)] | |
[string] | |
$VMName, | |
[Parameter(Mandatory = $True, | |
ValueFromPipelineByPropertyName = $True)] | |
[uint64] | |
$VHDSize, | |
[Parameter(Mandatory = $True, | |
ValueFromPipelineByPropertyName = $True)] | |
[ValidateSet('Dynamic')] | |
[string] | |
$VHDType, | |
[Parameter(Mandatory = $false, | |
ValueFromPipelineByPropertyName = $True)] | |
[string] | |
$VHDName, | |
[Parameter(Mandatory = $false, | |
ValueFromPipelineByPropertyName = $True)] | |
[System.Management.Automation.CredentialAttribute()] | |
$Credential | |
) | |
Process { | |
$invokeProps = @{ | |
ComputerName = $ComputerName | |
} | |
if ($PSBoundParameters.ContainsKey('Credential')) { | |
Write-Verbose -Message "Credential {$($Credential.UserName)} will be used to connect to Computer {$ComputerName}" | |
$invokeProps.Credential = $Credential | |
} | |
else { | |
Write-Verbose -Message "Processing Computer {$ComputerName} with default credentials of user {$($env:USERNAME)}" | |
} | |
Write-Verbose -Message "Processing adding new VHDX Disk to VM {$VMName} on Computer {$ComputerName}" | |
$vhdProps = @{ | |
Path = '' | |
SizeBytes = $VHDSize | |
ComputerName = $ComputerName | |
} | |
Write-Verbose -Message "Getting VM {$VMName} properties from Computer {$ComputerName} to set Path for new VHDX" | |
$VMCurrentHDD = Invoke-command @invokeProps -ScriptBlock { Get-VMHardDiskDrive -VMName $USING:VMName } | |
$VMBaseHDDPath = $VMCurrentHDD[0].Path | |
$VMCurrentVHDCount = ($VMCurrentHDD | Measure-Object ).Count | |
$VMParentPath = Split-Path $VMBaseHDDPath -Parent | |
if ($PSBoundParameters.ContainsKey('VHDName')) { | |
$VHDFileName = $VHDName | |
} | |
else { | |
$VHDFileName = "{0}_disk{1}.vhdx" -f $VMName, $VMCurrentVHDCount | |
} | |
$VHDPath = Join-Path $VMParentPath -ChildPath $VHDFileName | |
$vhdProps.Path = $VHDPath | |
Write-Verbose -Message "Setting disk type to {$VHDType} for {$VMName} on Computer {$ComputerName}" | |
if ($VHDType -eq 'Dynamic') { | |
$vhdProps.Dynamic = $true | |
} | |
Write-Verbose -Message "Creating new VHDX in {$($vhdProps.Path)} for {$VMName} on Computer {$ComputerName}" | |
Invoke-Command @invokeProps -ScriptBlock { | |
$vhdProps = $USING:vhdProps | |
New-VHD @vhdProps | |
} | |
Write-Verbose -Message "Attaching new VHD for {$VMName} on Computer {$ComputerName}" | |
Invoke-Command @invokeProps -ScriptBlock { | |
Add-VMHardDiskDrive -VMName $USING:VMName -Path $USING:VHDPath | |
} | |
Write-Verbose -Message "Attached new VHD for {$VMName} on Computer {$ComputerName}" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment