PowerShell script exchange
Post Reply
chrisflyckelen
Service Provider
Posts: 60
Liked: 7 times
Joined: Oct 15, 2019 7:51 am
Contact:

Disable and enable Backup to tape job ... with logging

Post by chrisflyckelen » 1 person likes this post

Hello guys,
I wrote a short scripts which disables a backup to tape job during the weekend. And looks like this:

Code: Select all

$filepath = "C:\Scripts\Logs"
$date = Get-Date -Format "dd.MM.yyyy HH:mm:ss"

$tapejob = Get-VBRTapeJob -Name "GFS Backup to Tape"
$tapejob | Disable-VBRJob

$status = Get-VBRTapeJob -Name "GFS Backup to Tape"

if ($status.Enabled -eq "False") 
{"$date Der Backup To Tape Job wurde erfolgreich deaktiviert." | Out-File "$filepath\DisableTapeJobLog.txt" -Append}
elseif ($status.Enabled -eq "True") 
{"$date Der Backup To Tape Job wurde nicht erfolgreich deaktiviert." | Out-File "$filepath\DisableTapeJobLog.txt" -Append}
else 
{"$date Irgendwas stimmt hier nicht. :-)" | Out-File "$filepath\DisableTapeJobLog.txt" -Append}
Everything is working fine, the if-statement excluded. It doesn't matter which value the variable $status results. The if-statement results always in the else condition.
Is it quite possible to work with the properties of a Cmdlet result in an if-statement?

Thank you,
Chris
oleg.feoktistov
Veeam Software
Posts: 1918
Liked: 636 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Disable and enable Backup to tape job ... with logging

Post by oleg.feoktistov »

Hi Chris,

It is possible. However wrapping True/False values in double quotes in your if/else flow makes them strings, not booleans.
And by default powershell doesn't do type conversion unless you instruct it. So you need to present True/False as actual booleans preppending a $ sign instead of using double quotes to make it work:

Code: Select all

if ($status.Enabled -eq $true) { # do something }

elseif ($status.Enabled -eq $false) { # do something }

else { # do something }

Best regards,
Oleg
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests