Quantcast
Channel: VMware Communities : Discussion List - All Communities
Viewing all articles
Browse latest Browse all 180259

Trouble with populating the Get-VIEventPlus function into html code

$
0
0

Hi

 

I'm using Luc's Get-VIEventPlus function but having trouble populating my html code: The function works great but what am I doing wrong that the data is not being populated into the html file?

 

Code:

 

# Function to perform the work for the data you need
function Get-VIEventPlus {
<# 
.SYNOPSIS  Returns vSphere events  
.DESCRIPTION The function will return vSphere events. With
  the available parameters, the execution time can be
  improved, compered to the original Get-VIEvent cmdlet.
.NOTES  Author:  Luc Dekens 
.PARAMETER Entity
  When specified the function returns events for the
  specific vSphere entity.
.PARAMETER EventType
  This parameter limits the returned events to those
  specified on this parameter.
.PARAMETER Start
  The start date of the events to retrieve
.PARAMETER Finish
  The end date of the events to retrieve.
.PARAMETER Recurse
  A switch indicating if the events for the children of
  the Entity will also be returned
.PARAMETER FullMessage
  A switch indicating if the full message shall be compiled.
  This switch can improve the execution speed if the full
  message is not needed. 
.EXAMPLE
  PS> Get-VIEventPlus -Entity $vm
.EXAMPLE
  PS> Get-VIEventPlus -Entity $cluster -Recurse:$true
#>
param(
    [VMware.VimAutomation.ViCore.Impl.V1.Inventory.InventoryItemImpl[]]$Entity,
    [string[]]$EventType,
    [DateTime]$Start,
    [DateTime]$Finish,
    [switch]$Recurse,
    [switch]$FullMessage = $false
  )

process {
    $eventnumber = 100
    $events = @()
    $eventMgr = Get-View EventManager
    $eventFilter = New-Object VMware.Vim.EventFilterSpec
    $eventFilter.disableFullMessage = ! $FullMessage
    $eventFilter.entity = New-Object VMware.Vim.EventFilterSpecByEntity
    $eventFilter.entity.recursion = &{if($Recurse){"all"}else{"self"}}
    $eventFilter.eventTypeId = $EventType
    if($Start -or $Finish){
      $eventFilter.time = New-Object VMware.Vim.EventFilterSpecByTime
      $eventFilter.time.beginTime = $Start
      $eventFilter.time.endTime = $Finish
    }

    $entity | %{
      $eventFilter.entity.entity = $_.ExtensionData.MoRef
      $eventCollector = Get-View ($eventMgr.CreateCollectorForEvents($eventFilter))
      $eventsBuffer = $eventCollector.ReadNextEvents($eventnumber)
      while($eventsBuffer){
        $events += $eventsBuffer
        $eventsBuffer = $eventCollector.ReadNextEvents($eventnumber)
      }
      $eventCollector.DestroyCollector()
    }
    $events
  }
}
$oneMonth = (Get-Date).AddDays(-31)
$entity = Get-Folder -Name datacenters
$events = Get-VIEventPlus -Entity $entity -EventType VmPoweredOnEvent -Recurse -FullMessage
$events | Group-Object -Property {$_.Vm.Name} | %{
$_.Group | Sort-Object -Property CreatedTime -Descending |
Select -First 1 |
where {$_.CreatedTime -lt $oneMonth -and (Get-VM -Name $_.Vm.Name -ErrorAction SilentlyContinue).PowerState -eq "PoweredOff"} |
   New-Object -TypeName PSObject -Property @{
          VM = $_.VM.Name
          LastPoweredOn = $_.CreatedTime
   User = $_.VM.Name | Select UserName
   Owner = (Get-VM $_.Vm.Name).CustomFields.Item("OwnerDL")

 

                    }
                writedata $_.VM $_.LastPowredOn $_.User $_.Owner
                     
             }
     

#Function will compile the data passed from the GetCluster Function into HTML table format
##########################################################################################
Function writedata {
    param ($VM,$LastPoweredOn,$User,$Owner)
   
#Write output to html table
    ###########################
    $tableEntry = "<td><font face='Tahoma'>$($VM)</font></td>" +
        "<td><font face='Tahoma'>$($LastPoweredOn)</font></td>" +       
        "<td><font face='Tahoma'>$($User)</font></td>" +       
         "<td><font face='Tahoma'>$($Owner)</font></td></tr>"
        Add-Content $fileName $tableEntry
        Write-Host $tableEntry}

 

End Code:

 

Thanks


Viewing all articles
Browse latest Browse all 180259

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>