VMware Cloud Community
Venkat_A
Enthusiast
Enthusiast
Jump to solution

passing bulk vm'' s in a text to powercli script

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.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

0 Kudos
21 Replies
LucD
Leadership
Leadership
Jump to solution

Did you try

$VMs= Get-VM -Name (Get-Content d:\vmslist.txt)


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

0 Kudos
Venkat_A
Enthusiast
Enthusiast
Jump to solution

yes still failing

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
Venkat_A
Enthusiast
Enthusiast
Jump to solution

$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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
Venkat_A
Enthusiast
Enthusiast
Jump to solution

same problem

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
Venkat_A
Enthusiast
Enthusiast
Jump to solution

yes, it is showing 2.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
Venkat_A
Enthusiast
Enthusiast
Jump to solution

yes it is showing 4 now, i tested with same txt file.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
Venkat_A
Enthusiast
Enthusiast
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That should have been -Name, not _Name.


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

0 Kudos
Venkat_A
Enthusiast
Enthusiast
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
Venkat_A
Enthusiast
Enthusiast
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
Venkat_A
Enthusiast
Enthusiast
Jump to solution

Excellent Lucd, it is working fine Smiley Happy

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?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos