i've been trying to write a script to check all powered on VMs and then email me a list of VMs where tools are not installed or not running.
i've not been able to do it, so can anyone help??
thanks
Ok, file attached
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
There are some samples in the Vmtools Reporting thread.
To send an email with the report have a look at the output to body of email thread.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
thanks, i had already seen that. it works, but doesn't filter out VMs that are not powered off.
That's not too difficult. Use a where clause
Get-VM | where {$_.PowerState -ne "PoweredOff"} | Select Name,
@{N="Tools version";E={$_.ExtensionData.Guest.ToolsVersion}},
@{N="Tools version status";E={$_.ExtensionData.Guest.ToolsVersionStatus}},
@{N="Tools running status";E={$_.ExtensionData.Guest.ToolsRunningStatus}} | `
Export-Csv "C:\VM-Tools.csv" -NoTypeInformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
thanks, that does work, however i need it to only report VMs where the tools are not running. is that possible?
Add this to Luc's code.
Get-VM | where {$_.PowerState -ne "PoweredOff" -and $_.ExtensionData.Guest.ToolsRunningStatus -eq "guestToolsNotRunning" } | Select Name,
@{N="Tools version";E={$_.ExtensionData.Guest.ToolsVersion}},
@{N="Tools version status";E={$_.ExtensionData.Guest.ToolsVersionStatus}},
@{N="Tools running status";E={$_.ExtensionData.Guest.ToolsRunningStatus}} | `
Export-Csv "C:\VM-Tools.csv" -NoTypeInformation
thank you. that does work.
is it possible to get it to email this to me as html?
Sure, try something like this
$smtpSrv = "smtp.server.domain"
$from = "vmware.report@lucd.info"
$to = "lucd@lucd.info"
$subject = "Tools Status Report"
$body = Get-VM | where {$_.PowerState -ne "PoweredOff" -and $_.Guest.State -ne "NotRunning" } | `
Select Name,
@{N="Tools version";E={$_.ExtensionData.Guest.ToolsVersion}},
@{N="Tools version status";E={$_.ExtensionData.Guest.ToolsVersionStatus}},
@{N="Tools running status";E={$_.ExtensionData.Guest.ToolsRunningStatus}} | `
ConvertTo-Html
$smtp = new-object Net.Mail.SMTPclient($smtpSrv)
$msg = new-object Net.Mail.MailMessage($from,$to,$subject,$body)
$msg.IsBodyHTML = $true
$smtp.send($msg)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Unexpected token 'smtp' in expression or statement. At C:\Users\username\tools.ps1:12 char:29 + $msg.IsBodyHTML = $true$smtp <<<< .send($msg) + CategoryInfo : ParserError: (smtp:String) [], ParseException + FullyQualifiedErrorId : UnexpectedToken
The <CR><LF> got lost during the copy/paste.
It's corrected now.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
is something not right with the format??? The term ' ' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:\Users\username\tools.ps1:5 char:100 + $body = Get-VM | where {$_.PowerState -ne "PoweredOff" -and $_.Guest.State - ne "NotRunning" } | ` <<<< Select Name, + CategoryInfo : ObjectNotFound: ( :String) [], CommandNotFoundEx ception + FullyQualifiedErrorId : CommandNotFoundException
That was another line where the <CR><LF> got lost.
Corrected, try again.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
hate to be a pain, but i'm getting the same errors again. is it down to the copy and paste? can it be uploaded in a file instead?
Ok, file attached
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
thank you very much.
just needed to change the Guest.State -ne "NotRunning" to "Running" and ist's exactly what I'm after.
thank you for your time, patience and effort.
one other question, at present it will email even if the list of VMs with no tools running is blank.
is there a way to send the email ONLY if there are machines with no tools running?
thanks
Sure, test if $body is not empty before sending.
Change the last line
if($body){
$smtp.send($msg)
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
blank email still sent.........
My bad, that's because of the ConvertTo-Html .
Try the attached script, it postpones the ConvertTo-Html
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
that really is excellent.
thank you so much for your help.