Help with trying to send the output in an email


i trying send output of script in body of email can't work right, please help. can emails, there nothing in body.

thanks,

lynn

cls
 
function get-domaincomputeraccounts
 
{
 
# use directory services object attach domain
$searcher = new-object directoryservices.directorysearcher([adsi]"")
#leaving adsi statement empty = attach root domain  
 
# filter down computer accounts
$searcher.filter = "(&(objectclass=computer))"
 
# cache results
$searcher.cacheresults = $true
$searcher.searchscope = “subtree”
$searcher.pagesize = 1000
 
# find can matches definition of being computer object
$accounts = $searcher.findall()
 
# check make sure found accounts
if($accounts.count -gt 0)
{
foreach($account in $accounts)
    {
# property contains last password change in long integer format
$pwdlastset = $account.properties["pwdlastset"];
 
# convert long integer normal datetime format
$lastchange = [datetime]::fromfiletimeutc($pwdlastset[0]);
 
# determine timespan between 2 dates
$datediff = new-timespan $lastchange $(get-date);
 
# create output object table formatting
$obj = new-object psobject;
 
# add member properties name , value pair
$obj | add-member noteproperty computername($account.properties["name"][0]);
$obj | add-member noteproperty lastpasswordchange($lastchange);
$obj | add-member noteproperty dayssincechange($datediff.days);
 
# write output screen
write-output $obj;
}
}
}
$body = $obj | out-string

$emailfrom = "noreply@domain.com"
$emailto = "me@domain"
$subject = "message subject"
$smtpserver = "xxx.xxx.xxx.xxx"
$smtp = new-object net.mail.smtpclient($smtpserver)
$mailmessage = new-object net.mail.mailmessage($emailfrom, $emailto, $subject, $body)
$mailmessage.isbodyhtml = $true
$mailmessage.replyto = "noreply@domain.com"
$smtp.send($mailmessage)

it doesn't you're calling function. try this...

$body=get-domaincomputeraccounts | out-string

you should remove $mailmessage.isbodyhtml = $true function output not html.

if need html can this:

$body=get-domaincomputeraccounts | convertto-html

get-help convertto-html -full

to see else cmdlet can do.


admiral ackbar says...



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Edit Group Policy

Hyper-V VM not reaching OS 'Logon' screen

DNS question...