Avatar of Irrylyn
Irrylyn
 asked on

Merge two scripts

i have two seperate scripts and I want to combine them in a scheduled task to run one right after the other.

Script A:  Copies the event log files into C:/EventLogs
Script B: Copies the event log files to a remote server location and clears the event logs.

I can't time them as seperate scheduled tasks or there will be data missing on one or the other, so how do I combine these two scripts to first copy the events into C:/EventLogs, then copy them to \\remoteserverlocation, then clear the events?

------
Script A
------
Dim DestServer
' Put in the UNC path for where you want the logs to be stored
DestServer = "C:\EventLogs"


'Create the Time variables
sDate=Right("0" & Month(Date),2) _
& "-" & Right("0" & Day(Date),2) _
& "-" & Right(Year(Date),2)

sTime = DatePart("h", Now) & DatePart("n", Now)

set oFSO = CreateObject("Scripting.FileSystemObject")
Set wshShell = WScript.CreateObject( "WScript.Shell" )
strServerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )

'If correct folder doesn't exist, make it
if Not oFSO.FolderExists(DestServer) then
   set oFolder = oFSO.CreateFolder(DestServer)
end if

'Gets the log files for this machine
strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate, (Backup, Security)}!\\" _
        & strComputer & "\root\cimv2")

Set colLogFiles = objWMIService.ExecQuery _
    ("Select * from Win32_NTEventLogFile")


'This section goes out and gets the hostname this is run on for us.

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)

For Each objItem in colItems
  strHOSTNAME = objItem.Name
NEXT


'Now archive the logs and clear them
if oFSO.FolderExists(DestServer & strComputerName) then
  For Each objLogfile in colLogFiles
    strBackupLog = objLogFile.BackupEventLog _
        (DestServer & strComputerName & "\"  & strHOSTNAME & "_" & objLogFile.LogFileName & "_" & sDate & "_" & sTime & ".evt")
  Next
end if

-------
Script B
-------
Dim DestServer
' Put in the UNC path for where you want the logs to be stored
DestServer = "\\remoteserverlocation\"


'Create the Time variables
sDate=Right("0" & Month(Date),2) _
& "-" & Right("0" & Day(Date),2) _
& "-" & Right(Year(Date),2)

sTime = DatePart("h", Now) & DatePart("n", Now)

set oFSO = CreateObject("Scripting.FileSystemObject")
Set wshShell = WScript.CreateObject( "WScript.Shell" )
strComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )

'If correct folder doesn't exist, make it
if Not oFSO.FolderExists(DestServer & strComputerName) then
   set oFolder = oFSO.CreateFolder(DestServer & strComputerName)
end if

'Gets the log files for this machine
strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate, (Backup, Security)}!\\" _
        & strComputer & "\root\cimv2")

Set colLogFiles = objWMIService.ExecQuery _
    ("Select * from Win32_NTEventLogFile")


'This section goes out and gets the hostname this is run on for us.

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)

For Each objItem in colItems
  strHOSTNAME = objItem.Name
NEXT


'Now archive the logs and clear them
if oFSO.FolderExists(DestServer & strComputerName) then
  For Each objLogfile in colLogFiles
    strBackupLog = objLogFile.BackupEventLog _
        (DestServer & strComputerName & "\"  & strHOSTNAME & "_" & objLogFile.LogFileName & "_" & sDate & "_" & sTime & ".evt")
    objLogFile.ClearEventLog()
  Next
end if
IT AdministrationMicrosoft Server OS

Avatar of undefined
Last Comment
Irrylyn

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
needleboy

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Irrylyn

ASKER
Thank you.

I used the cmd you provided.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck