UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Run Powershell Commands From VBA    
 
   
David92595
post Jul 14 2017, 05:11 PM
Post#1



Posts: 522
Joined: 27-June 11
From: Temecula, California


This is the closest forum I could find for running powershell commands. Please let me know if this post is better placed elsewhere.

I need to send an email which I am doing via powershell (below). Part of the coding is pulling the active users email from AD.
My current issue is how to call from within in Microsoft Access. I'm found ways to run this if I wanted to save the script as a .PS or .tst file. But I'm looking to keep it all within access.
**This code works in Powershell**

CODE
$searcher = [adsisearcher]"(samaccountname=$env:USERNAME)"
$FromEmailAddress = $searcher.FindOne().Properties.mail
$body = "Test 1"

send-mailmessage -from $FromEmailAddress -to "David92595@domain.com" -subject "Testing" -body $body -smtpServer mail.domain.com



Any idea's?

Thank You!
David92595
Go to the top of the page
 
David92595
post Jul 14 2017, 05:46 PM
Post#2



Posts: 522
Joined: 27-June 11
From: Temecula, California


I've worked out the string. Still now sure how to call the powershell command and insert my variable correctly.

CODE
Public Function SendMailwShell()
Dim Email As String
Dim retval

Email = "" & "$searcher = [adsisearcher]" & """" & "(samaccountname=$env:USERNAME)" & """" & "; $FromEmailAddress = $searcher.FindOne().Properties.mail; $body = """ & "Testing_1""" & "; send-mailmessage -from $FromEmailAddress -to """ & "david.katanski@mtglawfirm.com""" & " -subject " & """" & "Testing""" & " -body $body -smtpServer smtp"
Debug.Print Email

retval = Shell("powershell email")

MsgBox retval

End Function

This post has been edited by David92595: Jul 14 2017, 05:55 PM
Go to the top of the page
 
cheekybuddha
post Jul 15 2017, 05:56 AM
Post#3


UtterAccess VIP
Posts: 9,063
Joined: 6-December 03
From: Telegraph Hill


Surely:
CODE
retval = Shell("powershell " & Email)


?

d

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


Regards,

David Marten
Go to the top of the page
 


Custom Search
RSSSearch   Top   Lo-Fi    24th July 2017 - 01:29 PM