Automation

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

    Posted Aug 28, 2013 02:45 PM

    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



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

    Posted Aug 28, 2013 04:01 PM

    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



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

    Posted Aug 28, 2013 04:20 PM

    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

    }



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

    Posted Aug 29, 2013 01:44 PM

    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.



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

    Posted Aug 29, 2013 06:25 PM

    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

    }



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

    Posted Aug 29, 2013 07:27 PM

    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.



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

    Posted Aug 29, 2013 08:20 PM

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



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

    Posted Aug 30, 2013 12:29 PM

    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



  • 9.  RE: Script to Remove HDDs other than HDD1 from VM and re-add it back.
    Best Answer

    Posted Sep 20, 2013 08:46 PM

    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

    }