How do I emulate or duplicate the "include" feature of powershell

How do I emulate or duplicate the “include” feature of powershell (. D:\script_common\MyCode.ps1)

essentially I want to include a standard set of wrappers in all my procedures that allow me to export output to any of the 5 outputs types (as seen in the example below)

Switch ($_.logtype)
{
“email” {SendEmail(data)}
“computerlogfile” {WriteComputerLogFile(data)}
“computereventlog” {WriteComputerEventLog(data)}
“comodolog” {WritetoComodoLog(data)}
“webservice” {WritetoWebService(data)}
default {}
}

End of switch block

for example based on a certain action I can just write to the Comodo log but if it’s a critical action I can send an email or write to the local event log.

what I don’t want is to have to include and paste the same 100 lines of code in every script each and every time.

I have each individual script but I’m tired of having to include a lot of reusable code in every procedure

I didn’t know if there was a way to “include” another existing procedure

My current thought is to write a small procedure included in each script that downloads the global wrapper script and then I can just include that from a predetermined path on the hard drive.

I just wanted to see if anyone one else was doing something different or if there was a way to do this better.

Hi @smartnetworks

Sounds great request and idea that we have planned to work on it : ). Let us completely test and come up with a sample script to include scripts from your location by tomorrow.

Thanks
Kannan

Hi @smartnetworks

FYI

We have found a way to include reusable codes as a separate module from system location. Right now, we are creating example scripts for better illustrations and we will inform you soon.

Hi ,
Please find the attachments of two scripts below,

1.The First script lets you create the reusable codes as a separate module in the given path.

For better understanding, we are creating following four reusable modules with .py in the path viz,
1)executing command
2)sending email
3)system info
4)Appending Data to file

2.The Second script lets you include reusable codes as unique module whenever it is required.
For Example if you need send email, you can write program as below,

path=r"C:
ewmodule"
import sys
sys.path.insert(0,path)
import sendemail
print "SENDING Email
"
body=“Hi we are testing”
subject=“Test”
recepients=[‘XXXXXX@YYYYYY’]
sendemail.mail(subject,body,recepients)

Note: Please run the script as system user in order to avoid permission issues.
In both scripts Specify path for better output

Let us know the feedback.



path=r"C:
ewmodule"
import os
if not os.path.isdir(path):
    os.makedirs(path)
ki="""
import os
def info():
    ki=os.popen('systeminfo | findstr /C:"OS Name"').read()
    print ki
    ki=os.popen('systeminfo | findstr /C:"OS Version"').read()
    print ki
    ki=os.popen('systeminfo | findstr /C:"Product ID"').read()
    print ki
    ki=os.popen('systeminfo | findstr /C:"System Model"').read()
    print ki
    ki=os.popen('systeminfo | findstr /C:"System Type"').read()
    print ki
"""
path1=path+"\sysinfo.py"
f=open(path1,"wb")
f.write(ki)
f.close()
print "File Created Successfully for sysinfo"

ki="""

import os
import ctypes
global c
c=0
def emailreport(subject, emailto,emailfrom,password,smtpserver,port,msgbody):

    import smtplib
    import mimetypes
    from email.mime.multipart import MIMEMultipart
    from email import encoders
    from email.message import Message
    from email.mime.audio import MIMEAudio

    from email.mime.base import MIMEBase
    from email.mime.image import MIMEImage
    from email.mime.text import MIMEText
    import os
    msg = MIMEMultipart()
    msg["From"] = emailfrom
    msg["To"] = ",".join(emailto)
    msg["Subject"] = subject
    msg.preamble = subject
    body = MIMEText(msgbody)
    msg.attach(body)        

    server = smtplib.SMTP(smtpserver,port)
    server.ehlo()
    server.starttls()
    server.login(emailfrom, password)
    server.sendmail(emailfrom, emailto, msg.as_string())
    server.quit()
    return " "+msg["To"]

def mail(subject,body,mailaddress):
    sendmail=1
    msgbody=body
    subject=subject
    emailto=mailaddress#E-mail To 
    emailfrom='testcomodomail1@gmail.com'#Give your from addrees
    password='C0m0d0@123'#Password
    smtpserver='smtp.gmail.com'
    import os,sys,csv,urllib2,zipfile,shutil,re,sys,socket,_winreg,random,getpass
    import xml.etree.ElementTree as ET
    a=[]
    b=[]
    c=[]
    print "USER NAME: "+getpass.getuser()
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.connect(("8.8.8.8", 80))
    print "IP-ADDRESS : "+(s.getsockname()[0])
    from time import gmtime, strftime
    time=strftime("%Y-%m-%d %H:%M:%S", gmtime())
    port=587
    if sendmail==1:
        print "Email has sent with Mail Address"+ emailreport(subject,emailto,emailfrom,password,smtpserver,port,msgbody)

"""
path2=path+"\sendemail.py"
f=open(path2,"w")
f.write(ki)
f.close()
print "File Created Successfully  sendmail"

ki="""
import os
def info(command):
    ki=os.popen(command).read()
    print ki
"""
path3=path+"\excutecmd.py"
f=open(path3,"wb")
f.write(ki)
f.close()
print "File Created Successfully ipconfig "

ki="""
def addfile(data):    
    f=open("C:\log.txt","ab")
    f.write(data)
    f.close

"""
path4=path+"\wfile.py"
f=open(path4,"w")
f.write(ki)
f.close()
print "File Created Successfully wfile "


Script 2 :


path=r"C:
ewmodule"
import sys
sys.path.insert(0,path)
import sysinfo
import excutecmd
import wfile
import sendemail
print "****************"
print "Excute command"
print excutecmd.info("ipconfig")


print "********************"

print "SYSTEM INFO
 "
print sysinfo.info()
print "*************"

print "SENDING Email
"
body="Hi we are testing"
subject="Test"
recepients=['YYYYYYYYY@XXXXXX.com','YYYYYYYYYY@XXXXX.com']
sendemail.mail(subject,body,recepients)


print"*****************"
data="""
First line

second line


"""
print wfile.addfile(data)




20170727-ExcuteNewmodule.json (1.06 KB)

20170727-Newmodule (1).json (4.26 KB)

Hi @smartnetworks

I have updated the script in the initial post as per your request

Please let us know your feedback