RickEpnet
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!!
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
For IP try:
$vmguest = $vm | Get-VMGuest
$myObj.IPAddress = $vmguest.IPAddress[0]
Sorry
$vmview = $vm | Get-View
$vmguest = $vm | Get-VMGuest
$myObj.Memory = $vmview.MemoryMB
$myObj.IPAddress = $vmguest.IPAddress[0]
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.
$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
MemoryMB defined in view not config
ASKER
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
than replace
$myObj.Memory = $vmconfig.MemoryMB
to
$myObj.Memory = $vmview.MemoryMB
ASKER
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
}
}
10-31-2011-11-23-49-PM.jpg
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
ASKER
Lead me in the right direction
Open in new window