Link to home
Start Free TrialLog in
Avatar of RickEpnet
RickEpnetFlag for United States of America

asked on

List of VM's with Power State IP addresses and

I am working on this script and I cannot figure out why I am not getting the IP or memory in the email.

Can someone help me.

Thanks!!
Add-PSSnapin VMware.VimAutomation.Core

Connect-VIServer "vcenter" -User "xxxxxxx\xxxxx" -Password "xxxxxxxx"


$strOutFile = "C:\temp\list2.htm"


$head = "<style>"
$head = $head + "BODY{background-color:white;}"
$head = $head + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$head = $head + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:thistle}"
$head = $head + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:PaleGoldenrod}"
$head = $head + "</style>"


$smtpServer = "xxxxxxx" 
$strFrom = "rick@xxxxxxx"
$strTo = "rick@xxxxxxxx"
$strSubject = “Power on list - ” + (get-date -DisplayHint date)
$strBody = "Attached is the list of Power On VM"
$strMail = "<H2><u>" + $strSubject + "</u></H2>"


$vms = Get-VM
$myCol = @()
ForEach ($vm in $vms){
           if ($vm.PowerState -eq "PoweredOn") {
            $myObj = "" | select VMName, Powerstate, IPAddress, MemoryMB
            $myObj.VMName = $VM.Name
            $myObj.Powerstate = $VM.PowerState
            $myObj.Memory = $VM.MemoryMB
            $myObj.IPAddress = (Get-VMGuest -VM $VM).IPAddress[0]       
            $myCol += $myObj
    }
        }
$myCol | Sort-Object VM | ConvertTo-HTML -Head $head -Body $strMail | Out-File $strOutFile


$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($strOutFile)
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = $strFrom
$msg.To.Add($strTo)
$msg.Subject = $strSubject
$msg.IsBodyHtml = 1
$msg.Body = Get-Content $strOutFile
$msg.Attachments.Add($att)
$msg.Headers.Add("message-id", "<3BD50098E401463AA228377848493927-1>")	

$smtp.Send($msg)

DisConnect-VIServer

Open in new window

Avatar of morpheios
morpheios
Flag of Russian Federation image

Mistake

$vmview = $vm | Get-View
  $myObj.Memory = $vmconfig.MemoryMB

Open in new window

For IP try:

$vmguest = $vm | Get-VMGuest
$myObj.IPAddress = $vmguest.IPAddress[0] 

Open in new window

Sorry

$vmview = $vm | Get-View
$vmguest = $vm | Get-VMGuest

$myObj.Memory = $vmview.MemoryMB
$myObj.IPAddress = $vmguest.IPAddress[0] 

Open in new window

Avatar of RickEpnet

ASKER

Where do these go in the script

$vmview = $vm | Get-View


I tried it like this. Funny the IP address works like this but no memory.


ForEach ($vm in $vms){
           if ($vm.PowerState -eq "PoweredOn") {
            $myObj = "" | select VMName, Powerstate, IPAddress, MemoryMB
            $myObj.VMName = $VM.Name
            $myObj.Powerstate = $VM.PowerState
            $vmview = $vm | Get-View
	    $myObj.Memory = $vmconfig.MemoryMB
            $myObj.IPAddress = (Get-VMGuest -VM $VM).IPAddress[0]       
            $myCol += $myOb

Open in new window

MemoryMB defined in view not config
Ok I am very new at this how do I get View to work in my script or am I missing something here?
$vmview = $vm | Get-View

than replace
$myObj.Memory = $vmconfig.MemoryMB
to
$myObj.Memory = $vmview.MemoryMB
I am sure it is me but that did not work. Here is what I have. And here is what the email looks like.
$vms = Get-VM
$myCol = @()
ForEach ($vm in $vms){
           if ($vm.PowerState -eq "PoweredOn") {
            $myObj = "" | select VMName, Powerstate, IPAddress, MemoryMB
            $myObj.VMName = $VM.Name
            $myObj.Powerstate = $VM.PowerState
	    $vmview = $vm | Get-View
	    $myObj.Memory = $vmview.MemoryMB
            $myObj.IPAddress = (Get-VMGuest -VM $VM).IPAddress[0]       
            $myCol += $myObj
    }
        }

Open in new window

10-31-2011-11-23-49-PM.jpg
ASKER CERTIFIED SOLUTION
Avatar of morpheios
morpheios
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok this works. You lead me in the right direction kind of.

This was the issue
$myObj.Memory

it should have been this.
$myObj.MemoryMB


ForEach ($vm in $vms){
           if ($vm.PowerState -eq "PoweredOn") {
            $myObj = "" | select VMName, Powerstate, IPAddress, MemoryMB
            $myObj.VMName = $VM.Name
            $myObj.Powerstate = $VM.PowerState
	    $myObj.MemoryMB = $VM.MemoryMB
            $myObj.IPAddress = (Get-VMGuest -VM $VM).IPAddress[0]       
            $myCol += $myObj

Open in new window

Lead me in the right direction