<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
	
	>
<channel>
	<title>
	Comments on: Events – Part 5 : Powered off for more than 1 week ?	</title>
	<atom:link href="https://www.lucd.info/2010/02/09/events-part-5-powered-of-for-more-than-1-week/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.lucd.info/2010/02/09/events-part-5-powered-of-for-more-than-1-week/</link>
	<description>My PowerShell ramblings</description>
	<lastBuildDate>Thu, 16 Sep 2021 16:35:47 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.9</generator>
<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>
		By: LucD		</title>
		<link>https://www.lucd.info/2010/02/09/events-part-5-powered-of-for-more-than-1-week/#comment-8224</link>

		<dc:creator><![CDATA[LucD]]></dc:creator>
		<pubDate>Thu, 16 Sep 2021 16:35:47 +0000</pubDate>
		<guid isPermaLink="false">http://lucd.info/?p=1753#comment-8224</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.lucd.info/2010/02/09/events-part-5-powered-of-for-more-than-1-week/#comment-8222&quot;&gt;Joy&lt;/a&gt;.

Are you sure there is something in $vms?]]></description>
			<content:encoded><![CDATA[<p>Are you sure there is something in $vms?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: LucD		</title>
		<link>https://www.lucd.info/2010/02/09/events-part-5-powered-of-for-more-than-1-week/#comment-8223</link>

		<dc:creator><![CDATA[LucD]]></dc:creator>
		<pubDate>Thu, 16 Sep 2021 16:34:13 +0000</pubDate>
		<guid isPermaLink="false">http://lucd.info/?p=1753#comment-8223</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.lucd.info/2010/02/09/events-part-5-powered-of-for-more-than-1-week/#comment-8221&quot;&gt;Joy&lt;/a&gt;.

Hi Joy,
No, this snippet doesn&#039;t only get the most recent power off.

But if you want to find the VMs that have been powered off 90 days or more ago, it would be a matter of &lt;strong&gt;not&lt;/strong&gt; finding any Power Off events for that VM.
Something like this for example
&lt;code&gt;
$start = (Get-Date).AddDays(-90)

Get-VM &#124; where { $_.PowerState -eq &#039;PoweredOff&#039; } &#124;
ForEach-Object -Process {
    if (-not (Get-VIEvent -Start $start -Entity $_ &#124; where { $_.FullFormattedMessage -like &quot;*is powered off&quot; })) {
        Write-Host &quot;VM $($_.Name) was last powered off before $($start.ToString())&quot;
    }
}
&lt;/code&gt;

Unless you mean something different?
Luc]]></description>
			<content:encoded><![CDATA[<p>Hi Joy,<br />
No, this snippet doesn&#8217;t only get the most recent power off.</p>
<p>But if you want to find the VMs that have been powered off 90 days or more ago, it would be a matter of <strong>not</strong> finding any Power Off events for that VM.<br />
Something like this for example<br />
<code><br />
$start = (Get-Date).AddDays(-90)</p>
<p>Get-VM | where { $_.PowerState -eq 'PoweredOff' } |<br />
ForEach-Object -Process {<br />
    if (-not (Get-VIEvent -Start $start -Entity $_ | where { $_.FullFormattedMessage -like "*is powered off" })) {<br />
        Write-Host "VM $($_.Name) was last powered off before $($start.ToString())"<br />
    }<br />
}<br />
</code></p>
<p>Unless you mean something different?<br />
Luc</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Joy		</title>
		<link>https://www.lucd.info/2010/02/09/events-part-5-powered-of-for-more-than-1-week/#comment-8222</link>

		<dc:creator><![CDATA[Joy]]></dc:creator>
		<pubDate>Thu, 16 Sep 2021 15:58:36 +0000</pubDate>
		<guid isPermaLink="false">http://lucd.info/?p=1753#comment-8222</guid>

					<description><![CDATA[Luc,

What does this mean?

PowerCLI C:\VMware_scripts&#062; $events = Get-VIEvent -Start (Get-Date).AddDays(-90) -Entity $vms
Get-VIEvent : 9/16/2021 9:54:25 AM      Get-VIEvent             Exception has been thrown by the target of an invocation.
At line:1 char:11
+ $events = Get-VIEvent -Start (Get-Date).AddDays(-90) -Entity $vms
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-VIEvent], VimException
    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetEvent]]></description>
			<content:encoded><![CDATA[<p>Luc,</p>
<p>What does this mean?</p>
<p>PowerCLI C:\VMware_scripts&gt; $events = Get-VIEvent -Start (Get-Date).AddDays(-90) -Entity $vms<br />
Get-VIEvent : 9/16/2021 9:54:25 AM      Get-VIEvent             Exception has been thrown by the target of an invocation.<br />
At line:1 char:11<br />
+ $events = Get-VIEvent -Start (Get-Date).AddDays(-90) -Entity $vms<br />
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />
    + CategoryInfo          : NotSpecified: (:) [Get-VIEvent], VimException<br />
    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetEvent</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Joy		</title>
		<link>https://www.lucd.info/2010/02/09/events-part-5-powered-of-for-more-than-1-week/#comment-8221</link>

		<dc:creator><![CDATA[Joy]]></dc:creator>
		<pubDate>Thu, 16 Sep 2021 15:11:48 +0000</pubDate>
		<guid isPermaLink="false">http://lucd.info/?p=1753#comment-8221</guid>

					<description><![CDATA[Luc,

Does your base code get the most recent powered off date? For example, what happens if someone powered on the VM for a few days then powered it off again?

I&#039;m using the number of days as 90. I don&#039;t want to include those VMs that were powered off 90 days ago and were powered on let&#039;s say at 45 days ago then powered off at 30 days ago.]]></description>
			<content:encoded><![CDATA[<p>Luc,</p>
<p>Does your base code get the most recent powered off date? For example, what happens if someone powered on the VM for a few days then powered it off again?</p>
<p>I&#8217;m using the number of days as 90. I don&#8217;t want to include those VMs that were powered off 90 days ago and were powered on let&#8217;s say at 45 days ago then powered off at 30 days ago.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: LucD		</title>
		<link>https://www.lucd.info/2010/02/09/events-part-5-powered-of-for-more-than-1-week/#comment-4811</link>

		<dc:creator><![CDATA[LucD]]></dc:creator>
		<pubDate>Mon, 09 Mar 2020 18:56:53 +0000</pubDate>
		<guid isPermaLink="false">http://lucd.info/?p=1753#comment-4811</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.lucd.info/2010/02/09/events-part-5-powered-of-for-more-than-1-week/#comment-4808&quot;&gt;Joy Mamer&lt;/a&gt;.

Hi Joy,
Did you declare the $report array?
And you have to send the content to the output after the loop.

Something like this for example
&lt;code&gt;
$VMs = Get-VM &#124; Where-Object { $_.powerstate -eq &quot;poweredoff&quot; }
$Datastores = Get-Datastore &#124; Select-Object Name, Id
 
$report = @()

Get-VIEvent -Start (Get-Date).AddDays(-180) &#124; Where-Object { $_.FullFormattedMessage -like &quot;*is powered off&quot; } &#124;
Group-Object -Property { $_.Vm.Name } &#124; ForEach-Object {
    $lastPO = $_.Group &#124; Sort-Object -Property CreatedTime -Descending &#124; Select-Object -First 1
    $vm = Get-VIObjectByVIView -MORef $_.Group[0].VM.VM
    $row = &#039;&#039; &#124; Select-Object VMName, Powerstate, OS, Host, Cluster, Datastore, NumCPU, MemMb, DiskGb, PowerOFF
    $row.VMName = $vm.Name
    $row.Powerstate = $vm.Powerstate
    $row.OS = $vm.Guest.OSFullName
    $row.Host = $vm.VMHost.name
    $row.Cluster = $vm.VMHost.Parent.Name
    $row.Datastore = $Datastores &#124; Where-Object { $_.Id -eq ($vm.DatastoreIdList &#124; Select-Object -First 1) } &#124; Select-Object -ExpandProperty Name
    $row.NumCPU = $vm.NumCPU
    $row.MemMb = $vm.MemoryMB
    $row.DiskGb = Get-HardDisk -VM $vm &#124; Measure-Object -Property CapacityGB -Sum &#124; Select-Object -ExpandProperty Sum
    $row.PowerOFF = $lastPO.CreatedTime
    $report += $row
}
$report
&lt;/code&gt;]]></description>
			<content:encoded><![CDATA[<p>Hi Joy,<br />
Did you declare the $report array?<br />
And you have to send the content to the output after the loop.</p>
<p>Something like this for example<br />
<code><br />
$VMs = Get-VM | Where-Object { $_.powerstate -eq "poweredoff" }<br />
$Datastores = Get-Datastore | Select-Object Name, Id</p>
<p>$report = @()</p>
<p>Get-VIEvent -Start (Get-Date).AddDays(-180) | Where-Object { $_.FullFormattedMessage -like "*is powered off" } |<br />
Group-Object -Property { $_.Vm.Name } | ForEach-Object {<br />
    $lastPO = $_.Group | Sort-Object -Property CreatedTime -Descending | Select-Object -First 1<br />
    $vm = Get-VIObjectByVIView -MORef $_.Group[0].VM.VM<br />
    $row = '' | Select-Object VMName, Powerstate, OS, Host, Cluster, Datastore, NumCPU, MemMb, DiskGb, PowerOFF<br />
    $row.VMName = $vm.Name<br />
    $row.Powerstate = $vm.Powerstate<br />
    $row.OS = $vm.Guest.OSFullName<br />
    $row.Host = $vm.VMHost.name<br />
    $row.Cluster = $vm.VMHost.Parent.Name<br />
    $row.Datastore = $Datastores | Where-Object { $_.Id -eq ($vm.DatastoreIdList | Select-Object -First 1) } | Select-Object -ExpandProperty Name<br />
    $row.NumCPU = $vm.NumCPU<br />
    $row.MemMb = $vm.MemoryMB<br />
    $row.DiskGb = Get-HardDisk -VM $vm | Measure-Object -Property CapacityGB -Sum | Select-Object -ExpandProperty Sum<br />
    $row.PowerOFF = $lastPO.CreatedTime<br />
    $report += $row<br />
}<br />
$report<br />
</code></p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Joy Mamer		</title>
		<link>https://www.lucd.info/2010/02/09/events-part-5-powered-of-for-more-than-1-week/#comment-4808</link>

		<dc:creator><![CDATA[Joy Mamer]]></dc:creator>
		<pubDate>Mon, 09 Mar 2020 18:48:13 +0000</pubDate>
		<guid isPermaLink="false">http://lucd.info/?p=1753#comment-4808</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.lucd.info/2010/02/09/events-part-5-powered-of-for-more-than-1-week/#comment-330&quot;&gt;LucD&lt;/a&gt;.

Luc,
I wanted to incoporate resources used. 


$VMs = get-vm &#124;Where-object {$_.powerstate -eq &quot;poweredoff&quot;}

$Datastores = Get-Datastore &#124; select Name, Id

 

Get-VIEvent -Start (Get-Date).AddDays(-180) &#124; where{$_.FullFormattedMessage -like &quot;*is powered off&quot;} &#124;

Group-Object -Property {$_.Vm.Name} &#124; %{

  $lastPO = $_.Group &#124; Sort-Object -Property CreatedTime -Descending &#124; Select -First 1

  $vm = Get-VIObjectByVIView -MORef $_.Group[0].VM.VM

  $row = &#039;&#039; &#124; select VMName,Powerstate,OS,Host,Cluster,Datastore,NumCPU,MemMb,DiskGb,PowerOFF

    $row.VMName = $vm.Name

    $row.Powerstate = $vm.Powerstate

    $row.OS = $vm.Guest.OSFullName

    $row.Host = $vm.VMHost.name

    $row.Cluster = $vm.VMHost.Parent.Name

    $row.Datastore = $Datastores &#124; Where{$_.Id -eq ($vm.DatastoreIdList &#124; select -First 1)} &#124; Select -ExpandProperty Name

    $row.NumCPU = $vm.NumCPU

    $row.MemMb = $vm.MemoryMB

    $row.DiskGb = Get-HardDisk -VM $vm &#124; Measure-Object -Property CapacityGB -Sum &#124; select -ExpandProperty Sum

    $row.PowerOFF = $lastPO.CreatedTime

    $report += $row

}

My script gave me a zero byte file // failed.]]></description>
			<content:encoded><![CDATA[<p>Luc,<br />
I wanted to incoporate resources used. </p>
<p>$VMs = get-vm |Where-object {$_.powerstate -eq &#8220;poweredoff&#8221;}</p>
<p>$Datastores = Get-Datastore | select Name, Id</p>
<p>Get-VIEvent -Start (Get-Date).AddDays(-180) | where{$_.FullFormattedMessage -like &#8220;*is powered off&#8221;} |</p>
<p>Group-Object -Property {$_.Vm.Name} | %{</p>
<p>  $lastPO = $_.Group | Sort-Object -Property CreatedTime -Descending | Select -First 1</p>
<p>  $vm = Get-VIObjectByVIView -MORef $_.Group[0].VM.VM</p>
<p>  $row = &#8221; | select VMName,Powerstate,OS,Host,Cluster,Datastore,NumCPU,MemMb,DiskGb,PowerOFF</p>
<p>    $row.VMName = $vm.Name</p>
<p>    $row.Powerstate = $vm.Powerstate</p>
<p>    $row.OS = $vm.Guest.OSFullName</p>
<p>    $row.Host = $vm.VMHost.name</p>
<p>    $row.Cluster = $vm.VMHost.Parent.Name</p>
<p>    $row.Datastore = $Datastores | Where{$_.Id -eq ($vm.DatastoreIdList | select -First 1)} | Select -ExpandProperty Name</p>
<p>    $row.NumCPU = $vm.NumCPU</p>
<p>    $row.MemMb = $vm.MemoryMB</p>
<p>    $row.DiskGb = Get-HardDisk -VM $vm | Measure-Object -Property CapacityGB -Sum | select -ExpandProperty Sum</p>
<p>    $row.PowerOFF = $lastPO.CreatedTime</p>
<p>    $report += $row</p>
<p>}</p>
<p>My script gave me a zero byte file // failed.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: LucD		</title>
		<link>https://www.lucd.info/2010/02/09/events-part-5-powered-of-for-more-than-1-week/#comment-4412</link>

		<dc:creator><![CDATA[LucD]]></dc:creator>
		<pubDate>Fri, 16 Feb 2018 10:26:50 +0000</pubDate>
		<guid isPermaLink="false">http://lucd.info/?p=1753#comment-4412</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.lucd.info/2010/02/09/events-part-5-powered-of-for-more-than-1-week/#comment-4408&quot;&gt;Ayoub&lt;/a&gt;.

Hi,
I&#039;m afraid there is nothing besides the events and tasks.
If these are not kept sufficiently long, you&#039;re out of luck with Get-VIEvent I&#039;m afraid.

There is a possibility to look into the vmware.log file of the VM.
That should allow you to find the latest timestamp of a poweroff.
Luc]]></description>
			<content:encoded><![CDATA[<p>Hi,<br />
I&#8217;m afraid there is nothing besides the events and tasks.<br />
If these are not kept sufficiently long, you&#8217;re out of luck with Get-VIEvent I&#8217;m afraid.</p>
<p>There is a possibility to look into the vmware.log file of the VM.<br />
That should allow you to find the latest timestamp of a poweroff.<br />
Luc</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Ayoub		</title>
		<link>https://www.lucd.info/2010/02/09/events-part-5-powered-of-for-more-than-1-week/#comment-4408</link>

		<dc:creator><![CDATA[Ayoub]]></dc:creator>
		<pubDate>Wed, 07 Feb 2018 10:07:48 +0000</pubDate>
		<guid isPermaLink="false">http://lucd.info/?p=1753#comment-4408</guid>

					<description><![CDATA[HI LUCD ,
how to tackle this if the VM powered off since a long time ago?

in this case, the Get-VIEvent command is not useful at all .
please, could you advise? thank you for all your great effort]]></description>
			<content:encoded><![CDATA[<p>HI LUCD ,<br />
how to tackle this if the VM powered off since a long time ago?</p>
<p>in this case, the Get-VIEvent command is not useful at all .<br />
please, could you advise? thank you for all your great effort</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Loc Huynh		</title>
		<link>https://www.lucd.info/2010/02/09/events-part-5-powered-of-for-more-than-1-week/#comment-4363</link>

		<dc:creator><![CDATA[Loc Huynh]]></dc:creator>
		<pubDate>Thu, 12 Oct 2017 01:12:09 +0000</pubDate>
		<guid isPermaLink="false">http://lucd.info/?p=1753#comment-4363</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.lucd.info/2010/02/09/events-part-5-powered-of-for-more-than-1-week/#comment-1142&quot;&gt;LucD&lt;/a&gt;.

You are a legend LucD :-)
Thanks you !!!]]></description>
			<content:encoded><![CDATA[<p>You are a legend LucD 🙂<br />
Thanks you !!!</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: LucD		</title>
		<link>https://www.lucd.info/2010/02/09/events-part-5-powered-of-for-more-than-1-week/#comment-4303</link>

		<dc:creator><![CDATA[LucD]]></dc:creator>
		<pubDate>Fri, 02 Jun 2017 20:30:32 +0000</pubDate>
		<guid isPermaLink="false">http://lucd.info/?p=1753#comment-4303</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.lucd.info/2010/02/09/events-part-5-powered-of-for-more-than-1-week/#comment-4301&quot;&gt;Dominic&lt;/a&gt;.

Hi Dominic,
You could try something like this
&lt;code&gt;
$userName = &#039;joe&#039;

$targetVM = Get-VM -Name &quot;$($userName)-vm&quot; &#124; where{$_.PowerState -eq &#039;PoweredOn&#039;} &#124; select -ExpandProperty Name

if($targetVM){
    $start = (Get-Date).AddDays(-14)
    if(Get-VIEvent -Start $start -MaxSamples ([int]::MaxValue) &#124; where{$_ -is [VMware.Vim.VmPoweredOnEvent] -and $targetVM -contains $_.Vm.Name}){
        Send-MailMessage -Title &quot;VM $($userName)-vm&quot; -Body &quot;VM $(userName)-vm powered on more than 14 days&quot; `
            -From &#039;lucd@lucd.info&#039; -To &quot;$(userName)@abc.com&quot; -SmtpServer &#039;mail.abc.com&#039;
    }
}
&lt;/code&gt;
It will find the user&#039;s VM, check if was powered on more than 14 days and if yes, send the user an email.
Luc]]></description>
			<content:encoded><![CDATA[<p>Hi Dominic,<br />
You could try something like this<br />
<code><br />
$userName = 'joe'</p>
<p>$targetVM = Get-VM -Name "$($userName)-vm" | where{$_.PowerState -eq 'PoweredOn'} | select -ExpandProperty Name</p>
<p>if($targetVM){<br />
    $start = (Get-Date).AddDays(-14)<br />
    if(Get-VIEvent -Start $start -MaxSamples ([int]::MaxValue) | where{$_ -is [VMware.Vim.VmPoweredOnEvent] -and $targetVM -contains $_.Vm.Name}){<br />
        Send-MailMessage -Title "VM $($userName)-vm" -Body "VM $(userName)-vm powered on more than 14 days" `<br />
            -From 'lucd@lucd.info' -To "$(userName)@abc.com" -SmtpServer 'mail.abc.com'<br />
    }<br />
}<br />
</code><br />
It will find the user&#8217;s VM, check if was powered on more than 14 days and if yes, send the user an email.<br />
Luc</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
