VMware Cloud Community
melove51
Contributor
Contributor
Jump to solution

Modify script for multiple VMs

I'm working on user powercli to modify the CPU and Memory of multiple VMs.

I found the perfect script created:  http://ict-freak.nl/2010/05/07/powercli-script-to-schedule-memory-and-or-vcpu-updowngrade/comment-pa...

But it's only for 1 VM.

Can anyone tell me to modify it so do multiple VMs.  A text file would be ideal, but i'm up for anything that this point, nothing I try is working.

Thanks.

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You could do

foreach($vm in Get-VM -Name (Get-Content C:\Powercli_scripts\servers.txt))

I'm not sure why it's doing random poweroffs.

You could add the -WhatIf parameter on that line, that way it will only say what it's doing, without actually executing the cmdlet.


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

View solution in original post

Reply
0 Kudos
5 Replies
LucD
Leadership
Leadership
Jump to solution

A few small changes to the Main part of Arne's script should allow this.

Try something like this

....
####################################################################################### # Main script #######################################################################################
$VIServer = Connect-VIServer $vCenter
If
($VIServer.IsConnected -ne $true){   Write-Host "error connecting to $vCenter" -ForegroundColor Red
 
exit
} foreach($vm in Get-VM){   if($MemoryMB -or $CPUCount -ne "0"){     $poweroff = PowerOff-VM $vm.Name
    if($poweroff -eq "Ok"){       Write-Host "PowerOff OK"       if($MemoryMB -ne "0"){         if($MemoryOption -eq " ") {Write-Host "Please enter an option to add or remove memory"}         else{           Change-VMMemory $vm.Name $MemoryMB $MemoryOption
        }       }       if($CPUCount -ne "0"){         if($CPUOption -eq " ") {Write-Host "Please enter an option to add or remove cpu"}         else{           Change-VMCPUCount $vm.Name $CPUCount $CPUOption
        }       }      
$poweron = PowerOn-VM $vm.Name
      if($poweron -eq "Ok"){         Write-Host "PowerOn OK"}     }   } }

Note: the functions part of the script stays the same, just replace the "Main" part


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

Reply
0 Kudos
melove51
Contributor
Contributor
Jump to solution

Oh man, getting so close.

For some reason it's powering off random servers.

Can I do something like this and pull from a text file:  foreach($vm in C:\Powercli_scripts\servers.txt)

Or in the script: 

PowerCLI C:\Powercli_scripts> .\Change-VM_Memory_CPU_Count.ps1 -vCenter vcenter.local -vmName "C:\Powercli_scripts\servers.txt" -MemoryMB 4096 -MemoryOption Remove -CPUCount 3 -CPUOption Remove
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could do

foreach($vm in Get-VM -Name (Get-Content C:\Powercli_scripts\servers.txt))

I'm not sure why it's doing random poweroffs.

You could add the -WhatIf parameter on that line, that way it will only say what it's doing, without actually executing the cmdlet.


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

Reply
0 Kudos
melove51
Contributor
Contributor
Jump to solution

That was it man.

I had to update it a little, but that was it:  foreach($vm in Get-VM -Name (Get-Content .\servers.txt)

Reply
0 Kudos
TommyLee95
Contributor
Contributor
Jump to solution

Hello - I would i invoke the script not to specify one vm?

.\Change-VM_Memory_CPU_Count.ps1 -vCenter we-vctsrv-02 -vmName <> -MemoryMB 2048 -MemoryOption Add -CPUCount 1 -CPUOption Add

Reply
0 Kudos