Powershell function

Hello,

I am a newbies about powershell script but I would like to understand how to do one thing.

I'd like to send a value by email from powershell.

I found a very nice function and I'd like to put the result of this function in my email body. My email is ready and works, no problem with that.

I do not know how I can get the result. I though I need to get function result into variable and use this variable for my email but I do not know how to do that and I am not sure I need to do that.

Please see below the function:

Function ConvertTo-Char
(      
      $Array
)
{
      $output = ""
      ForEach($char in $Array)
      {      $Output += [char]$char -join ""
      }
      return $Output
}

$Query = Get-WmiObject -Query "Select * FROM WMIMonitorID" -Namespace root\wmi


$Results = ForEach ($Monitor in $Query)
{    
      New-Object PSObject -Property @{
            Manufacturer = ConvertTo-Char($Monitor.ManufacturerName)
            UserFriendlyName = ConvertTo-Char($Monitor.userfriendlyname)
            SerialNumber = ConvertTo-Char($Monitor.serialnumberid)
      }
}

$Results | Select Manufacturer,UserFriendlyName,SerialNumber


I'd like to get monitor information and send by email.

Thank you in advance for your help

Christophe
ChristopheIT CoordinatorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
You can shorten the code above to this:
Get-WmiObject -Namespace "Root\WMI" -Query "Select * FROM WMIMonitorID" | Select-Object -Property `
	@{Name="Manufacturer"; Expression={[System.Text.Encoding]::Ascii.GetString($_.ManufacturerName)}},
	@{Name="UserFriendlyName"; Expression={[System.Text.Encoding]::Ascii.GetString($_.UserFriendlyName)}},
	@{Name="SerialNumber"; Expression={[System.Text.Encoding]::Ascii.GetString($_.SerialNumberID)}}

Open in new window

To get that as text into an email, format it as needed, then pipe the output through Out-String; example:
$MonitorString = Get-WmiObject -Namespace "Root\WMI" -Query "Select * FROM WMIMonitorID" | Select-Object -Property `
	@{Name="Manufacturer"; Expression={[System.Text.Encoding]::Ascii.GetString($_.ManufacturerName)}},
	@{Name="UserFriendlyName"; Expression={[System.Text.Encoding]::Ascii.GetString($_.UserFriendlyName)}},
	@{Name="SerialNumber"; Expression={[System.Text.Encoding]::Ascii.GetString($_.SerialNumberID)}} |
	Format-Table -AutoSize |
	Out-String

Open in new window

Now you can use "$MonitorString" in your email body.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ChristopheIT CoordinatorAuthor Commented:
Thank you very much! It is working perfectly!

Now I will try to find how to modify result format because in my email, all values from $monitorstring are in the same line:

Monitor(s): Manufacturer UserFriendlyName SerialNumber ------------ ---------------- ------------ DEL DELL P2210 W416K05K0TDM SNY SDM-P232W 7701720  

Thanks again for your help!

Christophe
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.