VMware Cloud Community
Seanpeters
Enthusiast
Enthusiast
Jump to solution

PowerCLi Right Size

I have been modifing a script to do automated right sizing for me based on an imported csv file.

I think i have the Script sorted out but it will not run, I get this error.

Any help would be appriciated.

Get-VM : Cannot validate argument on parameter 'Name'. The argument is null or
empty. Supply an argument that is not null or empty and then try the command ag
ain.
At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\RightSize.ps1:
66 char:28
+ foreach($vm in Get-VM -Name <<<<  ($vms."Virtual Machine")){
    + CategoryInfo          : InvalidData: (:) [Get-VM], ParameterBindingValid
   ationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
   ation.ViCore.Cmdlets.Commands.GetVM

This is the code.

function PowerOff-VM{
    param([string] $vm)
   
    Shutdown-VMGuest -VM (Get-VM $vm) -Confirm:$false | Out-Null
    Write-Host "Shutdown $vm"
    do {
        $status = (get-VM $vm).PowerState
    }until($status -eq "PoweredOff")
    return "OK"
}

function PowerOn-VM{
    param( [string] $vm)
   
    if($vm -eq ""){    Write-Host "Please enter a valild VM name"}
   
    if((Get-VM $vm).powerstate -eq "PoweredOn"){
        Write-Host "$vm is already powered on"}
   
    else{
        Start-VM -VM (Get-VM $vm) -Confirm:$false | Out-Null
        Write-Host "Starting $vm"
        do {
            $status = (Get-vm $vm | Get-View).Guest.ToolsRunningStatus
        }until($status -eq "guestToolsRunning")
        return "OK"
    }
}


   
$VIServer = Connect-VIServer "vcenter"
If ($VIServer.IsConnected -ne $true){
  Write-Host "error connecting to $vCenter" -ForegroundColor Red
  exit
}
$vms = Import-CSV .\vcopsreport.csv
foreach($vm in Get-VM -Name ($vms."Virtual Machine")){

  if($vms."Recommended Memory" -or $vms."Recommended vCPU" -ne "0"){
    $poweroff = PowerOff-VM $vm.Name
    if($poweroff -eq "Ok"){
      Write-Host "PowerOff OK"
      if($vms."Recommended Memory" -ne "0"){
       
      
         
          $vm.Name | Set-VM -MemoryGB $vms."Recommended Memory" -Confirm:$False
          Write-Host "The new configured amount of memory is"(Get-VM $VM).MemoryGB
       
      }

      if($CPUCount -ne "0"){
        $vm.Name | Set-VM -NumCPU $vms."Recommended vCPU" -Confirm:$False
        Write-Host "The new configured number of vCPU's is"(Get-VM $VM).NumCPU
      }

      $poweron = PowerOn-VM $vm.Name
      if($poweron -eq "Ok"){
        Write-Host "PowerOn OK"}
    }
  }
}

Disconnect-VIServer -Confirm:$false

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try to set up the foreach loop like this

foreach($vm in Get-VM -Name ($vms | Select -ExpandProperty "Virtual Machine")){

That way you will pass an array with names to the Name parameter


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

View solution in original post

0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

Try to set up the foreach loop like this

foreach($vm in Get-VM -Name ($vms | Select -ExpandProperty "Virtual Machine")){

That way you will pass an array with names to the Name parameter


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

0 Kudos
SteveG1836
Contributor
Contributor
Jump to solution

Luc,

  I know this is an old topic, but hopefully a quick answer. Can what Sean is trying to accomplish be done in advance (ie: right size), but only have the changes done when the VM reboots next. I can't seem to find an answer (unless of course HotAdd is enabled).

Steve

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could define an Alarm that is fired when the VM is powered off, then from the Alarm fire a script.

In that script you could make the CPU and Memory changes.

If you want to do the calculation for the new CPU and Memory configuration in advance, you can store the outcome for example in Custom Variables on the VM.

When the Alarm script runs, it only has to compare the Custom Variables content against the actual configuration, and eventually make the changes.


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

0 Kudos
SteveG1836
Contributor
Contributor
Jump to solution

Luc,

  as always you are one of the best!  Thank you.. That should work.

Steve

0 Kudos
MaheshBadge
Contributor
Contributor
Jump to solution

Hi,

I am still unable to get this working ....

Error "

-OSCustomizationSpec : Cannot validate argument on parameter 'Name'. The argument is null or empty. Supply angument that is not null or empty and then try the command again."

I am new to PowerCLI but want to get this working ....

Thanks

Mahesh

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are you sure this the correct thread, the script in this thread doesn't use the OSCustomizationSpec parameter anywhere.


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

0 Kudos