<?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>log Archives - LucD notes</title>
	<atom:link href="https://www.lucd.info/tag/log/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.lucd.info/tag/log/</link>
	<description>My PowerShell ramblings</description>
	<lastBuildDate>Wed, 08 Apr 2020 11:03:30 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.10</generator>

<image>
	<url>https://www.lucd.info/wp-content/uploads/2018/12/cropped-120px-Tibetan_Dharmacakra-32x32.png</url>
	<title>log Archives - LucD notes</title>
	<link>https://www.lucd.info/tag/log/</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>Get your tasklog here!</title>
		<link>https://www.lucd.info/2010/11/04/get-your-tasklog-here/</link>
					<comments>https://www.lucd.info/2010/11/04/get-your-tasklog-here/#comments</comments>
		
		<dc:creator><![CDATA[LucD]]></dc:creator>
		<pubDate>Thu, 04 Nov 2010 15:11:19 +0000</pubDate>
				<category><![CDATA[ESX]]></category>
		<category><![CDATA[PowerCLI]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[vSphere]]></category>
		<category><![CDATA[log]]></category>
		<guid isPermaLink="false">http://www.lucd.info/?p=2743</guid>

					<description><![CDATA[It happens quite often that you launch a PowerCLI cmdlet or a call [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>It happens quite often that you launch a PowerCLI cmdlet or a call to a SDK method or a script and that you get a rather cryptic message that something went wrong. One source of information to find out what went wrong are the vCenter&#8217;s <strong>vpxd</strong> log or the ESX(i) server&#8217;s <strong>hostd</strong> log.</p>
<p>But these logs are flooded with messages and it&#8217;s often quite hard to find the messages that relate to your task.</p>
<p>When running against a vCenter Server you can set the vCenter&#8217;s <strong>Logging Options</strong> to &#8220;<em>Verbose</em>&#8221; or &#8220;<em>Trivia</em>&#8220;. That produces more information in the logs but that often also makes it harder to find/extract the information that belongs to your task. And who has never forgotten to set the the Logging Options back to their original state when done with debugging ?</p>
<p>To make life a bit easier, I wrote a function that automates the above steps. I expanded on a function that I was using privately, and made it more general. This function allows you to retrieve the log entries for one specific or all recent task. The function also allows you to pass a cmdlet, or even a complete script, run the script in an elevated Logging Options mode and returns the task-related log entries.</p>
<h2><span id="more-2743"></span>The script</h2>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">#requires -version 2
#requires -pssnapin VMware.VimAutomation.Core -version 4.1

function Get-TaskLog{
&lt;#
.SYNOPSIS
	Returns the log entries related to one or more tasks
.DESCRIPTION
	The function will extract from the log all entries from one or
	more tasks.
.NOTES
	Author:  Luc Dekens
.PARAMETER TaskId
	Specifies the ID of one specific task for which the function
	shall return the log entries.
.PARAMETER ScriptText
	The function accepts a script block which it will execute and
	then return the related log entries.
.PARAMETER LogLevel
	Specifies the logging mode in which the script block shall be
	executed.
.EXAMPLE
	PS&gt; $log = Get-TaskLog
.EXAMPLE
	PS&gt; $log = Get-TaskLog -TaskId &quot;task-6738&quot;
.EXAMPLE
	PS&gt; $cmd = {Get-VM PC1 | New-HardDisk -CapacityKB 1024 -ThinProvisioned:$true}
	PS&gt; $log = Get-TaskLog -ScriptText $cmd
.EXAMPLE
	PS&gt; $log = Get-TaskLog $cmd -LogLevel &quot;verbose&quot;
#&gt;
	[CmdletBinding(DefaultParametersetName=&quot;TaskId&quot;)]
	param(
	[parameter(ParameterSetName = &quot;TaskId&quot;,Position=0)]
	[string]$TaskId = $null,
	[parameter(ParameterSetName = &quot;Script&quot;,Position=0)]
	[scriptblock]$ScriptText = $null,
	[ValidateSet('verbose','warning','info')]
	[string]$LogLevel = $null
	)

	begin{
		function Set-VCLogLevel{
			param(
			$LogLevel
			)

			$optMgr = Get-View -Id 'OptionManager-VpxSettings'
			$currentLogLevel = ($optMgr.Setting | where{$_.Key -eq &quot;log.level&quot;}).Value
			if($currentLogLevel -ne $LogLevel){
				$setLog = New-Object VMware.Vim.OptionValue[] (1)
				$setLog[0] = New-Object VMware.Vim.OptionValue
				$setLog[0].key = &quot;log.level&quot;
				$setLog[0].value = $LogLevel
				$optMgr.UpdateOptions($setLog)
			}
			$currentLogLevel
		}

		function Get-VpxdLog{
			param($taskId)

			process{
				$taskIdPattern = [regex]&quot;(?i)opID=(\S+)]&quot;
				$taskTaskPattern = [regex]&quot;BEGIN (task-\d+)\s&quot;
				$taskEntityPattern = [regex]&quot;BEGIN task-\d+\s--\s([a-z]+-[0-9]+)\s&quot;

				$logDef = Get-LogType | where{$_.Key -match &quot;vpxd:vpxd-\d+.log&quot;} | Select -Last 1
				$log = Get-Log -Key $logDef.Key

				$prevTask = &quot;#dummy#&quot;
				$log.Entries | where{$_ -match &quot;BEGIN task-\d+&quot;} | %{
					if((!$taskId -or ($taskId -and $_.Contains($taskId))) -and !$_.Contains($prevTask)){
						$entityMoRef = New-Object VMware.Vim.ManagedObjectReference
						$entityMoRef.Value = $taskEntityPattern.Match($_).Groups[1].Value
						if($entityMoRef.Value -like &quot;host*&quot;){$entityMoRef.Type = &quot;HostSystem&quot;}
						elseif($entityMoRef.Value -like &quot;vm*&quot;){$entityMoRef.Type = &quot;VirtualMachine&quot;}

						$row = &quot;&quot; | Select Task,Id,Entity,Log
						$row.Task = $taskTaskPattern.Match($_).Groups[1].Value
						$prevTask = $row.Task
						$row.Id = $taskIdPattern.Match($_).Groups[1].Value
						$row.Entity = Get-View $entityMoRef
						$row.Log = &amp;{
							$extra = $false
							$log.Entries | where {$extra -or ($_ -match $row.Id)} | %{
								if($extra -and $_.StartsWith(&quot;[&quot;)){
									$extra = $false
								}
								if($_.Contains(&quot;ERROR &quot; + $row.$Task)){
									$extra = $true
								}
								$_
							}
						}
						$row
					}
				}
			}
		}

		function Get-EsxLog{
			param($taskId)

			process{
				$taskIdPattern = [regex]&quot;(?i)opID=([0-9a-z-]+)&quot;
				$taskTaskPattern = [regex]&quot;-(\d+)$&quot;

				$logDef = Get-LogType | where{$_.Key -match &quot;hostd&quot;}
				$log = Get-Log -Key $logDef.Key

				$prevTask = &quot;#dummy#&quot;
				foreach($line in ($log.Entries | where{$_ -match &quot;Task Created : haTask-&quot;})){
					if((!$taskId -or ($taskId -and $line.Contains($taskId))) -and !$line.Contains($prevTask)){
						$row = &quot;&quot; | Select Task,Id,Entity,Log
						$row.Id = $taskIdPattern.Match($line).Groups[1].Value
						if(!$row.Id){
							continue
						}
						$row.Task = $taskTaskPattern.Match($line).Groups[1].Value
						$prevTask = $row.Task
						$row.Entity = $null
						$row.Log = &amp;{
							$extra = $false
							$log.Entries | where {$extra -or ($_ -match $row.Id)} | %{
								if(!$extra -and $_.EndsWith(&quot;{&quot;)){
									$extra = $true
								}
								elseif($_.StartsWith(&quot;}&quot;)){
									$extra = $false
								}
								$_
							}
						}
						$row
					}
				}
			}
		}
	}

	process{
		if(!$defaultVIServer){
			Write-Error -Message &quot;You must be connected to a vSphere server!&quot; -RecommendedAction &quot;Use Connect-VIServer&quot;
			return
		}

		switch($defaultVIserver.ProductLine){
			&quot;vpx&quot; {
				if($PsCmdlet.ParameterSetName -eq &quot;Script&quot;){
					if($LogLevel){
						$prevLogLevel = Set-VCLogLevel $LogLevel
					}
					$scriptOutput = $ScriptText.Invoke()
					$TaskId = @(Get-Task)[-1].ExtensionData.Info.Key
					if($LogLevel){
						Set-VCLogLevel $prevLogLevel | Out-Null
					}
				}

				Get-VpxdLog $taskId | Add-Member -Name &quot;ScriptOutput&quot; -Value $scriptOutput -MemberType &quot;NoteProperty&quot; -PassThru:$true
			}
			&quot;esx&quot; {
				Get-EsxLog $taskId
			}
		}
	}
}</pre><p></p>
<h4>Annotations</h4>
<p><strong>Line 1-2</strong>: The script requires PowerShell v2 and PowerCLI 4.1</p>
<p><strong>Line 32</strong>: The default parameter set is the &#8220;TaskId&#8221; set. This definition is required to allow a call to the function without any parameters specified. In that case the function will assume that the TaskId parameter contains $null and the function will return the log entries for all tasks.</p>
<p><strong>Line 34-35</strong>: The TaskId parameter set.</p>
<p><strong>Line 36-39</strong>: The Script parameter set. This set allows the caller to pass a script block to the function and it optionally allows the caller to specify a new logging level for the execution of the script block.</p>
<p><strong>Line 38</strong>: The ValidateSet defines the accepted values for the LogLevel parameter.</p>
<p><strong>Line 43-58</strong>: An internal helper function. It changes the logging level. Note that this only valid when using a vCenter connection. The function returns the logging level before the change.</p>
<p><strong>Line 60-100</strong>: An internal function that extracts the required information from a <strong>vpxd</strong> log (vCenter connection).</p>
<p><strong>Line 64-66</strong>: RegEx expression to easily extract the required fields from the lines in the vpxd log.</p>
<p><strong>Line 68-69</strong>: Use the current vpxd log.</p>
<p><strong>Line 72</strong>: Find the <span style="background-color: #ffff00;">start of each Task</span>. Note that the script doesn&#8217;t look at the internal tasks created by vCenter.</p>
<p><a href="https://lucd.info/wp-content/uploads/2010/11/tasklog-vpx-task-start.png"><img fetchpriority="high" decoding="async" class="alignnone size-full wp-image-2760" title="tasklog-vpx-task-start" src="https://lucd.info/wp-content/uploads/2010/11/tasklog-vpx-task-start.png" alt="" width="842" height="146" srcset="https://www.lucd.info/wp-content/uploads/2010/11/tasklog-vpx-task-start.png 1052w, https://www.lucd.info/wp-content/uploads/2010/11/tasklog-vpx-task-start-300x51.png 300w, https://www.lucd.info/wp-content/uploads/2010/11/tasklog-vpx-task-start-1024x177.png 1024w" sizes="(max-width: 842px) 100vw, 842px" /></a></p>
<p><strong>Line 74-77</strong>: From the vpxd log entry we can construct a <span style="background-color: #ccffcc;">MoRef</span>, that will allow the script to get the Managed Object against which the task was executed.</p>
<p><strong>Line 84-95</strong>: This loop extract all the lines that are produced by the <span style="background-color: #ff99cc;">current task</span>.</p>
<p><strong>Line 102-140</strong>: An internal function that extracts the required information from a hostd log (ESX(i) connection)</p>
<p><strong>Line 106-107</strong>: RegEx expression to easily extract the required fields from the lines in the vpxd log.</p>
<p><strong>Line 113</strong>: Find the <span style="background-color: #ffff00;">start of each Task</span>. Note that the script doesn&#8217;t look at the internal tasks.</p>
<p><a href="https://lucd.info/wp-content/uploads/2010/11/tasklog-esx-task-start.png"><img decoding="async" class="alignnone size-full wp-image-2762" title="tasklog-esx-task-start" src="https://lucd.info/wp-content/uploads/2010/11/tasklog-esx-task-start.png" alt="" width="826" height="148" srcset="https://www.lucd.info/wp-content/uploads/2010/11/tasklog-esx-task-start.png 1032w, https://www.lucd.info/wp-content/uploads/2010/11/tasklog-esx-task-start-300x53.png 300w, https://www.lucd.info/wp-content/uploads/2010/11/tasklog-esx-task-start-1024x183.png 1024w" sizes="(max-width: 826px) 100vw, 826px" /></a></p>
<p><strong>Line 117-119</strong>: The hostd log also contains entries for some internal tasks, like for example the autoPowerOn task after a system start, that we don&#8217;t return in the results. This test skips those entries.</p>
<p><strong>Line 122</strong>: From the hostd log the script can&#8217;t find the Managed Object against which the task was executed.</p>
<p><strong>Line 123-134</strong>: This loop extract all the lines that are produced by the <span style="background-color: #ff99cc;">current task</span>.</p>
<p><strong>Line 143-146</strong>: The function needs a connection.</p>
<p><strong>Line 148</strong>: The Switch statement checks if the connection is to a <strong>vCenter</strong> or an <strong>ESX(i)</strong> server.</p>
<p><strong>Line 150-159</strong>: When the connection is to a vCenter server, the script checks which parameter set is used in the call. If the call used the <strong>Script</strong> parameter set, the script optionally changes the logging level, executes the script block and then sets the logging level back to the original value. See <a href="https://blogs.msdn.com/b/powershell/archive/2008/12/23/powershell-v2-parametersets.aspx" target="_blank" rel="noopener noreferrer">PowerShell V2: ParameterSets</a> for more info on parameter sets.</p>
<p><strong>Line 155</strong>: The script gets the task-id for the script block that was invoked. This task-id will be used in the subsequent call to extract the log entries. Notice that the result from the <strong>Get-Task</strong> cmdlet is converted to an array. This to allow retrieving the last entry via the <strong>-1 index</strong>, even when there is only 1 task returned.</p>
<p><strong>Line 154,161</strong>: The output from the executed script is captured and returned in the <strong>ScriptOutput</strong> property.</p>
<h2>Samples</h2>
<h3>Get all the tasks</h3>
<p>This is the simplest form to call the function.</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">Get-TaskLog</pre><p></p>
<p>The function returns an array of objects, one element for each task. This call is useful to get an overview of the tasks that are found in the log.</p>
<p>I mostly the following small script to display the results in screen.</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">Get-TaskLog | %{
	Write-Host $_.Task $_.Entity.Name
	$_.Log | Select -First 3 | %{
		Write-Host &quot;`t&quot; $_
	}
}</pre><p></p>
<p>When connected to a <strong>vCenter Server</strong> this produces output like this</p>
<p><a href="https://lucd.info/wp-content/uploads/2010/11/tasklog-vpx-all-tasks.png"><img decoding="async" class="alignnone size-full wp-image-2766" title="tasklog-vpx-all-tasks" src="https://lucd.info/wp-content/uploads/2010/11/tasklog-vpx-all-tasks.png" alt="" width="1105" height="112" srcset="https://www.lucd.info/wp-content/uploads/2010/11/tasklog-vpx-all-tasks.png 1105w, https://www.lucd.info/wp-content/uploads/2010/11/tasklog-vpx-all-tasks-300x30.png 300w, https://www.lucd.info/wp-content/uploads/2010/11/tasklog-vpx-all-tasks-1024x103.png 1024w" sizes="(max-width: 1105px) 100vw, 1105px" /></a><br />
When connected to an <strong>ESX server</strong>, the result looks like this. Notice that in this case we don&#8217;t have the entity against which the task was executed.</p>
<p><a href="https://lucd.info/wp-content/uploads/2010/11/tasklog-esx-all-tasks.png"><img loading="lazy" decoding="async" class="alignnone size-full wp-image-2768" title="tasklog-esx-all-tasks" src="https://lucd.info/wp-content/uploads/2010/11/tasklog-esx-all-tasks.png" alt="" width="953" height="112" srcset="https://www.lucd.info/wp-content/uploads/2010/11/tasklog-esx-all-tasks.png 953w, https://www.lucd.info/wp-content/uploads/2010/11/tasklog-esx-all-tasks-300x35.png 300w" sizes="auto, (max-width: 953px) 100vw, 953px" /></a></p>
<h3>Get a specific task</h3>
<p>You can find the task number from the <a href="https://www.vmware.com/support/developer/PowerCLI/PowerCLI41/html/Get-Task.html" target="_blank" rel="noopener noreferrer">Get-Task</a> cmdlet or from the list we obtained in the previous section.</p>
<p>A typical call, when connected to the <strong>vCenter</strong>, looks like this</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">Get-TaskLog -TaskId task-7244 | Select -ExpandProperty Log</pre><p></p>
<p><a href="https://lucd.info/wp-content/uploads/2010/11/tasklog-vpx-one-task.png"><img loading="lazy" decoding="async" class="alignnone size-full wp-image-2771" title="tasklog-vpx-one-task" src="https://lucd.info/wp-content/uploads/2010/11/tasklog-vpx-one-task.png" alt="" width="1213" height="99" srcset="https://www.lucd.info/wp-content/uploads/2010/11/tasklog-vpx-one-task.png 1213w, https://www.lucd.info/wp-content/uploads/2010/11/tasklog-vpx-one-task-300x24.png 300w, https://www.lucd.info/wp-content/uploads/2010/11/tasklog-vpx-one-task-1024x83.png 1024w" sizes="auto, (max-width: 1213px) 100vw, 1213px" /></a></p>
<p>And when you want to retrieve the log from an <strong>ESX server</strong>.</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">Get-TaskLog -TaskId 231 | Select -ExpandProperty Log -First 5</pre><p></p>
<p><a href="https://lucd.info/wp-content/uploads/2010/11/tasklog-esx-one-task.png"><img loading="lazy" decoding="async" class="alignnone size-full wp-image-2769" title="tasklog-esx-one-task" src="https://lucd.info/wp-content/uploads/2010/11/tasklog-esx-one-task.png" alt="" width="1105" height="39" srcset="https://www.lucd.info/wp-content/uploads/2010/11/tasklog-esx-one-task.png 1381w, https://www.lucd.info/wp-content/uploads/2010/11/tasklog-esx-one-task-300x10.png 300w, https://www.lucd.info/wp-content/uploads/2010/11/tasklog-esx-one-task-1024x36.png 1024w" sizes="auto, (max-width: 1105px) 100vw, 1105px" /></a></p>
<h3>Run a task</h3>
<p>The previous sections showed how you can trieve one or more of the tasks that ran on your vCenter or ESX server. But the function also allows you to run a script for you and optionally set the logging level on a vCenter server to a more verbose level.</p>
<p>You pass a script block to the function like this.</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">$cmd = {
	Get-VM PC1 | New-HardDisk -CapacityKB (2048 * 1KB) -ThinProvisioned:$true | Out-Null
}

Get-TaskLog -ScriptText $cmd | Select -ExpandProperty Log -First 5</pre><p></p>
<p>Note that we redirect the output of the cmdlets in the script block to $null. If we didn&#8217;t do that, the cmdlet&#8217;s output would also appear in the $log variable.</p>
<p>The result looks the same as what we got with the previous calls.</p>
<p><a href="https://lucd.info/wp-content/uploads/2010/11/tasklog-vpx-one-task-script.png"><img loading="lazy" decoding="async" class="alignnone size-full wp-image-2775" title="tasklog-vpx-one-task-script" src="https://lucd.info/wp-content/uploads/2010/11/tasklog-vpx-one-task-script.png" alt="" width="913" height="78" srcset="https://www.lucd.info/wp-content/uploads/2010/11/tasklog-vpx-one-task-script.png 1141w, https://www.lucd.info/wp-content/uploads/2010/11/tasklog-vpx-one-task-script-300x25.png 300w, https://www.lucd.info/wp-content/uploads/2010/11/tasklog-vpx-one-task-script-1024x87.png 1024w" sizes="auto, (max-width: 913px) 100vw, 913px" /></a></p>
<p>But we can also ask for more information with the <strong>-LogLevel</strong> parameter.</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">$cmd = {
	Get-VM PC1 | New-HardDisk -CapacityKB (2048 * 1KB) -ThinProvisioned:$true | Out-Null
}

Get-TaskLog -ScriptText $cmd | Select -ExpandProperty Log</pre><p></p>
<p>Now we see a lot more.</p>
<p><a href="https://lucd.info/wp-content/uploads/2010/11/tasklog-vpx-one-task-script-verbose.png"><img loading="lazy" decoding="async" class="alignnone size-full wp-image-2777" title="tasklog-vpx-one-task-script-verbose" src="https://lucd.info/wp-content/uploads/2010/11/tasklog-vpx-one-task-script-verbose.png" alt="" width="967" height="386" srcset="https://www.lucd.info/wp-content/uploads/2010/11/tasklog-vpx-one-task-script-verbose.png 1209w, https://www.lucd.info/wp-content/uploads/2010/11/tasklog-vpx-one-task-script-verbose-300x119.png 300w, https://www.lucd.info/wp-content/uploads/2010/11/tasklog-vpx-one-task-script-verbose-1024x409.png 1024w" sizes="auto, (max-width: 967px) 100vw, 967px" /></a></p>
<p>This kind of task log not only gives you an interesting way of finding problems in your code, but it also shows you that there is a lot going on behind the scene, that you normally wouldn&#8217;t see.</p>
<p>Enjoy !</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lucd.info/2010/11/04/get-your-tasklog-here/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
