MyDbBackup script – Use PrimalForms add value to your PowerShell Scripts…

Lets get right to the point…  Download this great from Sapien the “PrimalForms Community Edition”.   Its free and with a little extra work, you can add value to your scripts.  In this sample I’m adding a Windows GUI to one of my demo SQL SMO script to backup a database. 

Here’s the original script:

Add-Type -Assembly "Microsoft.SqlServer.Smo" | Out-Null

$bkp.Devices.AddDevice("c:\temp\xdeveloper.bak", "file");
$bkp.Database = "Developer";
$bkp.Action = "Database";
$bkp.Initialize = $true;
$bkp.SQLBackup($svr);

 

But, by adding a GUI interface and with some extra work, I can make it more flexible.  This way I can reuse this script to backup any database from any server.  Of course, keep in mind, you need use the right SMO assembly for this script to work.  But, for now, we only going to use “Microsoft.SqlServer.Smo” and not the "Microsoft.SqlServer.SmoExtended".

I build my simple Windows GUI so I can extend my script so I can type my required parameters to do my database backup. So I use PrimalForms:

 

I need to create a GUI with three text fields to pass some information and a button to execute my code.  During this process there will be some very basic validation.  Here’s how the form will look like:

 

After the form is completed, then at the tool bar you click on the “Export PowerShell”:

 

This option will allow you to build the forms code so you can send it to three areas:

1. Sapien “PrimalScript” Editor.
2. Send to Clipboard.
3. Send to a file.

After the export, make sure you name the file.  Now, with my editor,  I need to look for the section of the “Button_OnClick” so I can add both the validation and the SMO backup code.

#———————————————-
#Generated Event Script Blocks
#———————————————-
#Provide Custom Code for events specified in PrimalForms.
$button1_OnClick=
{
#TODO: Place custom script here

    # Storing values to textboxes
    [string] $SQLServer = $textBox1.text
    [string] $DbName = $textBox2.text
    [string] $BkPathName = $textBox3.text

# Simple textboxes validations look for empty values
    if ( $SQLServer -eq ""){
        $Allfields = "1. "+$SQLServer
        [system.Windows.forms.MessageBox]::Show($Allfields+"`r`n"+"Invalid entry or blank not allow!!")
        $Pass1 = $false;}
    else {
        $Pass1 = $true;
    }
    if ( $DbName -eq ""){
        $Allfields = "1. "+$textBox1.text+"`r`n"+"2. "+$DbName+"`r`n"
        [system.Windows.forms.MessageBox]::Show($Allfields+"`r`n"+"Invalid entry or blank not allow!!")
        $Pass1 = $false;}
    else {
        $Pass1 = $true;       
    }

    if ( $BkPathName -eq ""){
        $Allfields = "1. "+$SQLServer+"`r`n"+"2. "+$DbName+"`r`n"+"3. "+$BkPathName
        [system.Windows.forms.MessageBox]::Show($Allfields+"`r`n"+"Invalid entry or blank not allow!!")
        $Pass1 = $false;}
    else {
        $Pass1 = $true;
    }

    # If pass validation then execute the backup and verify that the backup file exist
    if ($Pass1 -eq $true){
        Add-Type -Assembly "Microsoft.SqlServer.Smo" | Out-Null
        #[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
        $svr = New-Object Microsoft.SqlServer.Management.Smo.server $SQLServer;
        $bkp = New-Object Microsoft.SqlServer.Management.Smo.Backup;
        $sqlBkupFullpathName = $BkPathName+"\x"+$DbName+".bak"
        $bkp.Devices.AddDevice($sqlBkupFullpathName, "file");
        $bkp.Database = $DbName;
        $bkp.Action = "Database";
        $bkp.Initialize = $true;
        $bkp.SQLBackup($svr);}
        if (Test-Path $sqlBkupFullpathName){
            [system.Windows.forms.MessageBox]::Show("Backup $sqlBkupFullpathName is done!")
        }
    else {
        $Allfields = "1. "+$SQLServer+"`r`n"+"2. "+$DbName+"`r`n"+"3. "+$BkPathName
        [system.Windows.forms.MessageBox]::Show($Allfields+"`r`n"+"Backup Error…")   
    }
}

Again, this script still have room evolve more and the you can keep extending the power of PowerShell with .NET framework.  A simple sample is the use of “… [system.Windows.forms.MessageBox]::Show(“Test message here”) …” so that a windows message box will popup.  Now, we finally completed our simple database backup GUI application.

    

Make sure you get SMO and PrimalForms installed and feel free to download the script: MyDbBackup.ps1

http://cid-a034d6a0ddc4e64e.skydrive.live.com/embedrow.aspx/August2009PowerShell/MyDbBackup.ps1.txt

(Please rename file to a *.ps1)

For more information:

PrimalForms Community Edition and other tools – http://www.primaltools.com/downloads/communitytools/

Microsoft SQL Server SMO downloads:

For SQL Server 2005: http://www.microsoft.com/downloads/details.aspx?FamilyID=d09c1d60-a13c-4479-9b91-9e8b9d835cdc&displaylang=en

For SQL Server 2008: http://www.microsoft.com/downloads/details.aspx?FamilyId=228DE03F-3B5A-428A-923F-58A033D316E1&displaylang=en

Advertisements

About maxt2posh

I’m from Puerto Rico, have been working with computers since 1979. Throughout many years, I have worked with SQL Server Technologies, provided support to Windows Servers/Client machines, Microsoft Virtualization Technologies, and build some Visual Studio solutions. I’m a Microsoft PowerShell MVP since 2009 and MVP SAPIEN Technologies since 2010. I speak in most of the SQLSaturday, IT Pro and .NET camps events around the Florida’s State. Also founder of the Florida PowerShell User Group which meeting every 3rd Tuesday evening of the month.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s