How to run Powershell code using AHK

Get help with using AutoHotkey and its commands and hotkeys
WalkerOfTheDay
Posts: 458
Joined: 24 Mar 2016, 03:01

How to run Powershell code using AHK

25 Apr 2017, 07:04

I am using following PowerShell code to send out email without the need of Outlook / Blat or other
email client, and it works perfectly.

What I would like to know, if there is a way to run this powershell code from a compiled Autohotkey script,
without saving below code into a .ps1 file (because that would expose the username / password.

Code: Select all

$EmailFrom = "[email protected]"
$EmailTo = "[email protected]" 
$Subject = "PowerShell Test"
$Body = "Hi There !"
$SMTPServer = "smtp.provider.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("SomeUserName", "SomePassword"); 
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
Eventually I would like to write some kind of gui in which I could type an email and send it out
using AHK, but first I need to figure out how to run above code.

Any ideas ?
guest3456
Posts: 2537
Joined: 09 Oct 2013, 10:31

Re: How to run Powershell code using AHK

25 Apr 2017, 11:39

I just use this AHK code:

Code: Select all


SendEmail("[email protected]", "fb sucks", "myspace was better")


SendEmail(receiver, subject, message)
{
   fields := Object()
   fields.sendusername := "[email protected]"
   fields.sendpassword := "password"
   fields.smtpserverport := 465
   fields.smtpserver := "smtp.gmail.com"
   fields.smtpusessl := True
   fields.sendusing := 2                      ; cdoSendUsingPort
   fields.smtpauthenticate := 1               ; cdoBasic authentication (plain-text)
   fields.smtpconnectiontimeout := 60
   schema := "http://schemas.microsoft.com/cdo/configuration/"

   pmsg := ComObjCreate("CDO.Message")
   pmsg.To := receiver
   pmsg.Subject := subject
   pmsg.TextBody := message
   pmsg.From := fields.sendusername

   pfld := pmsg.Configuration.Fields
   for field,value in fields
       pfld.Item(schema . field) := value
   pfld.Update()
   pmsg.Send()
}

WalkerOfTheDay
Posts: 458
Joined: 24 Mar 2016, 03:01

Re: How to run Powershell code using AHK

25 Apr 2017, 11:42

Thanks for your feedback.

I know about the Run function, but it's not clear to me What you are trying to say.

I know how to use Run to execute a .Ps1 file, but like I said this exposes the username and password, which I'm trying to avoid.
WalkerOfTheDay
Posts: 458
Joined: 24 Mar 2016, 03:01

Re: How to run Powershell code using AHK

26 Apr 2017, 05:26

Thank you guest3456, I will give it a try.

In the meantime, I (kinda) got the PowerShell option working, see below.

Unfortunatly it does require to generate a .ps1 file which contains the credentials.
But after one second of running the script it is automatically deleted, so the risk
is minimized.

Code: Select all

#NoEnv
#Warn
#SingleInstance Force
SetBatchLines, -1
SendMode, Input

FileDelete, email.ps1 

SMTPServer				= "smtp.someprovider.com"
SMTPClient				= New-Object Net.Mail.SmtpClient($SMTPServer, 587)
SMTPClientEnableSsl 	= $true
SMTPClientCredentials 	= New-Object System.Net.NetworkCredential("[email protected]", "SomePassword");

EmailFrom 	= "[email protected]"
EmailTo		= "[email protected]"
Subject 	= "NSA hacktool"
Body 		= "Hi Marc, I'm glad to inform you my NSA hacktool is ready"

FileAppend, $SMTPServer = %SMTPServer%`n, email.ps1
FileAppend, $SMTPClient = %SMTPClient%`n, email.ps1
FileAppend, $SMTPClient.EnableSsl = %SMTPClientEnableSsl%`n, email.ps1
FileAppend, $SMTPClient.Credentials = %SMTPClientCredentials%`n, email.ps1
FileAppend, $EmailFrom = %EmailFrom%`n, email.ps1
FileAppend, $EmailTo = %EmailTo%`n, email.ps1
FileAppend, $Subject = %Subject%`n, email.ps1
FileAppend, $Body = %Body%`n, email.ps1
FileAppend, $SMTPClient.Send($EmailFrom`, $EmailTo`, $Subject`, $Body)`n, email.ps1
run, powershell.exe -ExecutionPolicy Bypass -File email.ps1

Sleep, 1000
FileDelete, email.ps1 
shipaddicted
Posts: 19
Joined: 15 Jul 2016, 19:57

Re: How to run Powershell code using AHK

22 Dec 2018, 21:27

This is working great for me so far. However, I would like the options of adding cc: and attachments. Does anyone know how I can add these variables? I have tried
pmsg.Add
pmsg.To.Add

for the cc, both indicating ":= cc" to define the variable and adding ", cc," after the 2nd parameter in the function.
SendEmail("[email protected]", [email protected], "subject", "body")

SendEmail(receiver, cc, subject, message)
I haven't gotten to the attachment yet. One hurdle at a time.
guest3456 wrote:
25 Apr 2017, 11:39
I just use this AHK code:

Code: Select all


SendEmail("[email protected]", "fb sucks", "myspace was better")


SendEmail(receiver, subject, message)
{
   fields := Object()
   fields.sendusername := "[email protected]"
   fields.sendpassword := "password"
   fields.smtpserverport := 465
   fields.smtpserver := "smtp.gmail.com"
   fields.smtpusessl := True
   fields.sendusing := 2                      ; cdoSendUsingPort
   fields.smtpauthenticate := 1               ; cdoBasic authentication (plain-text)
   fields.smtpconnectiontimeout := 60
   schema := "http://schemas.microsoft.com/cdo/configuration/"

   pmsg := ComObjCreate("CDO.Message")
   pmsg.To := receiver
   pmsg.Subject := subject
   pmsg.TextBody := message
   pmsg.From := fields.sendusername

   pfld := pmsg.Configuration.Fields
   for field,value in fields
       pfld.Item(schema . field) := value
   pfld.Update()
   pmsg.Send()
}
shipaddicted
Posts: 19
Joined: 15 Jul 2016, 19:57

Re: How to run Powershell code using AHK

23 Dec 2018, 13:52

Nevermind! I found all the fields I need on a different thread!

Return to “Ask For Help”

Who is online

Users browsing this forum: Google [Bot], Odlanir, Portwolf, translatorgreek and 169 guests