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?
You mean something like this?
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
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
Don't these VMs already have a harddisk perhaps?
Can you run the same with the added debugging line?
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
Hi Again alert is showing, but disk is not added. Below VM has one disk already have.
more than 100 VMs i have to do new disk add under existing datastore.
Ok, I see what happened, the Split method had the incorrect character.
Try like this
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
Thanks alot, script is working fine and it is very helpful.