Automation

 View Only
  • 1.  Need Powercli script for adding new disk on servers.

    Posted Jul 12, 2021 03:57 PM

    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

     

     



  • 2.  RE: Need Powercli script for adding new disk on servers.

    Posted Jul 12, 2021 05:11 PM

    WHat is the meaning of this line?
    That variable is nowhere to be found in the script.



  • 3.  RE: Need Powercli script for adding new disk on servers.

    Posted Jul 12, 2021 05:34 PM

    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



  • 4.  RE: Need Powercli script for adding new disk on servers.

    Posted Jul 12, 2021 05:38 PM

    Where is that variable in that condition coming from?



  • 5.  RE: Need Powercli script for adding new disk on servers.

    Posted Jul 12, 2021 06:07 PM

    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} )
    {



  • 6.  RE: Need Powercli script for adding new disk on servers.

    Posted Jul 12, 2021 06:35 PM

    That might be what you want to do, but afaik 

    is not the same as



  • 7.  RE: Need Powercli script for adding new disk on servers.

    Posted Jul 12, 2021 06:39 PM

    Could you please help me to correct my script. So that I can get expected output from script.



  • 8.  RE: Need Powercli script for adding new disk on servers.

    Posted Jul 12, 2021 06:42 PM

    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?



  • 9.  RE: Need Powercli script for adding new disk on servers.

    Posted Jul 12, 2021 06:51 PM

    To be frank I took that % calculation from internet and make adjustments according my environment.



  • 10.  RE: Need Powercli script for adding new disk on servers.
    Best Answer

    Posted Jul 12, 2021 07:32 PM

    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%.



  • 11.  RE: Need Powercli script for adding new disk on servers.

    Posted Jul 13, 2021 01:39 PM

    Thank you so much for your help LucD. Appreciate your help on quick resolution.