Found a script that should theoretically increase the disk size on a .csv list of VM's. Unfortunately however it fails, and in honesty I dont have any idea why, using F8 to step through it you can see where it fails below. Any suggestions would be greatly appreciated, this is vSphere 6.5 if its relevant.
write-Host "Loading Automation Tools"
Add-PSSnapin VMware.VimAutomation.Core
# Establish Connection
Connect-VIServer -Server SecretServer -User ME -Password SecretPassword
Import-Csv -Path 'C:\temp\testvms.csv' | %{
$VMx = $_.Name
$vm = get-vm |?{$_.Name -eq $VMx}
$HD = get-harddisk -VM $vm.Name -Name "Hard disk 1"
$NewCap = [decimal]::round($HD.CapacityGB + 20)
$HD | Set-harddisk -CapacityGB $NewCap
}
OUTPUT...
SecretServer... 443 hidden\ServerMe
Get-HardDisk : 16/04/2018 09:17:07 Get-HardDisk Exception has been thrown by the target of an invocation. At
C:\temp\IncreaseDisk120GB.ps1:13 char:10
+ $HD = get-harddisk -VM $vm.Name -Name "Hard disk 1"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-HardDisk], VimException
+ FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.GetHard
Disk
Set-HardDisk : Cannot validate argument on parameter 'HardDisk'. The argument is null. Provide a valid value for the
argument, and then try running the command again.At C:\temp\IncreaseDisk120GB.ps1:15 char:10
+ $HD | Set-harddisk -CapacityGB $NewCap
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Set-HardDisk], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.Set
HardDisk
Get-HardDisk : 16/04/2018 09:17:07 Get-HardDisk Exception has been thrown by the target of an invocation. At
C:\temp\IncreaseDisk120GB.ps1:13 char:10
+ $HD = get-harddisk -VM $vm.Name -Name "Hard disk 1"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-HardDisk], VimException
+ FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.GetHard
Disk
Set-HardDisk : Cannot validate argument on parameter 'HardDisk'. The argument is null. Provide a valid value for the
argument, and then try running the command again.At C:\temp\IncreaseDisk120GB.ps1:15 char:10
+ $HD | Set-harddisk -CapacityGB $NewCap
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Set-HardDisk], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.Set
HardDisk
Get-HardDisk : 16/04/2018 09:17:07 Get-HardDisk Exception has been thrown by the target of an invocation. At
C:\temp\IncreaseDisk120GB.ps1:13 char:10
+ $HD = get-harddisk -VM $vm.Name -Name "Hard disk 1"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-HardDisk], VimException
+ FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.GetHard
Disk
Set-HardDisk : Cannot validate argument on parameter 'HardDisk'. The argument is null. Provide a valid value for the
argument, and then try running the command again.At C:\temp\IncreaseDisk120GB.ps1:15 char:10
+ $HD | Set-harddisk -CapacityGB $NewCap
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Set-HardDisk], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.Set
HardDisk
PS H:\>
There is something wrong with the passing of the CapacityGB property over the pipeline.
Try this variation.
Import-Csv -Path 'C:\temp\testvms.csv' | %{
Get-VM -Name $_.Name |
Get-HardDisk -Name 'Hard disk 1' | %{
Set-HardDisk -HardDisk $_ -CapacityGB ($_.CapacityGB + 20) -Confirm:$false
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Nothing too obvious in that error I'm afraid.
Did you already stop/start your PowerShell/PowerCLI session?
You seem to be using an older PowerCLI version, which version is it?
Can you upgrade?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
And can you show an example of the CSV file?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Nothing too obvious in that error I'm afraid.
Did you already stop/start your PowerShell/PowerCLI session?
I tried killing PowerShell and restarting mate but unfortunately no banana, same problem.
You seem to be using an older PowerCLI version, which version is it?
Can you upgrade?
From memory I was deliberately holding off upgrading, think I discovered newer version broke scripts/reports I ran on occasions. I will however pursue this now and report back mate.
Attached is the .CSV file if it helps ...
Current versions ...
Name Version
---- -------
VMware.VimAutomation.Cis.Core 6.0.0.0
VMware.VimAutomation.Common 6.3.0.0
VMware.VimAutomation.Sdk 6.3.0.0
Can you try with this version of your script?
Import-Csv -Path 'C:\temp\testvms.csv' | %{
Get-VM -Name $_.Name |
Get-HardDisk -Name 'Hard disk 1' |
Set-HardDisk -CapacityGB ($_.CapacityGB + 20) -Confirm:$false
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I ran that script mate and got the below, for clarity the drives on the VM's are already 40GB. Can I confirm this does extend the drive rather than nuke it and replace it with a larger one? ... If I change the value to 41GB however its works great, just a tad worried I might lose data however if I run it on production. Thanks however mate, feel free to educate me and let me know how I was being a newb with this one.
Set-HardDisk : 16/04/2018 11:03:56 Set-HardDisk The specified size '20971520' is less than actual size of the
virtual disk '41943040'. At H:\Handy PowerCLI Scripts\IncreaseDiskXXGB\IncreaseDisk120GB.ps1:16 char:4
+ Set-HardDisk -CapacityGB ($_.CapacityGB + 20) -Confirm:$false
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-HardDisk], InvalidArgument
+ FullyQualifiedErrorId : Client20_VirtualDeviceServiceImpl_TryValidateHardDiskNewSize_InvalidSize,VMware.VimAutomation.V
iCore.Cmdlets.Commands.VirtualDevice.SetHardDisk
Set-HardDisk : 16/04/2018 11:03:57 Set-HardDisk The specified size '20971520' is less than actual size of the
virtual disk '41943040'. At H:\Handy PowerCLI Scripts\IncreaseDiskXXGB\IncreaseDisk120GB.ps1:16 char:4
+ Set-HardDisk -CapacityGB ($_.CapacityGB + 20) -Confirm:$false
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-HardDisk], InvalidArgument
+ FullyQualifiedErrorId : Client20_VirtualDeviceServiceImpl_TryValidateHardDiskNewSize_InvalidSize,VMware.VimAutomation.V
iCore.Cmdlets.Commands.VirtualDevice.SetHardDisk
Set-HardDisk : 16/04/2018 11:03:59 Set-HardDisk The specified size '20971520' is less than actual size of the
virtual disk '41943040'. At H:\Handy PowerCLI Scripts\IncreaseDiskXXGB\IncreaseDisk120GB.ps1:16 char:4
+ Set-HardDisk -CapacityGB ($_.CapacityGB + 20) -Confirm:$false
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-HardDisk], InvalidArgument
+ FullyQualifiedErrorId : Client20_VirtualDeviceServiceImpl_TryValidateHardDiskNewSize_InvalidSize,VMware.VimAutomation.V
iCore.Cmdlets.Commands.VirtualDevice.SetHardDisk
It looks as if the current capacity of the harddisk is not passed correctly.
Or you are in such an old PowerCLI version that the CapacityGB doesn't exist.
Can you do the following to check?
Import-Csv -Path 'C:\temp\testvms.csv' | %{
Get-VM -Name $_.Name |
Get-HardDisk -Name 'Hard disk 1' | Select *
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Running that little test/snippet of code mate I get the following output, with seemingly no errors...
StorageFormat : Thin
Persistence : Persistent
DiskType : Flat
Filename : [blah_Content_Library] TestVM1/TestVM1.vmdk
CapacityKB : 42991616
CapacityGB : 41
ParentId : VirtualMachine-vm-40172
Parent :
Uid : /VIServer=blah\serverlukeg@blah.internal.secret.co.uk:443/VirtualMachine=VirtualMachine-vm-
40172/HardDisk=2000/
ConnectionState :
ExtensionData : VMware.Vim.VirtualDisk
Id : VirtualMachine-vm-40172/2000
Name : Hard disk 1
Client : VMware.VimAutomation.ViCore.Impl.V1.VimClient
PS H:\>
There is something wrong with the passing of the CapacityGB property over the pipeline.
Try this variation.
Import-Csv -Path 'C:\temp\testvms.csv' | %{
Get-VM -Name $_.Name |
Get-HardDisk -Name 'Hard disk 1' | %{
Set-HardDisk -HardDisk $_ -CapacityGB ($_.CapacityGB + 20) -Confirm:$false
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That worked flawlessly thanks mate, incidentally for the newer PowerCLI versions can you tell me the equivalent of ...
Add-PSSnapin VMware.VimAutomation.Core
The above is contained in all my scripts and they don't work if I am running the script from a machine containing newer versions of PowerCLI 6.3+.
The latest versions don't have any PSSnapin anymore.
If you are on PowerShell 4 or higher, and the PowerCLI modules are installed in one of the default locations (AllUsers or CurrentUser scope), you don't need to load anything anymore.
The module auto-load takes care of loading the required module(s) the first time you use a PowerCLI cmdlet.
See also Welcome PowerCLI to the PowerShell Gallery – Install Process Updates
I would advice to go for PowerShell 5.1 and PowerCLI 10.
You will get the latest PowerCLI version automatically when you run the Install-Module -Name VMware.PowerCLI.
You will have to comment out/remove the Add-PSSnapin lines in your scripts I'm afraid.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference