VMware Cloud Community
sachink0312
Enthusiast
Enthusiast
Jump to solution

Script to Remove HDDs other than HDD1 from VM and re-add it back.

Hi I was trying to write the script to shutdown the VM and then remove the HDDs other than HDD1 and then re-add it back, here is my script ( not completed yet)

$vmname = testvm02

Get-VM -Name $vmname | %{

  Write-Host "Stopping VM" $vmname

  $vmname | Get-VMGuest | where {$_.State -eq "Running"} | Shutdown-VMGuest -Confirm:$false

  $HDD = Get-HardDisk -VM $vmname | Remove-Harddisk

above script remove all the disks which are attached to VM which i don't want, I want only to remove HDDs other than Hard Disk1 .

Can anyone please help me to write this script

Sachin
Tags (1)
1 Solution

Accepted Solutions
sachink0312
Enthusiast
Enthusiast
Jump to solution

its done .. Thanks everyone for the inputs

here is the script ...

$vmname = Get-Content -Path "VM.txt"

foreach ($VM in $Vmname){

   $getVM = Get-VM -Name $VM

   Write-Host "Stopping VM" $VM

   $getVM | Get-VMGuest | where{$_.State -eq "Running"} | Shutdown-VMGuest -Confirm:$false

  do{

      #Wait for the VM to poweroff

      $Shutdownvm = Get-VM -Name $VM

      $powerstaus = $Shutdownvm.PowerState

      Write-host "Waiting for shutdown of $VM"

      sleep 5

     } until($powerstaus -eq "PoweredOff")

   $HDDfile = Get-HardDisk -VM $VM | Where-Object {$_.Name -ne 'Hard Disk 1'} | select Filename

  

   #$HDDpath = $HDDFile.FileName

   Write-Host "Removing The HDDs of VM" $VM

   Get-HardDisk -VM $VM | Where-Object {$_.Name -ne 'Hard Disk 1'} | Remove-Harddisk -Confirm:$false

  

   foreach ($HDD in $HDDfile){

   $HDDpath = $HDD.Filename

   Write-Host "Re-adding The HDD to VM" $VM

   New-HardDisk -VM $VM -DiskPath "$HDDpath"

}

  Write-Host "Starting VM" $VM

  Start-VM $getvm

}

Sachin

View solution in original post

Reply
0 Kudos
8 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

You can filter for hard disks other than hard disk 1 using the Where-Object cmdlet. E.g.

$HDD = Get-HardDisk -VM $vmname | Where-Object {$_.Name -ne 'Hard disk 1'} | Remove-Harddisk

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
sachink0312
Enthusiast
Enthusiast
Jump to solution

Hi Robert,

Thanks for the reply,

Here i have completed the script can u please suggest if it is right

$vmname = testvm02

Get-VM -Name $vmname| %{

  Write-Host "Stoping VM" $vmname

  $vmname | Get-VMGuest | where {$_.State -eq "Running"} | Shutdwon-VMGuest -Confirm:$false

  $HDDfile = Get-HardDisk -VM $vmname | Where-Object {$_.Name -e 'Hard Disk 1'} | select Filename

  Get-HardDisk -VM $vmname | Where-Object {$_.Name -e 'Hard Disk 1'} Remove_Harddisk

  New-HardDisk -VM $vmname -DiskPath $HDDfile

  Write-Host "Starting VM" $vmname

  $vmname | Start-Vm -Confirm:$false

}

Sachin
Reply
0 Kudos
sachink0312
Enthusiast
Enthusiast
Jump to solution

Can anyone please look at my script? I have doubt at "New-HardDisk -VM $vmname -DiskPath $HDDfile", I am not sure if it will take the correct path or not.

Sachin
Reply
0 Kudos
sachink0312
Enthusiast
Enthusiast
Jump to solution

Hi Friends,

Can you please review the script below, Script is to shutdown the VM remove the disk other than HDD1 and then re-add it back.

My script works till remove HDD but getting error while adding it back.. i am not sure how to give path of the disk.

$vmname = "testvm02"

Get-VM -Name $vmname | %{

  Write-Host "Stoping VM" $vmname

  $vmname | Get-VMGuest | where {$_.State -eq "Running"} | Shutdown-VMGuest -Confirm:$false

  $HDDfile = Get-HardDisk -VM $vmname | Where-Object {$_.Name -ne 'Hard Disk 1'} | select Filename

   Get-HardDisk -VM $vmname | Where-Object {$_.Name -ne 'Hard Disk 1'} | Remove-Harddisk -Confirm:$false

   New-HardDisk -VM $vmname -DiskPath {"$HDDfile.Filename"}

   Write-Host "Starting VM" $vmname

   $vmname | Start-Vm -Confirm:$false

}

Sachin
Reply
0 Kudos
markdjones82
Expert
Expert
Jump to solution

On new-harddisk why are you specifying the actual disk path? Why not specify just the datastore?

Try this to get the datastore name and put it on the same datastore:

$HDDfile = Get-HardDisk -VM $vmname | Where-Object {$_.Name -ne 'Hard Disk 1'} | select Filename

$datastorename =$HDDfile.split("[]")[1]

Get-HardDisk -VM $vmname | Where-Object {$_.Name -ne 'Hard Disk 1'} | Remove-Harddisk -Confirm:$false

   New-HardDisk -VM $vmname -Datastore $datastorename

You also have to specify the size of hte disk.  so -capacityGB.  Don't know if they will all be same size or not but you can get that info as well and pass it or hardcode it.

http://www.twitter.com/markdjones82 | http://nutzandbolts.wordpress.com
Reply
0 Kudos
sachink0312
Enthusiast
Enthusiast
Jump to solution

i have to re-add the same existing Harddisk which I removed that why i was adding the full path of that vmdk.

Sachin
Reply
0 Kudos
markdjones82
Expert
Expert
Jump to solution

Gotcha, misread that.  What is the error you get on the new-harddisk?

Try passing just the variable with out the quotes and curly braces {" "}

New-HardDisk -VM $vmname -DiskPath $HDDfile.Filename

http://www.twitter.com/markdjones82 | http://nutzandbolts.wordpress.com
Reply
0 Kudos
sachink0312
Enthusiast
Enthusiast
Jump to solution

its done .. Thanks everyone for the inputs

here is the script ...

$vmname = Get-Content -Path "VM.txt"

foreach ($VM in $Vmname){

   $getVM = Get-VM -Name $VM

   Write-Host "Stopping VM" $VM

   $getVM | Get-VMGuest | where{$_.State -eq "Running"} | Shutdown-VMGuest -Confirm:$false

  do{

      #Wait for the VM to poweroff

      $Shutdownvm = Get-VM -Name $VM

      $powerstaus = $Shutdownvm.PowerState

      Write-host "Waiting for shutdown of $VM"

      sleep 5

     } until($powerstaus -eq "PoweredOff")

   $HDDfile = Get-HardDisk -VM $VM | Where-Object {$_.Name -ne 'Hard Disk 1'} | select Filename

  

   #$HDDpath = $HDDFile.FileName

   Write-Host "Removing The HDDs of VM" $VM

   Get-HardDisk -VM $VM | Where-Object {$_.Name -ne 'Hard Disk 1'} | Remove-Harddisk -Confirm:$false

  

   foreach ($HDD in $HDDfile){

   $HDDpath = $HDD.Filename

   Write-Host "Re-adding The HDD to VM" $VM

   New-HardDisk -VM $VM -DiskPath "$HDDpath"

}

  Write-Host "Starting VM" $VM

  Start-VM $getvm

}

Sachin
Reply
0 Kudos