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
You could do something like this, but be aware that if a VM is spread over multiple datastores, this code will always take the 1st datastore, until freespace drops below 10%.
#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 "path of vmslist.txt"
$gb = Read-Host "Enter size in GB"
$size = $gb
#Declaring count variables
$countDiskAdded= 0
$countDiskNotAdded= 0
Get-VM -Name $listofvms -PipelineVariable vm |
ForEach-Object -Process {
Get-Datastore -RelatedObject $vm -PipelineVariable ds |
ForEach-Object -Process {
$freeSpacePercentage = $_.FreeSpaceGB/$_.CapacityGB*100
if($freeSpacePercentage -gt 10){
New-HardDisk -VM $vm -StorageFormat Thick -CapacityGB $size -Datastore $ds -Confirm:$false
Write-Host -ForegroundColor Green " New disk has been added successfully in $($vm.Name)"
$countDiskAdded++
}
else{
Write-Host -ForegroundColor Red "Insufficient free datastore on $($vm.Name)"
$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
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
WHat is the meaning of this line?
That variable is nowhere to be found in the script.
Where {$_."Percentage(>10%)" -gt 10} )
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
thanks for quickly respond on my post. That is condition I am using on vm’s datastore having greater than 10% free space. If something there is missing in my script. Could you please help me to correct it. Appreciate your help
Where is that variable in that condition coming from?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Condition is coming from below location. i have marked that in bold fonts. Please do let me know if below math is not correct to calculate datastore % with conditions.
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} )
{
That might be what you want to do, but afaik
$_."Percentage(>10%)"
is not the same as
"Percentage Free Space(%)"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Could you please help me to correct my script. So that I can get expected output from script.
How in fact did you write that script?
Or did you just throw together some code from the Internet and then ask for someone to make sense of it?
Have you ever written PowerCLI scripts?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
To be frank I took that % calculation from internet and make adjustments according my environment.
You could do something like this, but be aware that if a VM is spread over multiple datastores, this code will always take the 1st datastore, until freespace drops below 10%.
#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 "path of vmslist.txt"
$gb = Read-Host "Enter size in GB"
$size = $gb
#Declaring count variables
$countDiskAdded= 0
$countDiskNotAdded= 0
Get-VM -Name $listofvms -PipelineVariable vm |
ForEach-Object -Process {
Get-Datastore -RelatedObject $vm -PipelineVariable ds |
ForEach-Object -Process {
$freeSpacePercentage = $_.FreeSpaceGB/$_.CapacityGB*100
if($freeSpacePercentage -gt 10){
New-HardDisk -VM $vm -StorageFormat Thick -CapacityGB $size -Datastore $ds -Confirm:$false
Write-Host -ForegroundColor Green " New disk has been added successfully in $($vm.Name)"
$countDiskAdded++
}
else{
Write-Host -ForegroundColor Red "Insufficient free datastore on $($vm.Name)"
$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
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you so much for your help LucD. Appreciate your help on quick resolution.