How do I send Hashtable values in email message?

Ryan Farabaugh 21 Reputation points
2021-02-12T21:41:48.027+00:00

I'm learning Powershell scripting in my class and for my assignment I need to use a hashtable and send the information in an email. (I do not have to send the email, It just needs to work, I'd never send this stuff though an email) But when ever I do test the script the email I get shows System.Collections.Hastable.Computer_Name, ect. How do I get the values to show in the email? Thank you.
Here is my script:

$exists=Test-Path C:\Users\user\Desktop\cred.xml

if($exists -eq $false){
Get-Credential|Export-Clixml C:\Users\user\Desktop\cred.xml}

$cred=Import-clixml -Path C:\Users\user\Desktop\cred.xml

$hash = @{ID = 1; Computer_Name = $env:COMPUTERNAME; Ip_Address = (((Test-Connection $env:computername -Count 1).ipv4address).ipaddresstostring); RAM = ((systeminfo | Select-String 'Total Physical Memory:').ToString().Split(':')[1].Trim()); BIOS_Version = ((systeminfo | Select-String 'BIOS Version:').ToString().Split(':')[1].Trim())}

$mailMessage =@{
from="user@keyman .com"
to="user@keyman .com"
subject="Computer Information"
body="
Computer Name - $hash.Computer_Name
Ip Address - $hash.Ip_Address
RAM - $hash.RAM
BIOS Version - $hash.BIOS_Version"
smtpserver="smtp.office365.com"
port="587"
usessl=$true
credential=$cred}

Send-MailMessage @mailMessage

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,170 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 42,826 Reputation points
    2021-02-12T22:43:28.77+00:00

    See if this change works:

    $mailMessage =@{
        from="user@domain.com"
        to="user@domain.com"
        subject="Computer Information"
        body="
        Computer Name - $($hash.Computer_Name)
        Ip Address - $($hash.Ip_Address)
        RAM - $($hash.RAM)
        BIOS Version - $($hash.BIOS_Version)"
        smtpserver="smtp.office365.com"
        port="587"
        usessl=$true
        credential=$cred}
    

0 additional answers

Sort by: Most helpful