-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemote_Executor.vbs
More file actions
103 lines (89 loc) · 4.83 KB
/
Remote_Executor.vbs
File metadata and controls
103 lines (89 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
'File Name: Remote_Executor.vbs
'Version: v1.8, 3/11/2019, Specify full path to cmd.exe in .exec statement.
'Author: Justin Grimes, 6/7/2018
Option Explicit
Dim userInput, passwordInput, hostnameInput, commandInput, outFile, oShell, oFSO, oFile, ping, strComputerName, hostname, arg, param1, echo, query, pingResults, strUserName, _
pingResult, strLogFilePath, strSafeDate, strSafeTime, strDateTime, strLogFileName, objLogFile, strCommand, execute, WshFinished, WshFailed, strOutput, execStatus, results, scriptPath, _
companyAbbreviation, companyName
'Define variables for the session
Set oShell = WScript.CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set arg = WScript.Arguments
echo = TRUE
companyAbbreviation = "Company"
companyName = "Tru Form"
toEmail = "IT@company.com"
fromEmail = "Server@company.com"
strComputerName = oShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
strUserName = oShell.ExpandEnvironmentStrings("%USERNAME%")
scriptPath = "\\SERVER\AutomationScripts\Remote_Executor"
outFile = scriptPath & "\Warning.mail"
strLogFilePath = "\\SERVER\Logs"
strSafeDate = DatePart("yyyy",Date) & Right("0" & DatePart("m",Date), 2) & Right("0" & DatePart("d",Date), 2)
strSafeTime = Right("0" & Hour(Now), 2) & Right("0" & Minute(Now), 2) & Right("0" & Second(Now), 2)
strDateTime = strSafeDate & "-" & strSafeTime
strLogFileName = strLogFilePath & "\" & strComputerName & "-" & strDateTime & "-remote_executor.txt"
'A funciton for running SendMail.
Function SendEmail()
oShell.exec "C:\Windows\System32\cmd.exe /c " & scriptPath & "\sendmail.exe " & outFile
End Function
'A function to create a log file.
Function CreateLog(strEventInfo)
If Not (strEventInfo = "") Then
Set objLogFile = oFSO.CreateTextFile(strLogFileName, True)
objLogFile.WriteLine(strEventInfo)
objLogFile.Close
End If
End Function
'Define the user supplied inputs.
hostnameInput = InputBox("Enter a remote hostname or IP address to execute the command: ", companyAbbreviation & " Remote Executor")
commandInput = InputBox("Enter the command to be executed on the remote machine: ", companyAbbreviation & " Remote Executor")
strCommand = scriptPath & "\PSTools\PsExec.exe -h \\" & hostnameInput & " cmd /c """ & commandInput & """"
'Check if hostname is alive before sending the command.
query = "SELECT * FROM Win32_PingStatus WHERE Address = '" & hostnameInput & "'"
Set pingResults = GetObject("winmgmts://./root/cimv2").ExecQuery(query)
For Each pingResult In pingResults
'If host is alive execute the command.
If (pingResult.StatusCode = 0) Then
'Execute the command.
Set execute = oShell.Exec(strCommand)
'Determine if the command was executed sucessfully.
Select Case execute.Status
Case WshFinished
strOutput = execute.StdOut.ReadAll
execStatus = "Command Complete!"
Case WshFailed
strOutput = execute.StdErr.ReadAll
execStatus = "Command Failed!"
End Select
'Build a string from the output of the command.
Do
strOutput = execute.StdOut.ReadLine() & strOutput
Loop While Not execute.Stdout.atEndOfStream
'Display the output of the command to the user if the hostname is alive.
results = execStatus & vbNewLine & vbNewLine & "User: " & vbNewLine & strUserName & vbNewLine & _
vbNewLine & "Command: " & vbNewLine & commandInput & vbNewLine & vbNewLine & "Command Source: " & _
vbNewLine & strComputerName & vbNewLine & vbNewLine & "Command Target: " & _
vbNewLine & hostnameInput & vbNewLine & vbNewLine & "Output: " & strOutput
MsgBox results, 0, companyAbbreviation & " Remote Executor"
'Display an error message if the hostname is not alive.
Else
results = "Hostname """ & hostnameInput & """ is not online!" & vbNewLine
MsgBox results, 0, companyAbbreviation & " Remote Executor"
End If
Next
'Generate a logfile on \\TFISERVER/Logs.
CreateLog(results)
'Generate an email and send it to IT.
Set oFile = oFSO.CreateTextFile(outFile, TRUE)
oFile.Write "To: " & toEmail & vbNewLine & "From: " & fromEmail & vbNewLine & _
"Subject: " & companyAbbreviation & " Remote Execution Warning!!!" & vbNewLine & _
"This is an automatic email from the " & companyName & " Network to notify you that a command was just executed remotely." & _
vbNewLine & vbNewLine & "Please log-in and verify that the equipment listed below is functioning." & vbNewLine & _
vbNewLine & "USER NAME: " & strUserName & vbNewLine & "COMMAND SOURCE: " & strComputerName & vbNewLine & _
"COMMAND TARGET: " & hostnameInput & vbNewLine & "COMMAND: " & commandInput & vbNewLine & "COMMAND OUTPUT: " & _
vbNewLine & "--------------------" & vbNewLine & results & "--------------------" & vbNewLine & vbNewLine &_
"This check was generated by " & strComputerName & " and is performed when run by a user." & vbNewLine & vbNewLine & _
"Script: ""Remote_Executor.vbs"""
oFile.close
SendEmail