cancel
Showing results for 
Search instead for 
Did you mean: 

Running PowerShell script immediately after BE job finishes

BojanKG
Level 3

Hi, I have Backup Exec 2012 in my company. Among many jobs there is one for backing up Exchange Server 2010 IIS logs. Since I want to delete these log files after they are backed up (kind of archiving) I can write simply PowerShell script to delete all these files except the newest one (which is locked since it is used by Exchange). It would be nice if there is possibility to run this script automatically immediately after backup job is finished. Is this doable in Backup Exec 2012?

I know there are pre/post commands - does this mean that as a command I can use: C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -Noninteractive -File "C:\Delete-IISLogFiles.ps1"?

 

1 ACCEPTED SOLUTION

Accepted Solutions

pkh
Moderator
Moderator
   VIP    Certified
Where is your script located? According to your command, it should be located at the remote server.

View solution in original post

12 REPLIES 12

VJware
Level 6
Employee Accredited Certified

You could either provide the path to a .bat file containing the script or use the following (as an example) directly:-

c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe test.ps1

BojanKG
Level 3

Yes, I thought like you, I edited my post minutes after you had replied. I will test this next week when IIS logs start to eat my c partition free space on Exchange. PS script is like this (just one line):

 

Get-ChildItem '\\exchangeservername\c$\inetpub\logs\LogFiles\W3SVC1' | Sort-Object CreationTime -Descending | Select-Object -Skip 1 | Remove-Item -Force

BojanKG
Level 3

Yes, I thought like you. I posted it after you had replied to me. So just to call my ps script:

 

C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -Noninteractive -File "C:\Delete-IISLogFiles.ps1"

 

Which does this:

 

Get-ChildItem '\\exchangeservername\c$\inetpub\logs\LogFiles\W3SVC1' | Sort-Object CreationTime -Descending | Select-Object -Skip 1 | Remove-Item -Force

BojanKG
Level 3

I tested this but PS script was not executed - all log files are still on Exchange Server. In the attachment I put the screenshoot of job settings related to post-command. 

 

 

VJware
Level 6
Employee Accredited Certified

Are you able to execute this PS script outside of BE using the BE account ?

Is there any mention of the post command being run/attempted in the job log ?

Is BE 2012 & the remote agents updated with at least SP4 ?

BojanKG
Level 3

In the job log there is a following info:

Starting Post Job Command < c:\Windows\system32\WindowsPowerShell\1.0\powershell.exe -Noninteractive -File "c:\PSScripts\Delete-IISLogFiles.ps1" >

 

Below that there is a info Completed status: Successful for the job. Now I will restore these files to Exchange Server and run this script from the cmd prompt on BE with BE account (I deleted those files by running ps script from domain controller).

 

Newest info: I run c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noninteractive -file "c:\PSScripts\Delete-IISLogFiles.ps1" from within command prompt on BE server with BE account and it completed successfully - log files on exchange server were deleted.

pkh
Moderator
Moderator
   VIP    Certified
Where is your script located? According to your command, it should be located at the remote server.

BojanKG
Level 3

It is located in c:\PSScripts folder on the BE server itself.

VJware
Level 6
Employee Accredited Certified

In that case, you should be specifying the post command to run on the BE server itself and not on the remote server.

pkh
Moderator
Moderator
   VIP    Certified
The command is executed on the remote server so the script cannot be found. It should be on the remote server. You should test the command by logging in to the remote server with the BE credentials and then executing the command. Otherwise, you will not know whether the script even works

BojanKG
Level 3

Scenario is very simple. PS script file is physically on BE server. It connects to inetpub folder on Exchange Server, lists all log files there and deletes all of them except the newest one. From command prompt on BE server it works fine. Syntax for calling it from BE seems to be a cause of this - otherwise calling ps script from command prompt would also fail.

BojanKG
Level 3

According to all mentioned post command will always run directly on remote server who I am backing up - it tries to find script on remote server c:\PSScripts location which does not exist. This is conclusion? 

I tested, it worked. Thanks to all of you.

CONCLUSION: PRE OR POST COMMANDS IN BACKUP JOB ALWAYS RUN DIRECTLY ON REMOTE SERVER THAT IS BEING BACKED UP. SO ANY POWERSHELL SCRIPT CALLED HERE HAS TO BE LOCATED PHYSICALLY ON REMOTE SERVER. THIS WORKS SIMILARLY TO POWERSHELL REMOTE SESSIONS - COMMANDS ENTERED ARE EXECUTING DIRECTLY ON REMOTE COMPUTER.