0

1) How can I store my credentials in this script - it's prompting me for credentials

2) Should the last two lines be at the bottom or before the params? I am getting this error - I think I am not passing my credentials correctly

Send-MailMessage : The remote name could not be resolved: 'System.Net.Mail.SmtpClient' At line:21 char:1 + Send-MailMessage @smtpMessage Blockquote

    param (
    [string]$Path = "C:\Users\me\Desktop\ScanFolder",
    $From = "me@msn.com",
    $To = "you@msn.com",
    $Subject1 = "Changes Found",
    $Subject2 = "No Changes in last x minutes",
    $Body = "anything",
    $SMTPServer = "smtp.live.com",
    $SMTPPort = "587",
    [PSCredential]$Credential #pass in credential
)
$smtpMessage = @{
    To = $To
    From = $From
    Subject = $Subject
    Smtpserver = $SMTPClient
    Credential = $Credential
    BodyAsHtml = $true
}

Send-MailMessage @smtpMessage

$secpasswd = ConvertTo-SecureString "mypassword" -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential ("me@msn.com", $secpasswd)
MG2016
  • 289
  • 6
  • 32
  • Possible duplicate of [How to pass credentials to the Send-MailMessage command for sending emails](https://stackoverflow.com/questions/12460950/how-to-pass-credentials-to-the-send-mailmessage-command-for-sending-emails) –  Jun 09 '18 at 19:57

1 Answers1

3

Whether for email of otherwise, you can either enter your creds in plain text in the script (as you are doing now), but really an ill-advised approach or store them in a secure file (the better option) and call them as needed.

There are tons of examples of this thought process all over the web and this forum as well. For example:

Quickly and securely storing your credentials – PowerShell

https://www.jaapbrasser.com/quickly-and-securely-storing-your-credentials-powershell

# To get a credential object we can either manually create one or use the Get-Credential cmdlet to prompt for the account details:

$Credential = Get-Credential

# To store the credentials into a .cred file:

$Credential | Export-CliXml -Path "${env:\userprofile}\Jaap.Cred"

# And to load the credentials from the file and back into a variable:

$Credential = Import-CliXml -Path "${env:\userprofile}\Jaap.Cred"
Invoke-Command -Computername 'Server01' -Credential $Credential {whoami}

Storing PowerShell Credentials in JSON

https://jdhitsolutions.com/blog/powershell/5396/storing-powershell-credentials-in-json

$secure = ConvertTo-SecureString -String 'P@$$w0rd' -AsPlainText -Force
$cred = New-Object -typename PSCredential -ArgumentList @('company\admin',$secure)
$cred | Export-clixml c:\work\admin.xml

Securely Store Credentials on Disk

http://powershellcookbook.com/recipe/PukO/securely-store-credentials-on-disk

# The first step for storing a password on disk is usually a manual one. There is nothing mandatory about the filename, but we’ll use a convention to name the file CurrentScript.ps1.credential. Given a credential that you’ve stored in the $credential variable, you can safely use the Export-CliXml cmdlet to save the credential to disk. Replace CurrentScript with the name of the script that will be loading it:
PS > $credPath = Join-Path (Split-Path $profile) 

# CurrentScript.ps1.credential
PS > $credential | Export-CliXml $credPath

# In PowerShell version 2, you must use the ConvertFrom-SecureString cmdlet:

PS > $credPath = Join-Path (Split-Path $profile) CurrentScript.ps1.credential
PS > $credential.Password | ConvertFrom-SecureString | Set-Content $credPath

And this is a common question and thus your post request could be considered a duplicate of these:

Using PowerShell credentials without being prompted for a password

How to pass credentials to the Send-MailMessage command for sending emails

Community
  • 1
  • 1
postanote
  • 15,138
  • 2
  • 14
  • 25
  • Thanks for this, managed to get the credentials working without the prompt. Will try storing credentials in a file as suggested. – MG2016 Jun 09 '18 at 21:11
  • Seem to have an issue when changing to using my gmail account: Exception calling "Send" with "1" argument(s): "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Could it be because I have two-factor authentication? – MG2016 Jun 09 '18 at 21:27
  • 1
    Be sure you are using '-Usessl true' in your code, as is also noted in the last link in my response , 2FA is a whole different thing. – postanote Jun 11 '18 at 03:39
  • Got it to work - had to create an app password for gmail account. – MG2016 Jun 11 '18 at 13:30
  • Glad you were successful. 2FA is a good thing, but we all must plan for implications of using it. – postanote Jun 12 '18 at 00:11