If statement not working

  • I know I'm doing something silly & stupid, but I can't figure out what it is.

    Here's the, very simple, script I'm trying to run. Our backup jobs are named differently in SQL Server 2008 than they are in 2005 & 2000, so I need to deal differently with them. So, as you can see in the script, I'm checking the MajorVersion to see if it equals 10. If so, go to work, if not, don't. There are servers from all three versions in the ServerList.txt file. I can connect to them all... everything is working but it's skipping the 2008 servers. None of them show up as a hit.

    What the heck is up with this thing?

    [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | out-null

    foreach ($svr in Get-Content "c:\Scripts\ServerList.txt")

    {

    $srv=New-Object "Microsoft.SqlServer.Management.Smo.Server" "$svr"

    if ($srv.Information.MajorVersion -eq "10")

    {

    $srv.JobServer.Jobs | Where-Object { $_.Name -eq "Database Services Daily Compressed Backups"} | Format-Table name,lastrunoutcome,$svr.tostring() -autosize

    }

    else

    {

    #$srv.jobserver.jobs.Item("Database_Services_30Min_LS_Log_Backups").Start()

    $srv.JobServer.Jobs | Where-Object {$_.Name -eq "Database Services Daily LS_Backups"} | Format-Table name,lastrunoutcome,$svr.tostring() -autosize

    #| Where-Object {$_.Start(} #| where-object {$_.lastrunoutcome -eq "Failed" -and $_.isenabled -eq $TRUE} | format-table name,lastrunoutcome,lastrundate -autosize

    }

    }

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • The property you are trying to get to does not exist. Use $srv.VersionMajor instead of $srv.Information.MajorVersion.

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • I'm sorry. You're right. That was just a typo I made after I copied & pasted. I'm using the correct property or I'd get an error. It's just not returning the proper value and working through the IF logic and I don't understand why.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • I tested that against one of my 2008 systems and it returned "10". Try adding Write-Host $srv.VersionMajor before the if statement and see what is being returned from that server.

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • I'm entirely unsure of what I changed, but it's working now. I feel like an idiot because it should have worked. It looks good now... thanks for the help.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Been there done that - just remember, this is a 'scripting' language and as such does not tell you when you have made a typo. I have seen where it looks absolutely right - but I had a small typo and it just returns nothing.

    The other thing I noticed is that referencing a property that does not exist does not generate an error. It just doesn't return a value - which can cause all kinds of fun debugging issues.

    Glad you got it working.

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply