Modifying advanced virtual machine settings for logging using PowerCLI
search cancel

Modifying advanced virtual machine settings for logging using PowerCLI

book

Article ID: 376939

calendar_today

Updated On:

Products

VMware vCenter Server 7.0 VMware vCenter Server 8.0

Issue/Introduction

This article provides a comprehensive PowerCLI script designed to automate the process of enabling or disabling logging for multiple virtual machines

Environment

vCenter Server Appliance 7
vCenter Server Appliance 8

Cause

The script serves two primary use cases in virtualized environments:

  1. Enabling Logging Post-Deployment: In scenarios where virtual machines (VMs) were initially deployed with logging disabled to conserve disk space, there may later arise a need to enable logging for troubleshooting or auditing purposes. The script facilitates the activation of logging in such cases, allowing administrators to retroactively enable and configure logging without requiring a complete redeployment of the VMs.

  2. Managing Storage Constraints: In environments where storage resources are limited or under significant pressure, logging can consume valuable disk space. The script provides a solution for disabling logging to alleviate storage constraints. This is particularly useful when the storage capacity is insufficient to support extensive logging, and it allows administrators to manage and optimize storage utilization effectively.

Resolution

  1. Connect to the vCenter Server using the following command : Connect-VIServer <vcenter fqdn> 
  2. PowerCLI script :

    function Set-VMLogging{
    
     
    	[CmdletBinding(DefaultParametersetName="OnOff")]
    	param(
    	[parameter(Mandatory=$true,ValueFromPipeline=$true)]
    	[PSObject[]]$VM,
    	[Parameter(ParameterSetName="OnOff")]
    	[switch]$Logging,
    	[Parameter(ParameterSetName="Toggle")]
    	[switch]$Toggle
    	)
     
    	begin{
    		$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
    		$spec.Flags = New-Object VMware.Vim.VirtualMachineFlagInfo
    		if($psCmdlet.ParameterSetName -eq "OnOff"){
    			$spec.Flags.enableLogging = $Logging
    		}
    	}
     
    	process{
    		foreach($obj in $VM){
    			if($obj.GetType().Name -eq "string"){
    				$obj = Get-VM -Name $obj
    			}
    			if($psCmdlet.ParameterSetName -eq "OnOff"){
    				$obj | where {$_.Extensiondata.Config.Flags.enableLogging -eq (!$Logging)} | %{
    					$_.Extensiondata.ReconfigVM($spec)
    				}
    			}
    			else{
    				$spec.Flags.enableLogging = !$VM.Extensiondata.Config.Flags.enableLogging
    				$obj.Extensiondata.ReconfigVM($spec)
    			}
    		}
    	}
    }
  3. Save this script and run it.
  4. Run the following command after executing the script : Get-VM | Set-VMLogging -Logging:$true 
    (Change the value for -Logging to $false to disable logging)

NOTE : The Virtual Machine needs to be powered OFF in order to allow the script to modify the logging. In powered ON state the following error will be displayed.

On vCenter Server : 


 

On PowerCLI : 

Additional Information

Please refer to the following link for the detailed explanation of the script : Virtual Machine logging - LucD notes