PowerShell script exchange
Post Reply
APL_Ian
Novice
Posts: 7
Liked: never
Joined: Feb 27, 2023 7:57 am
Full Name: Ian James
Contact:

Script Weirdness

Post by APL_Ian »

Hi All

So I have this script, it finds the day and then disables or enables jobs based on the day is it. Quiet handy and work(ed)s pre version 13.

I will sanities and post the script at the end.

So some days it will trigger enable on a job and return an error in the transcript.

Code: Select all

Transcript started, output file is Veeam_PS_40.log

09 February 2026 00:00:00
>> TerminatingError(Enable-VBRJob): "Cannot process argument transformation on parameter 'Job'. The operation was canceled."
Enable-VBRJob: APL-VeeamSchedule.ps1:73
Line |
  73 |             Enable-VBRJob -Job "Tape-Daily"
     |                                ~~~~~~~~~~~~~~~~~~~
     | Cannot process argument transformation on parameter 'Job'. The operation was canceled.
Enable-VBRJob: C:\Scripts\APL-VeeamSchedule.ps1:73
Line |
  73 |             Enable-VBRJob -Job "Tape-Daily"
     |                                ~~~~~~~~~~~~~~~~~~~
     | Cannot process argument transformation on parameter 'Job'. The operation was canceled.
The job in question will remain disabled and won't run on schedule.

Other days, it will process the command and continue without any problems.

Code: Select all

Transcript started, output file is Veeam_PS_41.log

10 February 2026 00:00:00

LogNameMainPart              : VMHOST-Tape-Daily
The job will be enabled and run just fine.

This started in Version 13 and not sure how I can fix it.

The script is launching from pwsh.exe to pick up the correct version of powershell.
Module seems to load without any problem.
Just randomly fails to process a command then works the next day.

<insert confused look>

Any help would be much appreciated.

Names have been changed to protect the innocent.

Code: Select all

Import-Module Veeam.Backup.Powershell
$DNum = [DateTime]::Today.DayOfYear
$Log = "Veeam_PS_" + $DNUM + ".log" 
Start-Transcript -Path $Log

$SmtpServer = 'domain.com.mail.protection.outlook.com'
$From = 'backups@adomain.com'
$To = 'email@domain.com'
$Subject = "Month End for $(Get-Date -Format dd/MM/yyyy)"
$Month = (Get-Date).ToUniversalTime().Month | %{(Get-Culture).DateTimeFormat.GetMonthName($_)}
$Year = (Get-Date).Year

$mail_Body = "<html><head><title>$($Subject)</title>"
$mail_Body = $mail_Body -join "`n"
$mail_Body += "</head><body>"
$mail_Body += "<hr>"			
$mail_Body += '<table id="section"><tr><th width="95%">Today is the last working day of the month.</th></tr></table>'
$mail_Body += '<p><strong>Please make sure the month end tape is in the drive and ready to do the month end backup.</strong><br></p>'
$mail_Body += '<p><ul></p>'
$mail_Body += '<p><b><i>To create a month end tape:</i></b><br><br></p>'
$mail_Body += '<p><br></p>'
$mail_Body += '<p>1: Take a blank tape from the shelf and insert into the LTO8 drive..<br>'
$mail_Body += '2: Open Veeam on APL-VMHOST-02 and inventory the LTO8 tape library.<br>'
$mail_Body += '3: Open the properties of the tape and name it as below.<br>'
$mail_Body += '4: Right click tape and move to LTO8 media pool.<br></p>'
$mail_Body += '<p><br></p>'
$mail_Body += '<p>This months tape name is:<br>'
$mail_Body += '<b>Archive-' + $Month + '-' + $year +'</b></p>'
$mail_Body += '<p><ul></p>'
$mail_Body += '<p>Once the backup has finished. Move the backup to the Archived Media vault in Veeam.</p>'
$mail_Body += '<p></ul></p>'
$mail_Body += "<hr>"				
$mail_Body += '<table id="data">'


$DateTime = [DateTime]::Today
#$DateTime = [DateTime]::Today.AddDays(-1)
$Weekends = @("Saturday", "Sunday")
$BH = @( $Hols = (Invoke-RestMethod -Uri https://www.gov.uk/bank-holidays.json -Method GET).'england-and-wales'.events.date
$Hols += @('2026-12-21','2026-12-22','2026-12-23','2026-12-24','2026-12-29','2026-12-30')
Foreach ($Das in $hols){
Get-Date -Date $Das
})

$LastDay = (0..31 | ForEach-Object { (Get-Date "$($DateTime.Year)-$($DateTime.Month)-01").AddDays($_) } | Where-Object { $_.Month -eq $($DateTime.Month) } | Where-Object { $_.dayofweek -notin $Weekends } | Where-Object { $_ -notIn $BH })[-1]


If($LastDay -eq $DateTime)
{
      Disable-VBRJob -Job "Tape-Daily"
      Enable-VBRJob -Job "Monthly-Backups"
      Enable-VBRJob -Job "Tape-Monthly"
      #Send-MailMessage -From $From -Subject $Subject -To $To -Body $mail_Body -BodyAsHtml -Port 25 -SmtpServer $SmtpServer -UseSsl
}
Else
{
    If($DateTime -in $BH){
    Disable-VBRJob -Job "Tape-Daily"
    Disable-VBRJob -Job "Monthly-Backups"
    Disable-VBRJob -Job "Tape-Monthly"
    }

    Else
    {
        If($DateTime.DayOfWeek -in $Weekends){
        Disable-VBRJob -Job "Tape-Daily"
        Disable-VBRJob -Job "Monthly-Backups"
        Disable-VBRJob -Job "Tape-Monthly"
        }
         Else
        {
           $DateTime
           Enable-VBRJob -Job "Tape-Daily"
           Disable-VBRJob -Job "Monthly-Backups"
           Disable-VBRJob -Job "Tape-Monthly"
        }
    }
}

$Date = (Get-Date).AddDays(-14)
$Files = Get-ChildItem  -path "C:\Scripts" -filter '*.log' | Where-Object {$_.CreationTime -le $Date}
Foreach ($file in $Files){
Remove-Item $File.FullName -Force
}

Stop-Transcript
APL_Ian
Novice
Posts: 7
Liked: never
Joined: Feb 27, 2023 7:57 am
Full Name: Ian James
Contact:

Re: Script Weirdness

Post by APL_Ian »

Just wanted to add more context as I can no longer edit.

The time the script runs has been adjusted a few times. This was to check for timing issue with something else. Maybe the server or service was busy kind of thing. Currently it runs early hours of the morning, but I have switched it around to see if that was the issue, tried 09:00, 12:00, 18:00 and now its at 05:00. Nothing seems to have an effect.

I launch the script with a bat script that launches pwsh.exe
Any body had any issues launching scripts this way in 13? Maybe launching direct from scheduled tasks might have a better result.

Right now am testing a Start-Sleep 30 right after the start-transcript. Maybe its trying to run the command before the module fully loads. <shrug>
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests