Hello,
I am a newbie Powershell scripter, but have put together something that is mostly functional. The problem I have is that I have the information I need but once that info is put into a variable to be sent through email, it doesn't seem to transfer the information..
Below is the code, and the email shows up blank. I have hard set the $msg.body to something and it does email the string properly. So from what I can tell the $msg.body has issues with something included in the returned variable but I cannot tell.
Thanks in advance, and maybe this code will help someone else.
(I have put the smtp server as X.X.X.X but in my version it has the proper IP address as well as the email addresses)
#Get-MonitorInfo
Function Get-MonitorInfo
{
[CmdletBinding()]
Param
(
[Parameter(
Position=0,
ValueFromPipeLine=$true,
ValueFromPipeLineByPropertyName=$true)]
[alias("CN","MachineName","Name","Computer")]
[string[]]$ComputerName = $ENV:ComputerName
)
Begin {
$pipelineInput = -not $PSBoundParameters.ContainsKey('ComputerName')
}
Process
{
Function DoWork([string]$ComputerName) {
$ActiveMonitors = Get-WmiObject -Namespace root\wmi -Class wmiMonitorID -ComputerName $ComputerName
$monitorInfo = @()
foreach ($monitor in $ActiveMonitors)
{
$mon = $null
$mon = New-Object PSObject -Property @{
#ManufacturerName=($monitor.ManufacturerName | % {[char]$_}) -join ''
ManufacturerName=($monitor.ManufacturerName | % {[char]$_}) -join ''
ProductCodeID=($monitor.ProductCodeID | % {[char]$_}) -join ''
SerialNumberID=($monitor.SerialNumberID | % {[char]$_}) -join ''
UserFriendlyName=($monitor.UserFriendlyName | % {[char]$_}) -join ''
ComputerName=$ComputerName
WeekOfManufacture=$monitor.WeekOfManufacture
YearOfManufacture=$monitor.YearOfManufacture}
$monitorInfo += $mon
}
#Write-Output $monitorInfo
$monitorInfostring = $monitorInfo.toString()
return $monitorInfo
}
if ($pipelineInput) {
DoWork($ComputerName)
} else {
foreach ($item in $ComputerName) {
DoWork($item)
}
}
}
}
function sendMail{
#Write-Host "Sending Email"
#SMTP server name
$smtpServer = "X.X.X.X"
#Creating a Mail object
$msg = new-object Net.Mail.MailMessage
#Creating SMTP server object
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
#Email structure
$msg.From = "test@test.org"
$msg.ReplyTo = "test@test.org"
$msg.To.Add("test@test.org")
$msg.subject = "My Subject"
$BodyInfo = Get-MonitorInfo
$msg.body = $BodyInfo
write-output $BodyInfo
#Sending email
$smtp.Send($msg)
}
#Calling function
sendMail