If SQL DB exists, dont run function. If it doesnt run function.

  • Hello, I have created the script below, it works well, but I need to put a line in there to check whether the report server database exists or not before running the function. If it does exist, I'd like to exit the SQL provider (pop-location), and continue on with the rest of the functions. But I am not sure how to do this. Do I need to write another function, or can I add a line in somewhere between the 'Set-Location' line and the 'Invoke-SqlCmd' line? I am also not sure if I can use a powershell line, or another Invoke-SqlCmd line.

    Any help would be great, and thank you for taking the time to read/respond.

    Function fn-Set-SSRS-Create-RS-DB ([String]$ManagedComputer, [String] $SqlInstanceName, [String]$SSRSDatabaseServerName, [Int]$SSRSDatabaseServerTCPPort)

    #{

    #Try

    {

    Push-Location

    Import-Module "SqlPS" -DisableNameChecking

    $SSRSConMgr = Get-WmiObject -namespace "root\Microsoft\SqlServer\ReportServer\RS_$SqlInstanceName\v11\Admin" -Class MSReportServer_ConfigurationSetting -ComputerName $ManagedComputer

    $CreateSSRSDB = $SSRSConMgr.GenerateDatabaseCreationScript("ReportServer","3081","False")

    Set-Location SQLSERVER:\SQL\$SSRSDatabaseServerName

    Invoke-Sqlcmd -query $CreateSSRSDB["Script"] -ServerInstance $SSRSDatabaseServerName

    Pop-Location

    #if ($Result -ne (0))

    #{

    #Throw "Failed to create ReportServer Database : $Error[0]"

    #}

    #}

    #Catch

    #{

    #$Error[0]

    #}

    }

  • Untested but should work:

    if ( $null -ne $server.Databases[$DBName] ) { $exists = $true } else { $exists = $false }

    gsc_dba

Viewing 2 posts - 1 through 1 (of 1 total)

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