VMware Cloud Community
GuilhermeStela
Enthusiast
Enthusiast
Jump to solution

Get VM`s Disk Size

Hi !

Since i`ve learned to expand a disk partition i note that when running the cmdlet, the total size of the disk is needed.

Is there some way to give only the difference between the real size and the future size? IF i want to expand a disk with 40GB, i have to give, at the cmdlet request the 45GB info if i want to expand 5GB. Is there a way to put only the 5GB information or some way to mask the 45GB value?

i`ll put more information later. pls ask if there`s any doubts. tks

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Why would you need to switch between CMD and PowerCLI ?

You can launch CMD commands from PowerCLI.

Something like this should work

function Increase-HarddiskBy{
  param(
    [PSObject]$Harddisk,
    [
int]$IncreaseGB
  )    
Set-HardDisk -HardDisk $Harddisk -CapacityKB ($harddisk.CapacityKB + ($IncreaseGB*1MB)) -Confirm:$false
}
$extraSpace = 10

$vmName
= Read-Host "Enter the name of the VM"
$vm
= Get-VM -Name $vmName

$hd
= Get-HardDisk -VM $vm | where {$_.Name -eq "Hard disk 2"}
Increase-HarddiskBy
-Harddisk $hd -IncreaseGB $extraSpace
"\\pacotes\software$\vdi\Scripts\extdisk_.bat $vmName $extraSpace"


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

View solution in original post

Reply
0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

One way of solving this, is to write a function to which you pass the additional space you want add as a parameter.

Inside the function, you get the current size, add the extra space from the parameter, and then call the cmdlet with the new total size.

Let me know if you need more help with such a function.


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

Reply
0 Kudos
GuilhermeStela
Enthusiast
Enthusiast
Jump to solution

i'll reply as soon as possible with the function. i'll aprecciate if you could do a part of f it.

i don't really begin this part of the script but for sure it is possible.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Something very simple.

function Increase-HarddiskBy{
  param(
    [PSObject]$Harddisk,
    [int]$IncreaseGB
  )     Set-HardDisk -HardDisk $Harddisk -CapacityKB ($harddisk.CapacityKB + ($IncreaseGB*1MB)) -Confirm:$false
}
$hd = Get-VM -Name MyVM | Get-HardDisk | where {$_.Name -eq "Hard disk 2"} Increase-HarddiskBy -Harddisk $hd -IncreaseGB 5

There is no parameter checking, no pipeline functionality. Very simple like I said, but it should give you an idea (I hope)


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

Reply
0 Kudos
GuilhermeStela
Enthusiast
Enthusiast
Jump to solution

I want to mix powercli and CMD prompt script, so i think in the following steps:

1)get the name of the virtual machine in the cmd prompt;

2) execute still in the cmd prompt, the upgrade of the disk(defaulting 5GB) using PowerClI commands;

3) and execute the recognizing of the non-allocated disk inside the guestOs with another script that is ready;

Every time i call a powercli command in the cmd prompt i have to connect to viserver, execute the command itself and disconnect, so i want to make a one-line expansion. Well, see the script, it will self explain(or not...hehe)

:restartScript
set vm = 0
set opcao = 10
set user = 0
set pw  = 0

cls
echo off
set cabecalho= Criado por Suporte VDI - Guilherme Alves Stela
set cabecalho2= --------------------------------------------------

color 71
cls

echo %cabecalho%
echo %cabecalho2%
echo.
echo.
echo          ---------------------------------------------------
echo         |                                                   |
echo         | Digite seu usuário e senha para acesso ao vCenter |
echo         |                                                   |
echo          ---------------------------------------------------
echo.

cls

echo %cabecalho%
echo %cabecalho2%
echo.
echo.
echo          ----------------------------
echo         |                            |
echo         |  Digite o nome da maquina  |
echo         |                            |
echo          ----------------------------
echo.
set /p vm=
cls
echo       Escolha um dos volumes para Expandir
echo.
echo          ----------------
echo         | 0  |  Disco C: |   
echo         |----------------| 
echo         | 1  |  Disco 😧 |   
echo         |----------------| 
echo         | C  |  Cancelar |
echo          ----------------
echo.

set /p opcao=

if %opcao% equ 0 goto discoC
if %opcao% equ 1 goto discoD
if %opcao% equ c goto cancelar

:discoC

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\aut.ps1" -command "function Increase-HarddiskBy{param([PSObject]$Harddisk,[int]$IncreaseGB)Set-HardDisk -HardDisk $Harddisk -CapacityKB ($harddisk.CapacityKB + ($IncreaseGB*1MB)) -Confirm:$false};$hd = Get-VM -Name *%vm%* | Get-HardDisk | where {$_.Name -eq "Hard disk 1"};Increase-HarddiskBy -Harddisk $hd -IncreaseGB 5;"disconnect-viserver -confirm:$false"

goto expandOSdisk

:discoD

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\aut.ps1" -command " function Increase-HarddiskBy{param([PSObject]$Harddisk,[int]$IncreaseGB)Set-HardDisk -HardDisk $Harddisk -CapacityKB ($harddisk.CapacityKB + ($IncreaseGB*1MB)) -Confirm:$false};$hd = Get-VM -Name *%vm%* | Get-HardDisk | where {$_.Name -eq "Hard disk 2"};Increase-HarddiskBy -Harddisk $hd -IncreaseGB 5;"disconnect-viserver -confirm:$false"

goto expandOSdisk


:expandOSdisk

\\pacotes\software$\vdi\Scripts\extdisk_.bat %vm% %opcao%

goto restartScript

:cancelar

cls

goto restartScript


(some expressions are in portuguese)

My problem now is to increase the disk with the one-line command....every time a different error..:smileyconfused:

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Why would you need to switch between CMD and PowerCLI ?

You can launch CMD commands from PowerCLI.

Something like this should work

function Increase-HarddiskBy{
  param(
    [PSObject]$Harddisk,
    [
int]$IncreaseGB
  )    
Set-HardDisk -HardDisk $Harddisk -CapacityKB ($harddisk.CapacityKB + ($IncreaseGB*1MB)) -Confirm:$false
}
$extraSpace = 10

$vmName
= Read-Host "Enter the name of the VM"
$vm
= Get-VM -Name $vmName

$hd
= Get-HardDisk -VM $vm | where {$_.Name -eq "Hard disk 2"}
Increase-HarddiskBy
-Harddisk $hd -IncreaseGB $extraSpace
"\\pacotes\software$\vdi\Scripts\extdisk_.bat $vmName $extraSpace"


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

Reply
0 Kudos
GuilhermeStela
Enthusiast
Enthusiast
Jump to solution

The opposite works fine! tks !!Smiley Happy

The advantage is that now I've learned a few more tricks.

Reply
0 Kudos