shinpad
Contributor
Contributor

Script for VMTools status

Jump to solution

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

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

Ok, file attached


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

View solution in original post

0 Kudos
24 Replies
LucD
Leadership
Leadership

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

0 Kudos
shinpad
Contributor
Contributor

thanks, i had already seen that. it works, but doesn't filter out VMs that are not powered off.

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
shinpad
Contributor
Contributor

thanks, that does work, however i need it to only report VMs where the tools are not running. is that possible?

0 Kudos
zomx
Contributor
Contributor

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

0 Kudos
shinpad
Contributor
Contributor

thank you. that does work.

is it possible to get it to email this to me as html?

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
shinpad
Contributor
Contributor

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

0 Kudos
LucD
Leadership
Leadership

The <CR><LF> got lost during the copy/paste.

It's corrected now.


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

0 Kudos
shinpad
Contributor
Contributor

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

0 Kudos
LucD
Leadership
Leadership

That was another line where the <CR><LF> got lost.

Corrected, try again.


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

0 Kudos
shinpad
Contributor
Contributor

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?

0 Kudos
LucD
Leadership
Leadership

Ok, file attached


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

View solution in original post

0 Kudos
shinpad
Contributor
Contributor

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.

0 Kudos
shinpad
Contributor
Contributor

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

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
shinpad
Contributor
Contributor

blank email still sent.........

0 Kudos
LucD
Leadership
Leadership

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

shinpad
Contributor
Contributor

that really is excellent.

thank you so much for your help.

0 Kudos