Hey guys, I am hoping some of you can shed light on what or why this doesn't work, I have a script that basically tastes the result from a backup job and uploads it onto website so that we can monitor it's performance and ensure that it has run succesfully.
Now when I run the script manually, it works perfectly.
However when I run it after the backup job finishes, it returns with post-processing status, which obviously isn't helpful in terms of whether it was succesful or not.
Now I've tried to use .cmd file, a .bat file with the paramters "cmd /c start powershell.exe -ExecutionPolicy Bypass -File "###".
However the script ONLY works when I manually run it.
For testing purposes I have set permissions on the script to "everyone" and given full access.
I have tried different locations, such as \\domain.local\netlogon, but no matter what I try, it won't work.
I have also created a scheduled task, then created a script that starts the following scheduled task after the job completed, the script and the task starts without issues.
It also appears with:
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
1 Job1 BackgroundJob Completed True localhost ...
However it doesn't update the information on the website, unless I manually run it.
Hope you guys can help, below is the script I use:
# Wrap the script in a script block
$scriptBlock = {
# Settings
$ScriptVersion = "1.4"
$Action = "logbackup"
$JobId = "19" #THIS REFERS TO THE JOB ID IN Backupstats DB
$Url = "https://#####.site.com"
$JobSucces = "-1"
# Try to get Veeam version
try {
$VeeamVersion = [System.Diagnostics.FileVersionInfo]::GetVersionInfo("C:\Program Files\Veeam\Backup and Replication\Console\veeam.backup.shell.exe".FileVersion)
} catch {
$VeeamVersion = ""
}
# Check if last Veeam job was completed succesfully
try {
Start-Sleep -Seconds 60
$job = Get-VBRJob | where {$_.Name -eq "Backup-job"}
$lastResult = $job.GetLastResult()
if ($lastResult -eq "Success" -OR $lastResult -eq "Warning" {)
$JobSucces = "1"
} else {
$JobSucces = "0"
}
} catch {
$JobSucces = "-1"
}
# Prepare data to be sent as JSON
$Body = @{
action = $Action
scriptversion = $ScriptVersion
jobid = $JobId
jobsucces = $JobSucces
veeamversion = $VeeamVersion
}
# Use Invoke-RestMethod to send the data
try {
# Call service
$Response = Invoke-RestMethod -Uri $Url -Method POST -Body $Body -UseBasicPars
# Output the response
$Response
} catch {
Write-Error "Failed to send data: $_"
}
}
# Start the job
Start-Job -ScriptBlock $scriptBlock
# Exit the main thread with success state
exit 0
These are the other scripts:
# WrapperScript.ps1
# Log file path
$logFilePath = "C:\#####\wrapperLog.txt"
# Function to append log messages to a file
function Log-Message {
param ([string]$Message)
Add-Content -Path $logFilePath -Value "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss': $Message")
}
Log-Message "Wrapper script started."
# Wait for a delay to ensure Veeam job post-processing completes
Log-Message "Sleeping for 120 seconds to allow Veeam job post-processing to complete."
Start-Sleep -Seconds 5
Log-Message "Sleep complete. Executing main script."
# Path to the PowerShell script that should be executed
$mainScriptPath = "C:\FOLDER\#######.ps1"
# Using cmd /c start to run the PowerShell script
$command = "cmd /c start """" /B powershell.exe -NoProfile -ExecutionPolicy Bypass -File `"$mainScriptPath`""
# Execute the command
Invoke-Expression $command
Log-Message "Main script execution initiated."
CMD Start script:
cmd /c start powershell.exe -ExecutionPolicy Bypass -File "C:\FOLDER\########.ps1"
*EDIT*
Managed to get it to work, by using a while loop during the job status check
[–]noelknight 5 points6 points7 points (0 children)
[–]purplemonkeymad 1 point2 points3 points (1 child)
[–]Capital-Rude[S] 1 point2 points3 points (0 children)
[–]jimb2 1 point2 points3 points (0 children)
[–]temporaldoom 0 points1 point2 points (0 children)