Automation

 View Only
  • 1.  Vmotion Report How to modify to exempt a particular cluster

    Posted Mar 02, 2021 08:55 PM

    Hey guys,

     

    How can I modify this script below to exempt a particular cluster from the report?

     

    Import-Module VMware.VimAutomation.Core


    if ($Global:DefaultVIServers){
    $Global:DefaultVIServers | %{
    Disconnect-VIServer $_.Name -WarningAction:SilentlyContinue -ErrorAction:SilentlyContinue -Confirm:$false | Out-Null
    }

    }

    $domainUser = 'hidden'
    $File = 'C:\temp\hidden.txt'
    $domainUserCreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $domainUser, (Get-Content $File | ConvertTo-SecureString)


    #Connect-VIServer hidden -WarningAction:SilentlyContinue | Out-Null


    $inv = @()
    $date = Get-Date -Format "MM-dd-yyyy"
    $reportFile = "C:\temp\vMotion_Report-$date.csv"
    #$events = Get-VIEvent -maxsamples 1000000000 -Start (get-Date).AddHours(-25) -Finish (Get-Date) | Where-Object {($_.GetType()).Name -like "*VmMigratedEvent*"}
    $events = Get-VIEvent -maxsamples 1000000000 -Start (get-Date).AddDays(-3) -Finish (Get-Date) | Where-Object {($_.GetType()).Name -like "*VmMigratedEvent*"}


    foreach ($event in $events){

    $eventType = ($event.GetType()).Name

    if($eventType -like "*drs*"){
    if($event.SourceDatastore.Name -ne $event.Ds.Name){
    if($event.SourceHost.Name -ne $event.Host.Name){
    $eventType = "DRS - Host & Datastore change event"
    }
    else{
    $eventType = "DRS - Datastore change event"
    }
    }
    else{
    if($event.SourceHost.Name -ne $event.Host.Name){
    $eventType = "DRS - Host change event"
    }
    else{
    $eventType = "DRS - Nothing changed!"
    }
    }
    }
    else{
    if($event.SourceDatastore.Name -ne $event.Ds.Name){
    if($event.SourceHost.Name -ne $event.Host.Name){
    $eventType = "Manual - Host & Datastore change event"
    }
    else{
    $eventType = "Manual - Datastore change event"
    }
    }
    else{
    if($event.SourceHost.Name -ne $event.Host.Name){
    $eventType = "Manual - Host change event"
    }
    else{
    $eventType = "Manual - Nothing changed!"
    }
    }
    }

    $obj = "" | select Cluster, VM, "Time-EST", UserName, EventType, SourceHost, SourceDatastore, DestinationHost, DestinationDatastore

    $obj.Cluster = $event.ComputeResource.Name
    $obj.VM = $event.VM.Name
    $obj."Time-EST" = $event.CreatedTime
    $obj.UserName = $event.UserName
    $obj.EventType = $eventType
    $obj.SourceHost = $event.SourceHost.Name
    $obj.SourceDatastore = $event.SourceDatastore.Name
    $obj.DestinationHost = $event.Host.Name
    $obj.DestinationDatastore = $event.Ds.Name

    $inv += $obj

    }

    $inv | Export-Csv -NoTypeInformation $reportFile


    $smtpServer = "hidden"
    $smtpFrom = "hidden"
    #$smtpTo = @("hidden")
    $smtpTo = @("hidden")


    $messageSubject = "Daily vMotion Report"
    $messageBody = "Team,`n`nPlease find attached vMotion Report.`n`n`nRegards,`nVMware Team"
    $Attachments = $reportFile

    Send-MailMessage -From $smtpFrom -To $smtpTo -Subject $messageSubject -Body $messageBody -SmtpServer $smtpServer -Attachments $Attachments

     



  • 2.  RE: Vmotion Report How to modify to exempt a particular cluster
    Best Answer

    Posted Mar 02, 2021 09:40 PM

    You could adapt the Where-clause after the Get-VIEvent.

    $events = Get-VIEvent -maxsamples 1000000000 -Start (get-Date).AddDays(-3) -Finish (Get-Date) | 
        Where-Object {$_ -is [VMware.Vim.VmMigratedEvent] -and $_.ComputeResource.Name -ne "ClusterExcempt"}

    But I'm wondering if that script even produces anything useful.
    You collect the events of type VmMigratedEvent, but then later on in the script you test on events that start with "DRS", followed by a bunch of nested If-Else constructions. 



  • 3.  RE: Vmotion Report How to modify to exempt a particular cluster

    Posted Mar 02, 2021 09:44 PM

    On a side note, this will be the last thread of yours that I reply to.
    Since you don't seem to bother giving feedback or indicate that your question was answered, I guess you won't care about receiving a reply or not either.



  • 4.  RE: Vmotion Report How to modify to exempt a particular cluster

    Posted Mar 03, 2021 12:48 PM

    I apologize Luc.  On that last question I put in back in Dec.  Not giving feedback.