<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>VirtualMachine Archives - LucD notes</title>
	<atom:link href="https://www.lucd.info/tag/virtualmachine/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.lucd.info/tag/virtualmachine/</link>
	<description>My PowerShell ramblings</description>
	<lastBuildDate>Mon, 14 Mar 2011 12:22:08 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://www.lucd.info/wp-content/uploads/2018/12/cropped-120px-Tibetan_Dharmacakra-32x32.png</url>
	<title>VirtualMachine Archives - LucD notes</title>
	<link>https://www.lucd.info/tag/virtualmachine/</link>
	<width>32</width>
	<height>32</height>
</image> 
<atom:link rel="hub" href="https://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="https://pubsubhubbub.superfeedr.com"/><atom:link rel="hub" href="https://websubhub.com/hub"/>	<item>
		<title>Virtual Machine logging</title>
		<link>https://www.lucd.info/2011/02/27/virtual-machine-logging/</link>
					<comments>https://www.lucd.info/2011/02/27/virtual-machine-logging/#comments</comments>
		
		<dc:creator><![CDATA[LucD]]></dc:creator>
		<pubDate>Sun, 27 Feb 2011 22:26:14 +0000</pubDate>
				<category><![CDATA[Logging]]></category>
		<category><![CDATA[PowerCLI]]></category>
		<category><![CDATA[Virtual Machine]]></category>
		<category><![CDATA[vSphere]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[VirtualMachine]]></category>
		<guid isPermaLink="false">http://www.lucd.info/?p=3115</guid>

					<description><![CDATA[I recently received an interesting question in my mailbox. Someone wanted to know [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>I recently received an interesting question in my mailbox. Someone wanted to know if it was possible to enable/disable the logging for a Virtual Machine through <a href="https://communities.vmware.com/community/vmtn/vsphere/automationtools/powercli?view=discussions" target="_blank">PowerCLI</a>. These Virtual Machine logs can be a handy resource when analysing problems.<br />
This logging option is available through the vSphere client when you select <strong>Edit Settings</strong> and then <strong>Options-Advanced-General</strong>. In that  form there is a checkbox that allows you to enable/disable the virtual machine logging.</p>
<p><a href="https://lucd.info/wp-content/uploads/2011/02/vsphere-client-vm-logging1.png"><img fetchpriority="high" decoding="async" class="alignnone size-full wp-image-3121" title="vsphere-client-vm-logging" src="https://lucd.info/wp-content/uploads/2011/02/vsphere-client-vm-logging1.png" alt="" width="559" height="216" srcset="https://www.lucd.info/wp-content/uploads/2011/02/vsphere-client-vm-logging1.png 699w, https://www.lucd.info/wp-content/uploads/2011/02/vsphere-client-vm-logging1-300x115.png 300w" sizes="(max-width: 559px) 100vw, 559px" /></a></p>
<p>Afaik, this feature is not yet available through a <a href="https://communities.vmware.com/community/vmtn/vsphere/automationtools/powercli?view=discussions" target="_blank">PowerCLI</a> cmdlet. But it is easily accessible through the <a href="https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.VirtualMachine.html#reconfigure" target="_blank">VirtualMachine</a> object.</p>
<p><span id="more-3115"></span></p>
<h2>Enable/disable logging</h2>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">#requires -version 2
#requires -pssnapin VMware.VimAutomation.Core -version 4.1

function Set-VMLogging{
&lt;#
.SYNOPSIS
	Activate or deactivate logging on a virtual machine
.DESCRIPTION
	The function activates or deactivates logging on a
	virtual machine. The function allows toggling of
	the current setting through the
.NOTES
	Author:  Luc Dekens
.PARAMETER VM
	The virtual machine
.PARAMETER Logging
	A switch that indicates if logging should be activated or
	deactivated
.PARAMETER Toggle
	A switch that indicates if the current logging setting should
	be toggled
.EXAMPLE
	PS&gt; Set-VMLogging -VM $vm -Logging:$true
.EXAMPLE
	PS&gt; Get-VM | Set-VMLogging -Logging:$false
.EXAMPLE
	PS&gt; Set-VMLogging -VM $vm -Toggle:$true
#&gt;

	[CmdletBinding(DefaultParametersetName=&quot;OnOff&quot;)]
	param(
	[parameter(Mandatory=$true,ValueFromPipeline=$true)]
	[PSObject[]]$VM,
	[Parameter(ParameterSetName=&quot;OnOff&quot;)]
	[switch]$Logging,
	[Parameter(ParameterSetName=&quot;Toggle&quot;)]
	[switch]$Toggle
	)

	begin{
		$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
		$spec.Flags = New-Object VMware.Vim.VirtualMachineFlagInfo
		if($psCmdlet.ParameterSetName -eq &quot;OnOff&quot;){
			$spec.Flags.enableLogging = $Logging
		}
	}

	process{
		foreach($obj in $VM){
			if($obj.GetType().Name -eq &quot;string&quot;){
				$obj = Get-VM -Name $obj
			}
			if($psCmdlet.ParameterSetName -eq &quot;OnOff&quot;){
				$obj | where {$_.Extensiondata.Config.Flags.enableLogging -eq (!$Logging)} | %{
					$_.Extensiondata.ReconfigVM($spec)
				}
			}
			else{
				$spec.Flags.enableLogging = !$VM.Extensiondata.Config.Flags.enableLogging
				$obj.Extensiondata.ReconfigVM($spec)
			}
		}
	}
}</pre><p></p>
<h4>Annotations</h4>
<p><strong>Line 34-37</strong>: To avoid that the Logging and Toggle switch be used together, I placed both of them in different parameter sets.</p>
<p><strong>Line 41-45</strong>: The <a href="https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.vm.ConfigSpec.html" target="_blank">VirtualMachineConfigSpec</a> object that we need to change the logging setting is constant for all calls. Except if the function is called with the Toggle parameter.</p>
<p><strong>Line 33,49</strong>: The function defines the VM parameter as an <strong>array</strong>, that way the script can use a foreach loop. Even if the function is called with only 1 virtual machine, the $VM variable will still be an array, albeit with only 1 element.</p>
<p><strong>Line 50-52</strong>: An simple emulation of the <strong>OBN</strong> (Object By Name) feature that the PowerCLI cmdlets offer. The virtual machines can be passed as string or as an object returned by the <a href="https://www.vmware.com/support/developer/PowerCLI/PowerCLI41U1/html/Get-VM.html" target="_blank">Get-VM</a> cmdlet.</p>
<p><strong>Line 53-61</strong>: The actual change of the logging flag is done with the <a href="https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.VirtualMachine.html#reconfigure" target="_blank">ReconfigVM_Task</a>. The function handles the Logging and the Toggle switch with the if-then-else construct.</p>
<p>While I was writing the above function I realised that it would also be handy to have a function to retreive these logs. With the help of the Copy-DatastoreItem it turns out that this is also a trivial task.</p>
<h2>Retrieve the logs</h2>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">#requires -version 2
#requires -pssnapin VMware.VimAutomation.Core -version 4.1

function Get-VMLog{
&lt;#
.SYNOPSIS
	Retrieve the virtual machine logs
.DESCRIPTION
	The function retrieves the logs from one or more
	virtual machines and stores them in a local folder
.NOTES
	Author:  Luc Dekens
.PARAMETER VM
	The virtual machine(s) for which you want to retrieve
	the logs.
.PARAMETER Path
	The folderpath where the virtual machines logs will be
	stored. The function creates a folder with the name of the
	virtual machine in the specified path.
.EXAMPLE
	PS&gt; Get-VMLog -VM $vm -Path &quot;C:\VMLogs&quot;
.EXAMPLE
	PS&gt; Get-VM | Get-VMLog -Path &quot;C:\VMLogs&quot;
#&gt;

	param(
	[parameter(Mandatory=$true,ValueFromPipeline=$true)]
	[PSObject[]]$VM,
	[parameter(Mandatory=$true)]
	[string]$Path
	)

	process{
		foreach($obj in $VM){
			if($obj.GetType().Name -eq &quot;string&quot;){
				$obj = Get-VM -Name $obj
			}
		}
		$logPath = $obj.Extensiondata.Config.Files.LogDirectory
		$dsName = $logPath.Split(']')[0].Trim('[')
		$vmPath = $logPath.Split(']')[1].Trim(' ')
		$ds = Get-Datastore -Name $dsName
		$drvName = &quot;MyDS&quot; + (Get-Random)
		New-PSDrive -Location $ds -Name $drvName -PSProvider VimDatastore -Root '\' | Out-Null
		Copy-DatastoreItem -Item ($drvName + &quot;:&quot; + $vmPath + &quot;*.log&quot;) -Destination ($Path + &quot;\&quot; + $obj.Name + &quot;\&quot;) -Force:$true
		Remove-PSDrive -Name $drvName -Confirm:$false
	}
}</pre><p></p>
<h4>Annotations</h4>
<p><strong>Line 35-37</strong>: An simple emulation of the <strong>OBN</strong> (Object By Name) feature that the  PowerCLI cmdlets offer. The virtual machines can be passed as string or  as an object returned by the <a href="https://www.vmware.com/support/developer/PowerCLI/PowerCLI41U1/html/Get-VM.html" target="_blank">Get-VM</a> cmdlet.</p>
<p><strong>Line 43</strong>: The function creates a <strong>PSDrive</strong> to map the datastore where the virtual machine logs are located. To avoid/minimise collisions with existing PSDrives, the script adds a random number to the name.</p>
<p><strong>Line 45</strong>: The actual copy of the log files is done with the <a href="https://www.vmware.com/support/developer/PowerCLI/PowerCLI41U1/html/Copy-DatastoreItem.html" target="_blank">Copy-DatastoreItem</a> cmdlet. Note that the function always uses the Force parameter, this means that any existing files will be overwritten.</p>
<h2>Sample runs</h2>
<p>To activate logging for a virtual machine, you can call the function like this</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">Set-VMLogging -VM MyVM -Logging:$true</pre><p></p>
<p>To deactivate logging for a series of virtual machines, you can do something like this</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">$vm = Get-VM -Name MyVM
Set-VMLogging -VM $vm -Logging:$false</pre><p></p>
<p>The function also provides a &#8216;toggle&#8217; switch. This parameter will change the actual setting for logging.</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">Get-VM My* | Set-VMLogging -Toggle</pre><p></p>
<p>To retrieve the logs for a virtual machine, you do</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">Get-VMLog -VM MyVM -Path &quot;C:\VMLogs&quot;</pre><p></p>
<p><a href="https://lucd.info/wp-content/uploads/2011/02/vm-log-folder.png"><img decoding="async" class="alignnone size-full wp-image-3125" title="vm-log-folder" src="https://lucd.info/wp-content/uploads/2011/02/vm-log-folder.png" alt="" width="563" height="69" srcset="https://www.lucd.info/wp-content/uploads/2011/02/vm-log-folder.png 625w, https://www.lucd.info/wp-content/uploads/2011/02/vm-log-folder-300x36.png 300w" sizes="(max-width: 563px) 100vw, 563px" /></a><br />
<strong><span style="text-decoration: underline;">Note</span> </strong>that the script assumes that the folder VMLogs already exists.</p>
<p>And you can do this for a number of virtual machines as well</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">Get-VM PC* | Get-VMLog -Path &quot;C:\VMLogs&quot;</pre><p></p>
<p>You will find the virtual machine logs in the subfolders under C:\VMLogs</p>
<p><a href="https://lucd.info/wp-content/uploads/2011/02/vm-log-folders.png"><img decoding="async" class="alignnone size-full wp-image-3127" title="vm-log-folders" src="https://lucd.info/wp-content/uploads/2011/02/vm-log-folders.png" alt="" width="495" height="124" srcset="https://www.lucd.info/wp-content/uploads/2011/02/vm-log-folders.png 550w, https://www.lucd.info/wp-content/uploads/2011/02/vm-log-folders-300x75.png 300w" sizes="(max-width: 495px) 100vw, 495px" /></a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lucd.info/2011/02/27/virtual-machine-logging/feed/</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
	</channel>
</rss>
