locked
Convert PS1 to POwershell Command Line or Batch RRS feed

  • Question

  • Hi Folks -

    I have an email ps1 script that I want to convert to be used called from the batch command line. Can someone help me convert this? I"m not sure of all the switches.  Here is my ps1:

    $emailSmtpServer = "SMTP.Office365.com"
    $emailSmtpServerPort = "587"
    $emailSmtpUser = "alerts@client.com"
    $emailSmtpPass = "password"
     
    $emailFrom = "alerts@client.com"
    $emailTo = "recipient@email.com"
    $emailcc="recipient2@email.com"
     
    $emailMessage = New-Object System.Net.Mail.MailMessage( $emailFrom , $emailTo )
    $emailMessage.cc.add($emailcc)
    $emailMessage.Subject = "Test Subject" 
    $emailMessage.Body = "Test Body"
     
    $SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort )
    $SMTPClient.EnableSsl = "true"
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass );
    $SMTPClient.Send( $emailMessage )

    Here is what I"M trying to do via batch:

    @ECHO OFF
    
    SET "EXEC_POLICY=-ExecutionPolicy ByPass"
    SET "COMMAND=-Command Send-MailMessage"
    SET "SMTP_SERVER=SMTP.Office365.com -Port 587 -UseSsl"
    SET "TO_EMAIL=recipient@email.com"
    SET "FROM_EMAIL=alerts@client.com"
    SET "SUBJECT='Test Subject'"
    SET "BODY='Test Body'"
    SET "PASSWD='Password'"
    
    powershell %EXEC_POLICY% %COMMAND% ^
        -SmtpServer %SMTP_SERVER% ^
        -To %TO_EMAIL% ^
        -From %FROM_EMAIL% ^
        -Subject %SUBJECT% ^
        -Body %BODY%
    	-credential New-Object System.Net.NetworkCredential( %FROM_EMAIL% , "%PASSWD%" )
    	
    pause

    Thank you!

    Saturday, March 28, 2020 12:04 PM

All replies

  • Doesn't need converting just run it at the command line:

    powershell -file the script.ps1

    Simple as that.  Why  would you uninvent the wheel.  Would you give up your car to use a horse carriage?

    CMD and batch are obsolete and will soon be removed from Windows.  They should never be used.

    You cannot create credentials from a batch file.


    \_(ツ)_/

    Saturday, March 28, 2020 12:20 PM