VMware Cloud Community
nettech1
Expert
Expert
Jump to solution

Sort results by VM Name

Hi, 

I am generating a list of VMs with their network adapters and the connection state. Having a problem getting the results sorted by the VM name. 

Get-VM | Get-NetworkAdapter | Select-Object @{N="VM";E={$_.Parent.Name}}, name, NetworkName, type, MacAddress, @{N="Connection State";E={Set-Color -string $_.ConnectionState}} | Sort-Object @{E={$_.Parent.Name}} | ConvertTo-Html |% { ($_.Replace("&lt;","<")).Replace("&gt;",">").replace("&quot;",'"').replace("&#39;",'"') } | Out-String

Any help getting this sorted out (no pun intended) is greatly appreciated. 

Thank you!

Reply
0 Kudos
1 Solution

Accepted Solutions
wetnose88
Enthusiast
Enthusiast
Jump to solution

 

 

I changed your code slightly by Sort-Object VM instead of Sort-Object @{E={$_.Parent.Name}}, please try and see if make a difference.

Get-VM | Get-NetworkAdapter | Select-Object @{N="VM";E={$_.Parent.Name}}, name, NetworkName, type, MacAddress, @{N="Connection State";E={Set-Color -string $_.ConnectionState}} |
 Sort-Object VM | ConvertTo-Html |% { ($_.Replace("&lt;","<")).Replace("&gt;",">").replace("&quot;",'"').replace("&#39;",'"') } | Out-String

 

 

View solution in original post

2 Replies
wetnose88
Enthusiast
Enthusiast
Jump to solution

 

 

I changed your code slightly by Sort-Object VM instead of Sort-Object @{E={$_.Parent.Name}}, please try and see if make a difference.

Get-VM | Get-NetworkAdapter | Select-Object @{N="VM";E={$_.Parent.Name}}, name, NetworkName, type, MacAddress, @{N="Connection State";E={Set-Color -string $_.ConnectionState}} |
 Sort-Object VM | ConvertTo-Html |% { ($_.Replace("&lt;","<")).Replace("&gt;",">").replace("&quot;",'"').replace("&#39;",'"') } | Out-String

 

 

nettech1
Expert
Expert
Jump to solution

Thank you! 

# Configure variables in this section 
$smtpServ = "SMTP"
$rcptFrom = "from@from.from"
$rcptTo = "to@to.to"
$eMailSubj = "VM Nics"

# Set HTML color function

function Set-Color($string) {
   
    if ($string.Connected -eq $false){
        "<span class='red'>$string</span>"
    }
    elseif ($string.Connected -eq $true){
        "<span class='green'>$string</span>"
    }
    else{
        $string
    }
}

# HTML Header
$head = "<style>"
$head = $head + "BODY{background-color:white; font-size:10pt;font-family: 'Trebuchet MS', Arial, Helvetica, sans-serif} "
$head = $head + "table {margin-top:3px;border-collapse: collapse;width: 100%;} "
$head = $head + "table td, table th {border: 1px solid #ddd;text-align: left; padding: 7px; valign:top; font-size:10pt; height:10px; line-height:normal} "
$head = $head + "table tr:nth-child(even){background-color: #f2f2f2} "
$head = $head + "table tr:hover {background-color: #ddd;} "
$head = $head + "table th {padding-top: 3px;padding-bottom: 3px;background-color: #3B6E8F;color: white;} "
$head = $head + "table tr {line-height: 3px;} "
$head = $head + ".red {color:red;} " 
$head = $head + ".green {color:green;} "
$head = $head + "h4 {border-bottom:2px solid orangered;padding-bottom:5px;} "
$head = $head + "</style>" 

$body = Get-VM |  Get-NetworkAdapter | Select-Object @{N="VM";E={$_.Parent.Name}}, name, NetworkName, type, MacAddress, @{N="Connection State";E={Set-Color -string $_.ConnectionState}} | Sort-Object VM | ConvertTo-Html |% { ($_.Replace("&lt;","<")).Replace("&gt;",">").replace("&quot;",'"').replace("&#39;",'"') } | Out-String

# Assemble email message.
$msgbody = "<html><head>" + $head + "</head><body>" + $body + "</body></html>"

# Email resulst.
Send-MailMessage -To $rcptTo -from $rcptFrom -Subject $eMailSubj -BodyAsHtml $msgbody -SmtpServer $smtpServ 

 

Reply
0 Kudos