VMware Cloud Community
impranayk
Enthusiast
Enthusiast
Jump to solution

PowerCLI Script Required for Multiple Tasks

I have 3 vCenter servers. I want to get below reports on daily basis from all 3 different Virtual Center servers.  (VC Version - 6.5)

I am looking for a script which I run from a VC, and it generate all these information in one file per VC wise, and then send report to email. I am already using vCheck but specifically looking fa separate script for these tasks only.

  • Number of VMs migrated through DRS in last 24 hours.
  • Physical adapter Status of ESXi Hosts
  • List of Host in maintenance mode
  • List of Datastores over provisioned
  • New VMs created in last 24 hours
  • VMs removed in last 24 hours
  • Snapshot report
  • Snapshot older than 7 days

LucD

-------------------------------------------------------------------------
Follow me @ www.vmwareinsight.com
Please consider marking this answer "correct" or "helpful" if you found it useful

Pranay Jha | Blog: http://vmwareinsight.com
vExpert 2016/2017, VCAP5-DCD/DCA, VCP5-DCV, VCA-Cloud, VCE-CIA, MCSE, MCITP
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

This example sends an email in HTML format for each vCenter.
You can add additional script in the same way as scripts 1,2 and 3 are done.
Each script produces a fragment of the report.
You can change the style to whatever you want, this is just an example.

$vcenters = 'vc1','vc2','vc3'

$style = @'

<style>

body { background-color:#E5E4E2;

       font-family:Monospace;

       font-size:10pt; }

td, th { border:0px solid black;

         border-collapse:collapse;

         white-space:pre; }

th { color:white;

     background-color:black; }

table, tr, td, th { padding: 2px; margin: 0px ;white-space:pre; }

tr:nth-child(odd) {background-color: lightgray}

table { width:95%;margin-left:5px; margin-bottom:20px;}

h2 {

font-family:Tahoma;

color:#6D7B8D;

}

.alert {

color: red;

}

.footer

{ color:green;

  margin-left:10px;

  font-family:Tahoma;

  font-size:8pt;

  font-style:italic;

}

</style>

'@


$now = Get-Date


Connect-VIServer -Server $vcenters


$global:defaultVIServers | ForEach-Object -Process {

    $fragments = @()


    # Report Title

    $fragments += "<H1>vCenter $([string]($_.Name))</H1>"


    # Script 1

    $fragments += Get-VIEvent -Server $_ -Start $now.AddHours(-1) -MaxSamples ([int]::MaxValue) |

        where { $_ -is [VMware.Vim.DrsVmMigratedEvent] } |

        Select CreatedTime, @{N = 'VM'; E = { $_.Vm.Name } },

        @{N = 'From'; E = { $_.SourceHost.Name } },

        @{n = 'To'; E = { $_.Host.Name } } |

        Sort-Object -Property CreatedTime |

        ConvertTo-Html -Fragment -PreContent "<H2>DRS vMotion</H2>"


    # Script 2

    $fragments += Get-VMHost -Server $_ | Get-VMHostNetworkAdapter -Physical -VMKernel:$false |

    Select @{N = 'VMHost'; E = { $_.VMHost.Name } }, Name, BitRatePerSec, FullDuplex, Mac |

    Sort-Object -Property VMHost, Name |

    ConvertTo-Html -Fragment -PreContent "<H2>pNIC Configuration</H2>"


# Script 3

$fragments += Get-VMHost -Server $_ | where { $_.State -eq 'maintenance' } |

Select @{N = 'VMHost'; E = { $_.Name } }, State |

Sort-Object -Property VMHost, Name |

ConvertTo-Html -Fragment -PreContent "<H2>Hosts in maintenance mode</H2>"


# Send email

$sMail = @{

    From = 'me@domain'

    To = 'you@domain'

    Subject = 'Report'

    SmtpServer = 'mail.domain'

    BodyAsHtml = $true

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

}

Send-MailMessage @sMail

}


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


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

View solution in original post

7 Replies
LucD
Leadership
Leadership
Jump to solution

Most if not all of those tasks are, in one form or another, available in this community.
What do you already have?
What do you mean by

... a script which I run from a VC

Since a VCSA is to be considered a black box, I strongly advise against adding/running anything on there.


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

0 Kudos
impranayk
Enthusiast
Enthusiast
Jump to solution

Yes, these are available separately. If I want to merge all those seprate scripts in a single script, will it work? But I don't know how to do that. I have to run these scripts for each tasks individually. I am looking for a single script which I can run and fetch these all information in a html or excel file or email report.

