Remove-SCVirtualDiskDrive

Removes a virtual disk drive object from a virtual machine or from a virtual machine template.

Description

The Remove-SCVirtualDiskDrive cmdlet removes one or more virtual disk drive objects from a virtual machine or from a virtual machine template in a System Center Virtual Machine Manager (VMM) environment.

For more information about Remove-SCVirtualDiskDrive, type: "Get-Help Remove-SCVirtualDiskDrive -online".

Parameters

VirtualDiskDrive

Required? true
Accept Pipeline Input? true (ByValue)
Position? 0
Specifies a virtual disk drive object. You can attach either a virtual hard disk (for a virtual machine on any host) or a pass-through disk (for a virtual machine on a Hyper-V host or an ESX host) to a virtual disk drive object.

Force

Required? false
Accept Pipeline Input? false
Position? named
Forces the operation to complete. 

For example:
- Remove-SCSCVMHost -Force
  Forces the removal of a host object from the VMM database.

- Stop-SCVirtualMachine -Force
  Stops a virtual machine.

JobGroup

Required? false
Accept Pipeline Input? false
Position? named
Specifies an identifier for a series of commands that will run as a set just before the final command that includes the same job group identifier runs. 

JobVariable

Required? false
Accept Pipeline Input? false
Position? named
Specifies that job progress is tracked and stored in the variable named by this parameter. 

PROTipID

Required? false
Accept Pipeline Input? false
Position? named
Specifies the ID of the PRO tip that triggered this action. This allows for auditing of PRO tips.

RunAsynchronously

Required? false
Accept Pipeline Input? false
Position? named
Indicates that the job runs asynchronously so that control returns to the command shell immediately. 

SkipDeleteVHD

Required? false
Accept Pipeline Input? false
Position? named
Indicates that the VHD file will not be deleted when the virtual disk drive is removed.
Requires a VMM virtual disk drive object, which can be retrieved by using the Get-SCVirtualDiskDrive cmdlet.

Examples

1: Remove the second virtual disk drive object from the specified virtual machine.
PS C:\> $VM = Get-SCVirtualMachine | where { $_.VMHost.Name -eq "VMHost01.Contoso.com" -and $_.Name -eq "VM01" }
PS C:\> $VirtDiskDrive = @(Get-SCVirtualDiskDrive -VM $VM)
PS C:\> if($VirtDiskDrive.Count -gt 1){Remove-SCVirtualDiskDrive -VirtualDiskDrive $VirtDiskDrive[1]}
The first command gets the virtual machine object named VM01 deployed on VMHost01 and stores the object in the $VM variable.

The second command gets all virtual disk drive objects on VM01 and stores the retrieved objects in $VirtDiskDrive. Using the '@' symbol and parentheses ensures that the command stores the results in an array in case the command returns a single object or a null value.

The last command returns the number of virtual disk drives associated with the virtual machine and then, if more than one exists, the command removes the second virtual disk drive (designated by the [1]) from the virtual machine. 
2: Remove all pass-through disks attached to a VM.
PS C:\> $VM = Get-SCVirtualMachine | where {$_.Name -eq "VM02"}
PS C:\> $VirtDiskDrives = @(Get-SCVirtualDiskDrive -VM $VM | where {$_.IsVHD -eq $False})
PS C:\> if($VirtDiskDrives.Count -gt 0){foreach($VirtDiskDrive in $VirtDiskDrives){Remove-SCVirtualDiskDrive -Force -VirtualDiskDrive $VirtDiskDrive}}
The first command gets the virtual machine object named VM02 and stores the object in the $VM variable.

The second command gets all virtual disk drive objects attached to VM02 that are not virtual hard disks (that is, only objects that represent pass-through disks are retrieved) and stores the pass-through disk objects in the $VDDs object array.

The last command uses an if statement to determine whether at least one pass-through virtual disk drive exists. If the result is one or more, the command then uses the foreach statement to remove each virtual disk drive from the object array. Using the Force parameter ensures the removal of each virtual disk drive from its virtual machine even if other VMM objects depend on that virtual disk drive.

NOTE: For more information about the standard Windows PowerShell foreach loop statement, type: "Get-Help about_ForEach".
3: Remove virtual disk drives attached to a VM by name.
PS C:\> $VMs = @(Get-SCVirtualMachine | where {$_.Name -match "WebSrvLOB"})
PS C:\> foreach ($VM in $VMs){$VirtDiskDrives = Get-SCVirtualDiskDrive -VM $VM; foreach ($VirtDiskDrive in $VirtDiskDrives){if($VirtDiskDrive.Name -match "LOBData"){Remove-SCVirtualDiskDrive -VirtualDiskDrive $VirtDiskDrive}}}
The first command gets all virtual machine objects whose name match the string "WebSrvLOB" and stores the objects in the $VM object array.

The second command uses the ForEach cmdlet to iterate through the virtual machines stored in $VM to get all virtual disk drive objects from each virtual machine. The command stores the virtual disk drive objects in the $VirtDiskDrives object array. Then, the command uses a second ForEach loop to select all virtual disk drive objects whose name contains the string LOBData from the $VirtDiskDrives array and passes these objects to the Remove-SCVirtualDiskDrive cmdlet which removes the objects from VMM.

See Also