Please help me fix this method invocation failed error.

Sasi balaji 0 Reputation points
2025-05-13T11:58:44.64+00:00

The code iam trying to run is :

$scriptDirectory = Split-Path -Parent $MyInvocation.MyCommand.Path

$agentJobListFile = Join-Path -Path $scriptDirectory -ChildPath "AgentJobsList.txt"

$csvFilePath = Join-Path -Path $scriptDirectory -ChildPath "Veeamagent.csv"

function Check-FileExists {

param ([string]$filePath)

if (-not (Test-Path -Path $filePath)) {

    Write-Error "❌ The required file '$filePath' does not exist."

    exit 1

} else {

    Write-Host "✅ Found input file: $filePath"

}

}

Check-FileExists -filePath $agentJobListFile

$allAgentJobData = @()

$agentJobNames = Get-Content -Path $agentJobListFile

Write-Host "✅ Loaded $(($agentJobNames).Count) agent job names from AgentJobsList.txt"

foreach ($agentJobName in $agentJobNames) {

Write-Host "`n🚀 Processing agent job: $agentJobName"

$job = Get-VBRComputerBackupJob -Name $agentJobName -ErrorAction SilentlyContinue

if ($null -eq $job) {

    Write-Warning "⚠️ Skipping: Job '$agentJobName' not found."

    continue

}

# Get the last session for this job only (memory-safe)

$session = Get-VBRBackupSession -Job $job | Sort-Object CreationTime -Descending | Select-Object -First 1

if ($null -eq $session) {

    Write-Warning "⚠️ No session found for job: $agentJobName"

    continue

}

Write-Host "📦 Last session: $($session.CreationTime)"

$tasks = Get-VBRTaskSession -Session $session

Write-Host "🔧 Task count: $($tasks.Count)"

foreach ($task in $tasks) {

    $jobData = [PSCustomObject]@{

        JobName      = $job.Name

        Computer     = $task.Name

        StartTime    = $task.Progress.StartTime

        EndTime      = $task.Progress.StopTime

        Duration     = if ($task.Progress.StopTime -and $task.Progress.StartTime) {

            ($task.Progress.StopTime - $task.Progress.StartTime).ToString()

        } else { "N/A" }

        Status       = $task.Status

        Result       = $task.Result

        BackupSizeGB = if ($task.Progress.ProcessedSize) {

            "{0:N2}" -f ($task.Progress.ProcessedSize / 1GB)

        } else { "0.00" }

    }

    $allAgentJobData += $jobData

}

}

if ($allAgentJobData.Count -gt 0) {

$allAgentJobData | Export-Csv -Path $csvFilePath -NoTypeInformation -Encoding UTF8

Write-Host "`n✅ Agent job data exported to: $csvFilePath"

} else {

Write-Host "`n⚠️ No agent job data was found to export."

}

Error I got :

Method invocation failed because [Veeam.Backup.PowerShell.Infos.VBRComputerBackupJob] does not contain a method named '

GetLastSession'.

At C:\Users\T1SV132301\Documents\Daily_Report_Generation\Daily_Report_Generation\Veeamagent.ps1:38 char:5

  • $session = $job.GetLastSession()
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (:) [], RuntimeException
    • FullyQualifiedErrorId : MethodNotFound
Windows for business | Windows Server | User experience | PowerShell
{count} votes

1 answer

Sort by: Most helpful
  1. MotoX80 36,401 Reputation points
    2025-05-13T14:16:20.7566667+00:00

    You posted the Powershell script as normal text. This forum is interpreting some characters in your script as formatting directives. That's why your question has multiple grey boxes. Please edit the question and repost the script using the Code Block tool.

    User's image

    Did you post the correct script?

    The message says that the error is occurring on line 38 in Veeamagent.ps1, and that this is the statement in error.

    $session = $job.GetLastSession()
    

    But I don't see "GetLastSession" referenced anywhere in your script.

    Assuming that that the $job variable is populated by a statement similar to this one...

    $job = Get-VBRComputerBackupJob -Name $agentJobName -ErrorAction SilentlyContinue
    

    ...then the assumption would be that Veeam is not exposing that method.

    Have Powershell list all methods and properties like this.

    $job | get-member
    

    I found a Get-VBRSession cmdlet documented here.

    https://helpcenter.veeam.com/docs/backup/powershell/get-vbrsession.html?ver=120

    You would need to consult the Veeam documentation on the proper way to query a session. That would be Veeam specific, and we really can't help you with that.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.