If you can help for 2-3 scripts, I can try for others.

-------------------------------------------------------------------------
Follow me @ www.vmwareinsight.com
Please consider marking this answer "correct" or "helpful" if you found it useful

Pranay Jha | Blog: http://vmwareinsight.com
vExpert 2016/2017, VCAP5-DCD/DCA, VCP5-DCV, VCA-Cloud, VCE-CIA, MCSE, MCITP
0 Kudos
LucD
Leadership
Leadership
Jump to solution

What about my VCSA remark?

Also, how do you want to organise this report?

Everything in 1 CSV file is practically impossible, the different report components have different layouts.


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

0 Kudos
impranayk
Enthusiast
Enthusiast
Jump to solution

VCSA - I mean to say running from PowerCLI from windows based vCenter server.

Different CSV file is also fine..or if it can be visible in a html format.

-------------------------------------------------------------------------
Follow me @ www.vmwareinsight.com
Please consider marking this answer "correct" or "helpful" if you found it useful

Pranay Jha | Blog: http://vmwareinsight.com
vExpert 2016/2017, VCAP5-DCD/DCA, VCP5-DCV, VCA-Cloud, VCE-CIA, MCSE, MCITP
0 Kudos
LucD
Leadership
Leadership
Jump to solution

This example sends an email in HTML format for each vCenter.
You can add additional script in the same way as scripts 1,2 and 3 are done.
Each script produces a fragment of the report.
You can change the style to whatever you want, this is just an example.

$vcenters = 'vc1','vc2','vc3'

$style = @'

<style>

body { background-color:#E5E4E2;

       font-family:Monospace;

       font-size:10pt; }

td, th { border:0px solid black;

         border-collapse:collapse;

         white-space:pre; }

th { color:white;

     background-color:black; }

table, tr, td, th { padding: 2px; margin: 0px ;white-space:pre; }

tr:nth-child(odd) {background-color: lightgray}

table { width:95%;margin-left:5px; margin-bottom:20px;}

h2 {

font-family:Tahoma;

color:#6D7B8D;

}

.alert {

color: red;

}

.footer

{ color:green;

  margin-left:10px;

  font-family:Tahoma;

  font-size:8pt;

  font-style:italic;

}

</style>

'@


$now = Get-Date


Connect-VIServer -Server $vcenters


$global:defaultVIServers | ForEach-Object -Process {

    $fragments = @()


    # Report Title

    $fragments += "<H1>vCenter $([string]($_.Name))</H1>"


    # Script 1

    $fragments += Get-VIEvent -Server $_ -Start $now.AddHours(-1) -MaxSamples ([int]::MaxValue) |

        where { $_ -is [VMware.Vim.DrsVmMigratedEvent] } |

        Select CreatedTime, @{N = 'VM'; E = { $_.Vm.Name } },

        @{N = 'From'; E = { $_.SourceHost.Name } },

        @{n = 'To'; E = { $_.Host.Name } } |

        Sort-Object -Property CreatedTime |

        ConvertTo-Html -Fragment -PreContent "<H2>DRS vMotion</H2>"


    # Script 2

    $fragments += Get-VMHost -Server $_ | Get-VMHostNetworkAdapter -Physical -VMKernel:$false |

    Select @{N = 'VMHost'; E = { $_.VMHost.Name } }, Name, BitRatePerSec, FullDuplex, Mac |

    Sort-Object -Property VMHost, Name |

    ConvertTo-Html -Fragment -PreContent "<H2>pNIC Configuration</H2>"


# Script 3

$fragments += Get-VMHost -Server $_ | where { $_.State -eq 'maintenance' } |

Select @{N = 'VMHost'; E = { $_.Name } }, State |

Sort-Object -Property VMHost, Name |

ConvertTo-Html -Fragment -PreContent "<H2>Hosts in maintenance mode</H2>"


# Send email

$sMail = @{

    From = 'me@domain'

    To = 'you@domain'

    Subject = 'Report'

    SmtpServer = 'mail.domain'

    BodyAsHtml = $true

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

}

Send-MailMessage @sMail

}


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


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

vmk2014
Expert
Expert
Jump to solution

LucD,

Can we add storage vmotion also in this report ?

Also, i observed that the report doesn't send the report for :-

  • List of Datastores over provisioned
  • New VMs created in last 24 hours
  • VMs removed in last 24 hours
  • Snapshot report
  • Snapshot older than 7 days

Thanks

V

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I think I addressed that in my first reply, most, if not all, of those scripts are available in this community.

Do a search.
If there is one requirement for which you can't find a script, open a new thread.


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