VMware Cloud Community
alamathar
Contributor
Contributor

How to find a correct datastore of a VM and add a new disk of size 10GB to list of VMs that saved in a ".txt" file?

How to find a correct datastore of a VM and add a new disk of size 10GB to list of VMs that saved in a ".txt" file?

For example i have below code to add new disk of 10 GB, but i need to check each VM for correct datastore name.

I need a single script that fetch correct datastore name and add a new disk of same type that it already has.

$vmlist = Get-Content C:\vm.txt

foreach($VM in $VMlist)

{

New-HardDisk -VM $vm -CapacityGB 10 -Datastore "Datastore_Name" -storageFormat thick

}

Any one please help on this?

6 Replies
LucD
Leadership
Leadership

You mean something like this?

$vmlist = Get-Content C:\vm.txt

foreach($vm in Get-VM -Name $vmlist){

   $hd = Get-HardDisk -VM $vm | select -First 1

   $dsName = $hd.Filename.Split('/')[0].TrimStart('[')

   New-HardDisk -VM $vm -CapacityGB 10 -Datastore $dsName -storageFormat $hd.StorageFormat

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
alamathar
Contributor
Contributor

Thanks for quick response. but getting below error msg. tried for 6 VMs.

WARNING: Parameter 'VM' is obsolete. Passing multiple values to this parameter is obsolete.

New-HardDisk : 11/11/2018 3:02:33 AM    New-HardDisk            StorageResource parameter: Could not find any of the objects specified by na

me.

At C:\Users\alamathaadmin\Desktop\1.ps1:10 char:4

+    New-HardDisk -VM $vm -CapacityGB 10 -Datastore $dsName -storageFor ...

+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (VMware.VimAutom...ource Datastore:RuntimePropertyInfo) [New-HardDisk], ObnRecordProcessingF

   ailedException

    + FullyQualifiedErrorId : Core_ObnSelector_SetNewParameterValue_ObjectNotFoundCritical,VMware.VimAutomation.ViCore.Cmdlets.Commands.Vi

   rtualDevice.NewHardDisk

pastedImage_0.png

0 Kudos
LucD
Leadership
Leadership

Don't these VMs already have a harddisk perhaps?

Can you run the same with the added debugging line?

$vmlist = Get-Content C:\vm.txt

foreach($vm in Get-VM -Name $vmlist){

   $hd = Get-HardDisk -VM $vm | select -First 1

   Write-Host "VM $($vm.Name) HD $($hd.Name) - $($hd.Filename)"

   $dsName = $hd.Filename.Split('/')[0].TrimStart('[')

   New-HardDisk -VM $vm -CapacityGB 10 -Datastore $dsName -storageFormat $hd.StorageFormat

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
alamathar
Contributor
Contributor

Hi Again alert is showing, but disk is not added. Below VM has one disk already have.

pastedImage_0.png

more than 100 VMs i have to do new disk add under existing datastore.

0 Kudos
LucD
Leadership
Leadership

Ok, I see what happened, the Split method had the incorrect character.

Try like this

$vmlist = Get-Content C:\vm.txt

foreach($vm in Get-VM -Name $vmlist){

   $hd = Get-HardDisk -VM $vm | select -First 1

   $dsName = $hd.Filename.Split(']')[0].TrimStart('[')

   New-HardDisk -VM $vm -CapacityGB 10 -Datastore $dsName -storageFormat $hd.StorageFormat

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

alamathar
Contributor
Contributor

Thanks alot, script is working fine and it is very helpful.

0 Kudos