Need Powercli script for adding new disk on servers. If VM's datastore having more than 10% free space available(Condition) matches then servers are eligible to add new disks to servers through script . i am using below script however i am not able to get required output( add new disk) on servers. please help me to resolve my new disk add scripts.
#Script checks status of VMs free datastore space and new disk in VMs.
$myvcenters= read-host "Enter Your Vcenter Name"
$creds = Get-Credential -Message "Enter your Vcenter credentials"
connect-viserver -server $myvcenters -credentials $creds
Write-Host -ForegroundColor Green "$myvcenters have been connected successfully"
$listofvms= "get-content "path of vmslist.txt""
$gb = Read-Host "Enter size in GB"
$size = $gb
#Decalring count variables
$countDiskAdded= 0
$countDiskNotAdded= 0
foreach ($vm in $listofvms){
if (Get-VM -Name $vm | Get-Datastore | select @{N="DataStoreName";E={$_.Name}},@{N="Percentage Free Space(%)";E={[math]::Round(($_.FreeSpaceGB)/($_.CapacityGB)*100,2)}} | Where {$_."Percentage(>10%)" -gt 10} )
{
get-vm $vm | New-HardDisk -StorageFormat Thick -CapacityGB $size | Select-Object Parent, Filename, CapacityGB
Write-Host -ForegroundColor Green " New disk has been added successfully in $vm"
$countDiskAdded++
}
Else {
Write-Host -ForegroundColor Red "Insufficient free datastore on $vm"
$countDiskNotAdded++
}
}
Write-Host -ForegroundColor Green "`r`nNumber of New disk added VMs = $countDiskAdded"
Write-Host -ForegroundColor Red "`nNumber of No new disk added VMs= $countDiskNotAdded"
#Disconnect-VIServer * -Confirm:$false