VMware Cloud Community
shinpad
Contributor
Contributor
Jump to solution

Script for VMTools status

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

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Ok, file attached


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

View solution in original post

Reply
0 Kudos
24 Replies
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
shinpad
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
shinpad
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos
zomx
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos
shinpad
Contributor
Contributor
Jump to solution

thank you. that does work.

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
shinpad
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

It's corrected now.


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

Reply
0 Kudos
shinpad
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Corrected, try again.


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

Reply
0 Kudos
shinpad
Contributor
Contributor
Jump to solution

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?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, file attached


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

Reply
0 Kudos
shinpad
Contributor
Contributor
Jump to solution

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.

Reply
0 Kudos
shinpad
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
shinpad
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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
Jump to solution

that really is excellent.

thank you so much for your help.

Reply
0 Kudos