简体   繁体   中英

Powershell Email Issue

I am running this code, and it runs perfect on my PC. Runs the exact way that i want it to, but when I tried to test it on an end-users computer (2 users actually), I received an error in regards to almost everything that is in regards to Outlook. I am not sure why I am receiving this error on other users PC's, but not my own. This is my first program in Powershell, and it is very basic. The code has not been cleaned up yet. I make them work first, and then clean them up.

$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)

$USEmail = "Example@example.com"

#Enter who the email will be sent to
$Mail.To = "Example@example.com"
$Mail.CC = "Example@example.com"


#email will be sent to the two above email address, Please insert ( email; email; email; ) in that format when adding new email addresses to list



#Get the users IP Address
$HostIP = (
    Get-NetIPConfiguration |
    Where-Object {
        $_.IPv4DefaultGateway -ne $null -and
        $_.NetAdapter.Status -ne "Disconnected"
    }
).IPv4Address.IPAddress
#end of code for IP address. This code is currently not being used in the program, as the IP address is not as necessary for computers with Teamviewer


#Setting up the variables that will hold the information to be displayed for helpdesk and usa it at the end of the email
$computerOS = Get-CimInstance CIM_OperatingSystem
$computerCPU = Get-CimInstance CIM_Processor
$computerSystem = Get-CimInstance CIM_ComputerSystem
$computerBIOS = Get-CimInstance CIM_BIOSElement

$SerialNum = "Serial Number: " + $computerBIOS.SerialNumber


$computerHDD = Get-CimInstance Win32_LogicalDisk -Filter "DeviceID = 'C:'"
Clear-Host

#Get the computer model and display it back in the email
$CompModel = (Get-WmiObject -Class:Win32_ComputerSystem).Model
#Get the computer name
$CompName = hostname
# All variables are hosted above.


#Gather user information and add it back to the email for IT 
$CompCPU = "CPU: " + $computerCPU.Name
$CompCap = "HDD Capacity: "  + "{0:N2}" -f ($computerHDD.Size/1GB) + "GB"
$CompSpace = "HDD Space: " + "{0:P2}" -f ($computerHDD.FreeSpace/$computerHDD.Size) + " Free (" + "{0:N2}" -f ($computerHDD.FreeSpace/1GB) + "GB)"
$RamPerc = "RAM: " + "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"
$CompOS = "Operating System: " + $computerOS.caption + ", Service Pack: " + $computerOS.ServicePackMajorVersion
$loggedIn =  $computerSystem.UserName
$LastReboot = "Last Reboot: " + $computerOS.LastBootUpTime
$CompDate = (Get-Date).ToString('MM/dd/yyyy hh:mm:ss tt')


#Add Subject to the email
$Mail.Subject = "$loggedIn  Date: $CompDate" 
#end subject to email


#Link For Teamviewer
$TeamV = "http://download.teamviewer.com/download/version_11x/TeamViewerQS.exe"



#Start to the Body of the email
$Mail.HTMLBody ="<h1>Frutarom IT Help Request Form</h1>
<br>
Printer Name: 

<br><br>


Teamviewer ID: 
<br>
Teamviewer Password:
<br><br>
<strong>Download Teamviewer Below:</strong>
<br>
$TeamV
<br><br>
Brief Description of Issue: 


<br><br><br>
<h2>---------------------------------------------------------------------------------------------------------------------</h2><br>

<h4>Information For IT:</h4>
<br><br>
Computer Model: <i>$CompModel</i><br>
Computer Name: <i>$CompName</i> <br>
RAM: $RamPerc<br>
$SerialNum<br>
$CompCPU<br>
$CompCap <br>
$CompSpace <br>
$CompOS <br>
User logged In: $loggedIn <br>
$LastReboot <br>

"
#end of body of code, and all HTML that will be displayed in the body of the email





#Save the mail and open it for the user to be able to review and edit
$mail.save()
$inspector = $mail.GetInspector
$inspector.Display()

#end of code

enter image description here

Do the other users have an outlook profile set-up? If only outlook is installed but no profile is set-up your second line will throw an error.

$Mail = $Outlook.CreateItem(0)

I believe your approach does not allow to automatically send an e-mail, hence a bit unsure about your last code comment or indeed a review is required.

#Save the mail and open it for the user to be able to review and edit...

If the review is not necessary then it would be quicker/sleeker to use the built in PowerShell commandlet

$MailBody = "<h1>Frutarom IT Help Request Form</h1>..."
Send-MailMessage -From 'Example01 <Example@example.com>' -To 'Example02 <Example@example.com>' -Subject "$loggedIn  Date: $CompDate"  -Body $Mailbody -BodyAsHtml # other parameters

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/send-mailmessage?view=powershell-6

Big+: Less headache at the end as no outlook, no profile requirement in order make your script work on any PC.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM