none
Credential use in Send-MailMessage, can't get it to work programmatically RRS feed

  • Question

  • I have been working on this for 4 months and can't make it work except interactively, i.e., I have to enter my login name and pwd, manually.  I got this code from the forum from "jrv". Here's my code:


    ##This works perfectly but interactively! I even received and read the message.

    $mailprops = @{
            ##User = 'me@mysite.com'
            ##Pword = ConvertTo-SecureString -String "xxxxxxxx" -AsPlainText -Force
            ##Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList User, Pword

        From = 'me@verizon.net'
        To = 'you@verizon.net'
        Subject = 'PowerShell test of sending an email using your-site in e93.ps1'
        Body = 'PowerShell test of sending an email using your-site in e93.ps1'
        Attachments = 'D:\DOWNLOAD\xcopy.txt'
        SMTPServer = 'smtp.your-site.com'
        Port = '587'
        UseSSL = $true
        Credential = Get-Credential

        ##Credential = 'Cred'
        ##Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "them@verizon.net", "yyyyyyyyy"
        ##Credential = 'Cred'
        ##Credential = "www.gmail.com\mygmail"
        ##Credential = 'PSCredential'
    }

    Send-MailMessage @mailprops

    ------------------------------------------------
    ##This doesn't work from the PowerShell cmdline!

    $mailprops = @{
        User = 'me@mysite.com'
        Pword = ConvertTo-SecureString -String "xxxxxxxxx" -AsPlainText -Force
        Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList User, Pword
        From = 'me@verizon.net'
        To = 'you@verizon.net'
        Subject = 'PowerShell test of sending an email using your-site in e93.ps1'
        Body = 'PowerShell test of sending an email using your-site in e93.ps1'
        Attachments = 'D:\DOWNLOAD\xcopy.txt'
        SMTPServer = 'smtp.your-site.com'
        Port = '587'
        UseSSL = $true
        Credential = 'Cred'

            ##Credential = Get-Credential
            ##Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "them@verizon.net", "yyyyyyyyy"
        ##Credential = 'Cred'
        ##Credential = "www.gmail.com\mygmail"
        ##Credential = 'PSCredential'
    }

    Send-MailMessage @mailprops

    ------------------------------------------------
    Here is the run and the errors:

    PS D:\DOWNLOAD\PROGRAMS\POWERSHELL> .\e93

    New-Object : Cannot find an overload for "PSCredential" and the argument count: "2".
    At D:\DOWNLOAD\PROGRAMS\POWERSHELL\e93.ps1:11 char:12
    +     Cred = New-Object -TypeName System.Management.Automation.PSCreden ...
    +            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodException
        + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

    Send-MailMessage : Cannot validate argument on parameter 'To'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

    At D:\DOWNLOAD\PROGRAMS\POWERSHELL\e93.ps1:28 char:18
    + Send-MailMessage @mailprops
    +                  ~~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Send-MailMessage], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.SendMailMessage
    ------------------------------------------------

    This is driving me nuts. Just can't get it.  Any Help, that actually works, will be greatly appreciated!  
    Thank you all Greatly in advance!
    Crank

    Wednesday, February 10, 2016 5:24 PM

Answers

  • UPDATE -- UPDATE

    You gentlemen are slightly subtle in your approach to teach.  Here's what I did with my code:

    # e94.ps1
    $User = 'jeff@me.com'
    $Pword = ConvertTo-SecureString -String "xxxxpwd" -AsPlainText -Force

    $mailprops = @{
        From = 'jeff@me.com'
        To = 'me@verizon.net'
        Subject = 'PowerShell test of sending an email using your-site in e94.ps1'
        Body = 'PowerShell test of sending an email using your-site in e94.ps1'
        Attachments = 'D:\DOWNLOAD\xcopy.txt'
        Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $Pword
        SMTPServer = 'smtp.your-site.com'
        Port = '587'
        UseSSL = $true
    }

    Send-MailMessage @mailprops

    ---------------------------------------------------------------

    So, I took out the fake or bad params User and Pword and put them outside the @bulk (sp?) and I took out the Cred param and went straight in with the Credential line of code.

    To my vast surprise, the Shell waited 2 seconds and came back to another Shell line!  Like, Yikes!  It worked!  But I had to kinda translate your subtleties.  Might as well close this thread, now!  Tremendously Great thanks!!!!  Now, I have to call it from a batch file from Task Scheduler.

    Crank

    • Proposed as answer by BASATI Thursday, February 11, 2016 7:32 AM
    • Marked as answer by Bill_StewartModerator Monday, March 7, 2016 10:37 PM
    Wednesday, February 10, 2016 7:50 PM

All replies

  • You cannot place undefined parameters in a @splat.  Place them outside the @splat.

    https://technet.microsoft.com/en-us/library/jj672955.aspx?f=255&MSPPError=-2147217396


    \_(ツ)_/

    • Proposed as answer by jrv Wednesday, February 10, 2016 6:58 PM
    Wednesday, February 10, 2016 5:33 PM
  • Read this first:


    PS C:\> help about_Splatting

    This provides a good overview about splatting. What you are concerned with is how to pass parameters to a command using a hash table - @{ } syntax.

    Now that you understand that you are using a hash table, the next thing to understand is what a hash table is. Read this:


    PS C:\> help about_Hash_Tables

    As jrv noted, it is not useful to put undefined values into the hash table, because the command will fail, just as if you typed an undefined or null parameter on the command line.

    So what you need to do instead is to add the Credential parameter only when needed. Here is a short function that demonstrates the concept:


    function ParamTest {
      param(
        [String] $TestValue
      )
      
      $paramHash = @{
        "Parameter1" = "Value1"
        "Parameter2" = "Value2"
      }
      
      if ( $TestValue ) {
        $paramHash.Add("TestValue", $TestValue)
      }
      $paramHash
      # The hash table will always contain Parameter1 and Parameter2,
      # but it won't contain TestValue unless you specify something for it.
    }
    


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by jrv Wednesday, February 10, 2016 6:58 PM
    Wednesday, February 10, 2016 5:57 PM
    Moderator
  • Thanks Bill !

    I just ran this (see nice 'n tight) with the errors:

    # e94.ps1
    $mailprops = @{
            From = 'jeff@ca.com'
            To = 'me@verizon.net'
            Subject = 'PowerShell test of sending an email using your-site in e94.ps1'
            Body = 'PowerShell test of sending an email using your-site in e94.ps1'
            Attachments = 'D:\DOWNLOAD\xcopy.txt'
            User = 'jeff@ca.com'
            Pword = ConvertTo-SecureString -String "xxxxxxxpwd" -AsPlainText -Force
            Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList User, Pword
        SMTPServer = 'smtp.your-site.com'
        Port = '587'
        Credential = 'Cred'
        UseSSL = $true
    }

    Send-MailMessage @mailprops

    ---------
    PS D:\DOWNLOAD\PROGRAMS\POWERSHELL> .\e94

    New-Object : Cannot find an overload for "PSCredential" and the argument count: "2".
    At D:\DOWNLOAD\PROGRAMS\POWERSHELL\e94.ps1:11 char:12
    +     Cred = New-Object -TypeName System.Management.Automation.PSCreden ...
    +            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodException
        + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

    Send-MailMessage : Cannot validate argument on parameter 'To'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
    At D:\DOWNLOAD\PROGRAMS\POWERSHELL\e94.ps1:18 char:18
    + Send-MailMessage @mailprops
    +                  ~~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Send-MailMessage], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.SendMailMessage
       

    Wednesday, February 10, 2016 6:45 PM

  • New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList User, Pword

    So here I am guessing that you are trying to create a credential object with a username and password?

    The constructor for the PSCredential object won't accept a plain-text string. It only works with a SecureString object.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, February 10, 2016 6:59 PM
    Moderator
  • Are "User" and "Pword" parameters for Send-MailMessage?  Did you read the help for the CmdLet?

    \_(ツ)_/

    Wednesday, February 10, 2016 7:00 PM
  • I got the code for User and Pword right off the forum, and I got the basis cmdlet code right from jrv.  Jrv helped me a lot with the Send-MailMessage cmdlet, but later suggested I start a new thread about my Credential troubles.

    My cmdlet uses this:  ConvertTo-SecureString -String "xxxxxxxpwd"   to get the SecureString.

    Yes, not only have I read very many help files, but have been compiling a whole book into which I cat the help files, and so on.

    The cmdlet works interactively but not from a PS cmd line.  Would you kindly show me the correct code to get solve the errors? Thanks Bill and jrv.

    Wednesday, February 10, 2016 7:22 PM
  • UPDATE -- UPDATE

    You gentlemen are slightly subtle in your approach to teach.  Here's what I did with my code:

    # e94.ps1
    $User = 'jeff@me.com'
    $Pword = ConvertTo-SecureString -String "xxxxpwd" -AsPlainText -Force

    $mailprops = @{
        From = 'jeff@me.com'
        To = 'me@verizon.net'
        Subject = 'PowerShell test of sending an email using your-site in e94.ps1'
        Body = 'PowerShell test of sending an email using your-site in e94.ps1'
        Attachments = 'D:\DOWNLOAD\xcopy.txt'
        Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $Pword
        SMTPServer = 'smtp.your-site.com'
        Port = '587'
        UseSSL = $true
    }

    Send-MailMessage @mailprops

    ---------------------------------------------------------------

    So, I took out the fake or bad params User and Pword and put them outside the @bulk (sp?) and I took out the Cred param and went straight in with the Credential line of code.

    To my vast surprise, the Shell waited 2 seconds and came back to another Shell line!  Like, Yikes!  It worked!  But I had to kinda translate your subtleties.  Might as well close this thread, now!  Tremendously Great thanks!!!!  Now, I have to call it from a batch file from Task Scheduler.

    Crank

    • Proposed as answer by BASATI Thursday, February 11, 2016 7:32 AM
    • Marked as answer by Bill_StewartModerator Monday, March 7, 2016 10:37 PM
    Wednesday, February 10, 2016 7:50 PM
  • The most important lesson to learn with computer technology is how to read the instructions.  Now that you have understood why that is important you will certainly ask much more difficult questions in the future.

    Have fun with PowerShell.


    \_(ツ)_/

    Wednesday, February 10, 2016 7:59 PM