VMware Cloud Community
vmk2014
Expert
Expert
Jump to solution

VM removed from vCenter

Hi All,

I'm trying to fetch the VM's removed in last 24  hours, but when i execute the script, it fails with an errors. Any pointers will be much appreciated.

pastedImage_0.png

$fragments += $start = (Get-Date).AddDays(-1) | Get-VIEvent -Start $start -MaxSamples ([int]::MaxValue) | where{$_ -is [VMware.Vim.VmRemovedEvent]}

Sort-Object -Property CreatedTime, UserName |

ConvertTo-Html -Fragment -PreContent "<H2>VM's deleted in last 24 hours</H2>"

Thanks

V

Tags (1)
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Not sure where you got that code from, but you can'f feed the Start parameter over the pipeline to Get-VIEvent.

Also not sure why you use a Fragment in this script.

Try something like this

$start = (Get-Date).AddDays(-1)

Get-VIEvent -Start $start -MaxSamples ([int]::MaxValue) | where{$_ -is [VMware.Vim.VmRemovedEvent]} |

Sort-Object -Property CreatedTime, UserName |

ConvertTo-Html -PreContent "<H2>VM's deleted in last 24 hours</H2>" | Out-String |

Out-File -FilePath .\report.html


Invoke-Item -Path .\report.html


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

Not sure where you got that code from, but you can'f feed the Start parameter over the pipeline to Get-VIEvent.

Also not sure why you use a Fragment in this script.

Try something like this

$start = (Get-Date).AddDays(-1)

Get-VIEvent -Start $start -MaxSamples ([int]::MaxValue) | where{$_ -is [VMware.Vim.VmRemovedEvent]} |

Sort-Object -Property CreatedTime, UserName |

ConvertTo-Html -PreContent "<H2>VM's deleted in last 24 hours</H2>" | Out-String |

Out-File -FilePath .\report.html


Invoke-Item -Path .\report.html


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
vmk2014
Expert
Expert
Jump to solution

Thank you, LucD. As you recommended that most of this is available in another thread, hence i took the code from your thread for multiple tasks but doesn't send report but its failing.

with an error. It will run forever as shown in screenshot. I'm getting report for another tasks but it taking time due to this VM removed event.

pastedImage_1.png

I used the code recommended by you.and removed the pipline and i have used the thread from PowerCLI Script Required for Multiple Tasks

and Report VMs deleted past 90 days

$fragments += $start = (Get-Date).AddDays(-1)

Get-VIEvent -Start $start -MaxSamples ([int]::MaxValue) | where{$_ -is [VMware.Vim.VmRemovedEvent]}

Sort-Object -Property CreatedTime, UserName |

ConvertTo-Html -Fragment -PreContent "<H2>VM's deleted in last 24 hours</H2>"

Thanks

V

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The code is working for me.

Which error are you getting?
"run forever"? Are you sure, the Get-VIEvent might run a rather long time.

Perhaps change the $start to (Get-Date).AddHours(-1) as a test.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
vmk2014
Expert
Expert
Jump to solution

Yes, LucD. It works for me as well, but it takes time and it doesn't come  in mail box. I'm able to receive for DRS,VMotion and host maintenance mode report without any issue.

"run forever"? No. Are you sure, the Get-VIEvent might run a rather long time. - yes. Ideally the below output should not onscreen instead of sending over mail.

pastedImage_0.png

Thanks

V

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is because you didn't copy the pipeline symbol at the end of Get-VIEvent line


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
vmk2014
Expert
Expert
Jump to solution

LuCD,

I'm using the below code. It will take time show output on the console and no report over mail. I have used the pipeline at the end of Get-VIEvent line

pastedImage_2.png

$fragments += $start = (Get-Date).AddDays(-1)

Get-VIEvent -Start $start -MaxSamples ([int]::MaxValue) | where{$_ -is [VMware.Vim.VmRemovedEvent]}

Sort-Object -Property CreatedTime, UserName |

ConvertTo-Html -Fragment -PreContent "<H2>VM's deleted in last 24 hours</H2>"

# Send email

$sMail = @{

    From = 'abc@domain.com'

    To = 'subcon@domain.com'

    Subject = 'VM report'

    SmtpServer = 'smtp.ab.xyz.com'

    BodyAsHtml = $true

    Body = ConvertTo-Html -Head $style -Body $fragments | Out-String

}

Send-MailMessage @sMail

}

Disconnect-VIServer -Server $vcenters -Confirm:$false

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Really?
I have now posted a working script and shown you where the issue with your code is, but you apparently won't listen or correct your code.

Sorry, I have better things to do then keep repeating myself to someone who will not read or listen.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
vmk2014
Expert
Expert
Jump to solution

LucD,

No error. I got it.

Thanks

Reply
0 Kudos