PowerShell script exchange
Post Reply
pflynn782
Lurker
Posts: 2
Liked: never
Joined: Mar 31, 2022 3:04 pm
Contact:

Script not running if I run as post-job, works from command -line

Post by pflynn782 »

Hello all - wondering if you might have any ideas.

I have fileserver1 that has a bunch of files on it (dumps of db`s from previous night - separate process non-veeam)

Then, I backup this fileserver, and if the job is successful, I would like to delete those files. It works fine from command line, whether I start as powershell as admin or not. But when i run this from the post job option within the backup job, I get nothing, I have tried adding a separate script with a sleep 900 to see if it was because the backup job was still running but to no avail.

here`s what I am doing - any ideas are appreciated :

Code: Select all

$job = Get-VBRJob -Name "MYJobName"

$backupjob=Get-VBRBackupSession | Where {$_.jobId -eq $job.Id.Guid} | Sort EndTimeUTC -Descending | Select -First 1
if ( $backupjob.Result -eq "Success" )
{

$secureFileonprem = "C:\VeeamFLR\my-creds.txt"
$passwordonprem = Get-Content $secureFileonprem | ConvertTo-SecureString
$usernameonprem = "MyServer\Mylocalaccount"
$cred = New-Object System.Management.Automation.PSCredential ($usernameonprem, $passwordonprem )

$s = New-PSSession -ComputerName myfileserver.domain.com -Credential $cred
Invoke-Command -Session $s -Scriptblock {Remove-Item -Path D:\MyPath\dir\* -Recurse}

Remove-PSSession $s

 }

Thanks
Mildur
Product Manager
Posts: 9846
Liked: 2607 times
Joined: May 13, 2017 4:51 pm
Full Name: Fabian K.
Location: Switzerland
Contact:

Re: Script not running if I run as post-job, works from command -line

Post by Mildur »

Hi Paul

Can you use a text file output to check what the job result is? This helps to check if somehow the result is not success in your case.

Code: Select all

$job = Get-VBRJob -Name "MYJobName"
$backupjob = Get-VBRBackupSession | Where {$_.jobId -eq $job.Id.Guid} | Sort EndTimeUTC -Descending | Select -First 1
if ( $backupjob.Result -eq "Success" )
       {
          New-Item C:\temp\test.txt
          Set-Content C:\temp\test.txt 'Job successfull'
       }
else 
       {
          New-Item C:\temp\test.txt
          Set-Content C:\temp\test.txt 'Job not successfull'
       }
Product Management Analyst @ Veeam Software
jorgedlcruz
Veeam Software
Posts: 1493
Liked: 655 times
Joined: Jul 17, 2015 6:54 pm
Full Name: Jorge de la Cruz
Contact:

Re: Script not running if I run as post-job, works from command -line

Post by jorgedlcruz »

Hello,
You can find the very same situation here, with a few tips on how to solve it.

Give it a try and let us know. Thanks
Jorge de la Cruz
Senior Product Manager | Veeam ONE @ Veeam Software

@jorgedlcruz
https://www.jorgedelacruz.es / https://jorgedelacruz.uk
vExpert 2014-2024 / InfluxAce / Grafana Champion
david.domask
Veeam Software
Posts: 2123
Liked: 513 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Script not running if I run as post-job, works from command -line

Post by david.domask »

One thing to note is that if you encrypt the file, you need to make sure the Veeam Service account is able to access it also; this will be a small hiccup since we cannot change the user context of the script execution at this time.

I actually think it's better to run this as a background process outside of Veeam instead of as a post-job script and just monitor for when the job runs and use the same cmdlets to check the status of the job, but it's just personal preference. You could install the console onto the file server for simplicity and then just never use it except for the scripting and just do the same checks and deletion live on the server, and save the PSRemoting session.
David Domask | Product Management: Principal Analyst
pflynn782
Lurker
Posts: 2
Liked: never
Joined: Mar 31, 2022 3:04 pm
Contact:

Re: Script not running if I run as post-job, works from command -line

Post by pflynn782 »

Hello all - Thanks for the suggestions - I had to setup local system to be able to read the secure string and it works. Much appreciated.
david.domask
Veeam Software
Posts: 2123
Liked: 513 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Script not running if I run as post-job, works from command -line

Post by david.domask »

Glad you got it working! Thank you for sharing what worked also.
David Domask | Product Management: Principal Analyst
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests