none
How to show filtered data in the body of a mailmessage RRS feed

  • Question

  • Hello, I have been using a powershell command with my VMs and it works great. Here is what I use -
    get-vm | export-csv "C:test.csv"
    send-mailmessage -to "test@abc.com" -from "atest@abc.com" -attachments "C:test.csv"
    Basically the code above works fine but there is so much information in that CSV all I really need is to know if it is powered on or off. How would I go about showing all the VMs that are running in the body of the email. That way I dont have to open the csv up and look through it.

    Monday, February 9, 2015 2:22 PM

Answers

  • Are you using the PowerCli cmdlets? You can filter by Powerstate and only grab the machines that are powered on - 

    get-vm | ? {$_.PowerState -eq "PoweredOn" | Select Name} export-csv "C:\test.csv"
    send-mailmessage -to "test@abc.com" -from "atest@abc.com" -attachments "C:\test.csv"

    • Marked as answer by bohlingj Monday, February 9, 2015 5:58 PM
    Monday, February 9, 2015 2:35 PM
  • Tell us what you're using--VMWare or Hyper-V--by entering Get-Command Get-VM and reporting what is listed under ModuleName.
    • Marked as answer by bohlingj Monday, February 9, 2015 5:58 PM
    Monday, February 9, 2015 3:39 PM

All replies

  • Are you using the PowerCli cmdlets? You can filter by Powerstate and only grab the machines that are powered on - 

    get-vm | ? {$_.PowerState -eq "PoweredOn" | Select Name} export-csv "C:\test.csv"
    send-mailmessage -to "test@abc.com" -from "atest@abc.com" -attachments "C:\test.csv"

    • Marked as answer by bohlingj Monday, February 9, 2015 5:58 PM
    Monday, February 9, 2015 2:35 PM
  • To send the list in the body try something like this:

    get-vm | Where-Object { $_.PowerState -eq "PoweredOn" } | ForEach { $list += $_.Name + "`n" }
    send-mailmessage -to "test@abc.com" -from "atest@abc.com" -body $list

    Monday, February 9, 2015 2:43 PM
  • No luck when using this.
    get-vm | Win7-1 {$_.PowerState -eq "PoweredOn" | Select Name} export-csv "C:\users\Desktop\test.csv"
    send-mailmessage -to "XXX@XXXX" -from "XXX@XXXX.com" -attachments "C:\users\Desktop\test.csv" -Subject "Hyper-V Status" -SmtpServer XXXXXX

    Monday, February 9, 2015 2:46 PM
  • Sorry, the "?" is shorthand for Where-Object.

    The below are both the same command, the "Win7-1" you included isn't needed.

    get-vm | ? {$_.PowerState -eq "PoweredOn"}

    get-vm | where-object {$_.PowerState -eq "PoweredOn"}

    Edit: Jalapeno's method would probably work quite well for you.

    • Edited by Braham20 Monday, February 9, 2015 2:56 PM
    Monday, February 9, 2015 2:56 PM
  • For some reason I am now receiving this error.
    Send-MailMessage : Cannot validate argument on parameter 'Body'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

    I have one VM that I know for a fact is running.
    Monday, February 9, 2015 3:16 PM
  • This may work better - 

    $on = (get-vm | Where-Object {$_.PowerState -eq "PoweredOn" | Select Name}).Name 
    
    send-mailmessage -to "test@abc.com" -from "atest@abc.com" -body ($on | out-string)


    • Edited by Braham20 Monday, February 9, 2015 3:26 PM
    Monday, February 9, 2015 3:24 PM
  • $_PowerState -eq "PoweredOn" is based on VMware is you are using Hyper-V it may be $_.State -eq "Running".  I do not have Hyper-V so I can not test it.
    Monday, February 9, 2015 3:29 PM
  • If the OP is actually using Hyper-V, then the property is in fact State and the values are on or off.

    Get-VM | Where-Object State -eq On | Select...

    Edit: Running seems to work as well.
    Monday, February 9, 2015 3:34 PM
  • Thanks for your help. I am getting closer. From the post above from Braham20 that one outputs some data but for some reason it just lists all the VM's I have even if they are off.  I tried both says State and PowerState. Both give the same data.
    Monday, February 9, 2015 3:37 PM
  • Tell us what you're using--VMWare or Hyper-V--by entering Get-Command Get-VM and reporting what is listed under ModuleName.
    • Marked as answer by bohlingj Monday, February 9, 2015 5:58 PM
    Monday, February 9, 2015 3:39 PM
  • I am using Hyper-V 

    $on = (get-vm | Where-Object {$_.State -eq "On" | Select Name}).Name
    send-mailmessage -to "xxxx@xxxx.com" -from "noreply@xxxx.com" -body ($on | out-string) -Subject "Hyper-V Status" -SmtpServer xxxx

    When using this command I am sent an email with all my vms listed even if they are powered off.
    Monday, February 9, 2015 3:44 PM
  • Looks like 'On' isn't always working so you should probably go back in 'Running'. Try this example:
    $on = (Get-VM | Where-Object {$_.State -eq 'Running'}).Name

    Monday, February 9, 2015 3:52 PM
  • After I did a restart on my PC and run the code again it worked. Not sure what was going on but I am good to go now. Thank you for all your help.
    Monday, February 9, 2015 5:57 PM