Hi,
I want to increase the vmdk size . I have a script to increase the vmdk size, but I want to pass the file with vm names as a paramter,
I tried using this
$VMs=Get-Content d:\vmslist.txt
but it is taking all machines names as 1vm name even though they are 1 per line.
Try the script like this now
foreach($vm in (Get-VM -Name (Get-Content d:\vmslist.txt ))){
$guestUser = "xxxxx"
$guestPass = "xxxxx"
$newSizeGB = xxx
Get-HardDisk -vm $vm | ?{$_.name -eq "Hard disk 1"} |
Set-HardDisk -CapacityKB ($newSizeGB * 1MB) -ResizeGuestPartition -GuestUser $guestUser -GuestPassword $guestPass -confirm:$false -ErrorAction:SilentlyContinue
Invoke-VMScript -vm $vm -ScriptText "echo select vol c > c:\diskpart.txt && echo extend >> c:\diskpart.txt && diskpart.exe /s c:\diskpart.txt" -GuestUser $guestUser -GuestPassword $guestPass -ScriptType BAT
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Did you try
$VMs= Get-VM -Name (Get-Content d:\vmslist.txt)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
yes still failing
Can you attach the TXT file ?
And perhaps the script you are trying to use this in ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
$VMs= Get-VM -Name (Get-Content d:\vmslist.txt)
$guestName="$VMs"
$guestUser="xxxxx"
$guestPass="xxxxx"
$newSizeGB=xxx
Get-HardDisk -vm "$guestName" |
?{$_.name -eq "hard disk 1"} |
Set-HardDisk -CapacityKB ($newSizeGB*1MB) -ResizeGuestPartition -GuestUser $guestUser -GuestPassword $guestPass -confirm:$false -ErrorAction:SilentlyContinue
Invoke-VMScript -vm $guestName -ScriptText "echo select vol c > c:\diskpart.txt && echo extend >> c:\diskpart.txt && diskpart.exe /s c:\diskpart.txt" -GuestUser $guestUser -GuestPassword $guestPass -ScriptType BAT
Try like this
foreach($vm in (Get-VM -Name (Get-Content d:\vmslist.txt ))){
$guestName = $vm
$guestUser = "xxxxx"
$guestPass = "xxxxx"
$newSizeGB = xxx
Get-HardDisk -vm $guestName | ?{$_.name -eq "Hard disk 1"} |
Set-HardDisk -CapacityKB ($newSizeGB * 1MB) -ResizeGuestPartition -GuestUser $guestUser -GuestPassword $guestPass -confirm:$false -ErrorAction:SilentlyContinue
Invoke-VMScript -vm $guestName -ScriptText "echo select vol c > c:\diskpart.txt && echo extend >> c:\diskpart.txt && diskpart.exe /s c:\diskpart.txt" -GuestUser $guestUser -GuestPassword $guestPass -ScriptType BAT
}
The Get-Content cmdlet will return an array of VM names, so you will have to handle them one by one.
Hence the ForEach loop
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
same problem
Can you do the following test
$lines = Get-Content d:\vmslist.txt
$lines.Count
and tell me what number, if any, is returned ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
yes, it is showing 2.
That should have been 4, provided you used the same TXT file you attached earlier.
Can you try with a new TXT file, and enter the VM names with a plain text editor (for example Notepad or Notepad++) ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
yes it is showing 4 now, i tested with same txt file.
Ok, next step. Try
$lines = Get-Content d:\vmslist.txt
Get-VM -name $lines
Does that return 4 VM objects ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
ERROR
PowerCLI D:\> $lines = Get-Content d:\vmslist.txt
PowerCLI D:\> Get-VM _name $lines
Get-VM : A positional parameter cannot be found that accepts argument 'System.O
bject[]'.
At line:1 char:7
+ Get-VM <<<< _name $lines
+ CategoryInfo : InvalidArgument: (:) [Get-VM], ParameterBindingE
xception
+ FullyQualifiedErrorId : PositionalParameterNotFound,VMware.VimAutomation
.ViCore.Cmdlets.Commands.GetVM
That should have been -Name, not _Name.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
PowerCLI D:\> $lines = Get-Content d:\vmslist.txt
PowerCLI D:\> Get-VM -Name $lines
Name PowerState Num CPUs MemoryGB
---- ---------- -------- --------
AT3NPSNYA01V0_Son... PoweredOn 2 4.000
AT3NPSUNA05V0_Sun... PoweredOn 2 4.000
AT3NPOMXA03V0_Off... PoweredOn 2 2.000
AT3NPSNLA05V_SNL_RPC PoweredOn 2 6.000
Great, and finally
Get-VM -Name (Get-Content d:\vmslist.txt )
Does this also return 4 VM objects ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
yes
PowerCLI D:\> Get-VM -Name (Get-Content d:\vmslist.txt )
Name PowerState Num CPUs MemoryGB
---- ---------- -------- --------
AT3NPSNYA01V0_Son... PoweredOn 2 4.000
AT3NPSUNA05V0_Sun... PoweredOn 2 4.000
AT3NPOMXA03V0_Off... PoweredOn 2 2.000
AT3NPSNLA05V_SNL_RPC PoweredOn 2 6.000
Try the script like this now
foreach($vm in (Get-VM -Name (Get-Content d:\vmslist.txt ))){
$guestUser = "xxxxx"
$guestPass = "xxxxx"
$newSizeGB = xxx
Get-HardDisk -vm $vm | ?{$_.name -eq "Hard disk 1"} |
Set-HardDisk -CapacityKB ($newSizeGB * 1MB) -ResizeGuestPartition -GuestUser $guestUser -GuestPassword $guestPass -confirm:$false -ErrorAction:SilentlyContinue
Invoke-VMScript -vm $vm -ScriptText "echo select vol c > c:\diskpart.txt && echo extend >> c:\diskpart.txt && diskpart.exe /s c:\diskpart.txt" -GuestUser $guestUser -GuestPassword $guestPass -ScriptType BAT
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Excellent Lucd, it is working fine
I have a small query, Now this script is increasing the vmdk size and increase at OS level only for win2008 servers.
how to do the same for 2003 with the script?
You should use the HelperVM parameter on the Set-Harddisk cmdlet, but be aware that the VM needs to be powered off.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference