Hi,
I have created a script which collected the VM counts for the selective folders in each Vcenter, This scripts seems working fine but it's not collecting output to the CSV.
$output = @()
Get-Content -Path C:\capacity_donot_delete\folder.txt | %{
New-Object PSObject -Property @{
Folder = $_
VMCount = Get-Folder -Name $_ | Get-VM | Measure-Object | Select -ExpandProperty Count
}
}
$output | Export-Csv -Path C:\capacity_donot_delete\folder_count.csv -NoTypeInformation -UseCulture
Note: Also, I would like to make this script to generate a mail report whichever collected in the CSV.
Need support to fix this issue - Thanks in advance!!
BR/Subash.
You were never actually filling the $output variable with data anywhere, so it's empty. Try this:
$output = @()
Get-Content -Path C:\capacity_donot_delete\folder.txt | % {
$output += New-Object PSObject -Property @{
Folder = $_
VMCount = Get-Folder -Name $_ | Get-VM | Measure-Object | Select -ExpandProperty Count
}
}$output | Export-Csv -Path C:\capacity_donot_delete\folder_count.csv -NoTypeInformation -UseCulture
To send a mail use the Send-MailMessage cmdlet or use a function like this (enables anonymous SMTP):
function Send-Mail {
Param(
[string][Parameter(Mandatory=$true)]$MailTo,
[string][Parameter(Mandatory=$true)]$MailFrom,
[string][Parameter(Mandatory=$true)]$MailSubject,
[string][Parameter(Mandatory=$true)]$MailBody
)
$SMTPServer = 'MyMailRelay.fqdn'
$mailer = New-Object Net.Mail.SMTPclient($SMTPServer)
$msg = New-Object Net.Mail.MailMessage($MailFrom, $MailTo, $MailSubject, $MailBody)
$mailer.send($msg)
}
Send-Mail -MailTo "recipient@mydomain" -MailFrom "sender@mydomain" -MailSubject "VM Folder Export" -MailBody $output
You were never actually filling the $output variable with data anywhere, so it's empty. Try this:
$output = @()
Get-Content -Path C:\capacity_donot_delete\folder.txt | % {
$output += New-Object PSObject -Property @{
Folder = $_
VMCount = Get-Folder -Name $_ | Get-VM | Measure-Object | Select -ExpandProperty Count
}
}$output | Export-Csv -Path C:\capacity_donot_delete\folder_count.csv -NoTypeInformation -UseCulture
To send a mail use the Send-MailMessage cmdlet or use a function like this (enables anonymous SMTP):
function Send-Mail {
Param(
[string][Parameter(Mandatory=$true)]$MailTo,
[string][Parameter(Mandatory=$true)]$MailFrom,
[string][Parameter(Mandatory=$true)]$MailSubject,
[string][Parameter(Mandatory=$true)]$MailBody
)
$SMTPServer = 'MyMailRelay.fqdn'
$mailer = New-Object Net.Mail.SMTPclient($SMTPServer)
$msg = New-Object Net.Mail.MailMessage($MailFrom, $MailTo, $MailSubject, $MailBody)
$mailer.send($msg)
}
Send-Mail -MailTo "recipient@mydomain" -MailFrom "sender@mydomain" -MailSubject "VM Folder Export" -MailBody $output
Hi MK,
Thanks a lot!! Yes it was missed in my earlier script.
I have tested , It's working like a charm.
Also, I have two more things to do with this script, Could you please guide me to do it better - i will be more obliged.
1. The output file(CSV file) should be created with respective Vcenter name ( likes as folder_count_vcenter name.csv)
2.Mail should be generated with the attachedment of the CSV files.
Thanks in advance!!
-subash.
Use something similar to my response in Re: Powercli Script to measure the count of Host and its Vm's in a each clusters :
Get-content C:\vc.txt | % {
$vc = $_
connect-viserver $vc
$output = @()
Get-Content -Path C:\capacity_donot_delete\folder.txt | % {
$output += New-Object PSObject -Property @{
Folder = $_
VMCount = Get-Folder -Name $_ | Get-VM | Measure-Object | Select -ExpandProperty Count
}
}
disconnect-viserver $vc –force –confirm:$false
$output | Export-Csv -Path "C:\capacity_donot_delete\folder_count_$vc.csv" -NoTypeInformation -UseCulture
}$Attachments = Get-ChildItem C:\capacity_donot_delete\folder_count_*.csv
Send-MailMessage -To "recipient@domain" -From "sender@domain" -SmtpServer mailrelay.domain -Subject "VC Reports" -Body "Report files attached." -Attachments $Attachments
Hi Mk,
This finally works perfectly. Although I am trying to change the concept/logic of the requirement and script.
Whereby, Now I would looking to get the VM counts for the set of sub-folders which is mapped to a specific folder (likes as i have a folder name with "customers" whereby multiple cusotmers sub-folders will be pointing folder- customers.
I didn't find any -recurse option in get-folder command - Any Idea, How can we achieve this??
Thanks in advance!!
-Subash.