Thursday 5 March 2015

Powershell - scripting objects from SQL Server


#svn credentials can be appended here for each  scripted file. 
#note the encoding UTF8 is important and needs to be declared explicitly

param( [string] $serverlist)

if ($serverlist -eq ""){
    Write-Host "No list specified, using $env:computername"
    $servers = $env:computername
}
else
{
    if ((Test-Path $serverlist) -eq $false)
    {
        Write-Host "Invalid audit path specified: $serverlist"
        exit
    }
    else
    {
        Write-Host "Using Audit list: $serverlist"
        $Servers = Get-Content $serverlist
    }
}

Add-PSSnapin SqlServerProviderSnapin100
Add-PSSnapin SqlServerCmdletSnapin100

Foreach ($server in $Servers)
{

#[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
$path = "c:\servers\$server"

IF(!(Test-Path "$path\LinkedServers")) {New-Item "$path\LinkedServers" -type directory}
IF(!(Test-Path "$path\Triggers")) {New-Item "$path\Triggers" -type directory}
IF(!(Test-Path "$path\Mail")) {New-Item "$path\Mail" -type directory}
IF(!(Test-Path "$path\Mail\Profiles")) {New-Item "$path\Mail\Profiles" -type directory}
IF(!(Test-Path "$path\Mail\Accounts")) {New-Item "$path\Mail\Accounts" -type directory}
IF(!(Test-Path "$path\BackupDevices")) {New-Item "$path\BackupDevices" -type directory}
IF(!(Test-Path "$path\Logins")) {New-Item "$path\Logins" -type directory}
IF(!(Test-Path "$path\Jobs")) {New-Item "$path\Jobs" -type directory}

cd SQLSERVER:\SQL\$server\DEFAULT\Databases

<#
$encoding = new-object "System.Text.ASCIIEncoding"
$encoding = new-object "System.Text.UTF8Encoding"
$Scripter.Options.Encoding = $encoding
#>

$encoding = new-object "System.Text.UTF8Encoding";

$Scripter=new-object ("Microsoft.SqlServer.Management.Smo.Scripter") ("(local)")
$Scripter.Options.DriAll=$True
$Scripter.Options.IncludeHeaders=$True
$Scripter.Options.ToFileOnly=$True
$Scripter.Options.Encoding = $encoding;
$Scripter.Options.WithDependencies=$False

foreach ($Item in Get-ChildItem) {
$db = $Item.ToString()
$db = $db.replace("[","")
$db = $db.replace("]","")
IF(!(Test-Path "$path\$db")) {New-Item "$path\$db" -type directory}
$Scripter.Options.FileName="$path\$db\" +$Item + ".sql"
$Scripter.Options.AppendToFile=$False
$Scripter.Options.ScriptDrops=$True;
$Scripter.Options.IncludeIfNotExists=$True;
$Scripter.Options.Indexes=$true;
$Scripter.Script($Item)
$Scripter.Options.ScriptDrops=$False;
$Scripter.Options.IncludeIfNotExists=$False;
$Scripter.Options.AppendToFile=$True
$Scripter.Script($Item)

IF(!(Test-Path "$path\$db\Tables")){New-Item "$path\$db\Tables" -type directory}
IF(!(Test-Path "$path\$db\Functions")){New-Item "$path\$db\Functions" -type directory}
IF(!(Test-Path "$path\$db\StoredProcedures")){New-Item "$path\$db\StoredProcedures" -type directory}
IF(!(Test-Path "$path\$db\Roles")){New-Item "$path\$db\Roles" -type directory}
IF(!(Test-Path "$path\$db\Triggers")){New-Item "$path\$db\Triggers" -type directory}
IF(!(Test-Path "$path\$db\Users")){New-Item "$path\$db\Users" -type directory}
IF(!(Test-Path "$path\$db\Views")){New-Item "$path\$db\Views" -type directory}
IF(!(Test-Path "$path\$db\Assemblies")){New-Item "$path\$db\Assemblies" -type directory}
IF(!(Test-Path "$path\$db\Synonyms")){New-Item "$path\$db\Synonyms" -type directory}
IF(!(Test-Path "$path\$db\Tables\Indexes")){New-Item "$path\$db\Tables\Indexes" -type directory}
IF(!(Test-Path "$path\$db\Tables\Triggers")){New-Item "$path\$db\Tables\Triggers" -type directory}
#New-Item "$path\$db\Tables\Statistics" -type directory
#New-Item "$path\$db\Tables\Checks" -type directory
IF(!(Test-Path "$path\$db\Tables\ForeignKeys")){New-Item "$path\$db\Tables\ForeignKeys" -type directory}



cd SQLSERVER:\SQL\$server\DEFAULT\Databases\$db\Tables


foreach ($tbl in Get-ChildItem) {
$table = $tbl.ToString()
$table = $table.replace("[","")
$table = $table.replace("]","")
$Scripter.Options.FileName="$path\$db\Tables\" +$table + ".sql"
$Scripter.Options.AppendToFile=$False
$Scripter.Options.Encoding = $encoding;
$Scripter.Options.ScriptDrops=$True;
$Scripter.Options.IncludeIfNotExists=$True;
$Scripter.Script($tbl)
$Scripter.Options.ScriptDrops=$False;
$Scripter.Options.IncludeIfNotExists=$False;
$Scripter.Options.AppendToFile=$True
$Scripter.Script($tbl)


cd SQLSERVER:\SQL\$server\DEFAULT\Databases\$db\Tables\$table\indexes


foreach ($idx in Get-ChildItem) {
$index = $idx.ToString()
$index = $index.replace("[","")
$index = $index.replace("]","")
$Scripter.Options.FileName="$path\$db\Tables\Indexes\" +$index + ".sql"
$Scripter.Options.AppendToFile=$False
$Scripter.Options.ScriptDrops=$True;
$Scripter.Options.IncludeIfNotExists=$True;
$Scripter.Script($idx)
$Scripter.Options.ScriptDrops=$False;
$Scripter.Options.IncludeIfNotExists=$False;
$Scripter.Options.AppendToFile=$True
$Scripter.Script($idx)}


cd SQLSERVER:\SQL\$server\DEFAULT\Databases\$db\Tables\$table\Triggers


foreach ($trg in Get-ChildItem) {
$trigger = $trg.ToString()
$trigger = $trigger.replace("[","")
$trigger = $trigger.replace("]","")
$Scripter.Options.FileName="$path\$db\Tables\Triggers\" +$trigger + ".sql"
$Scripter.Options.AppendToFile=$False
$Scripter.Options.ScriptDrops=$True;
$Scripter.Options.IncludeIfNotExists=$True;
$Scripter.Script($trg)
$Scripter.Options.ScriptDrops=$False;
$Scripter.Options.IncludeIfNotExists=$False;
$Scripter.Options.AppendToFile=$True
$Scripter.Script($trg)}


#cd SQLSERVER:\SQL\$server\DEFAULT\Databases\$db\Tables\$table\Statistics
#
#
#foreach ($sts in Get-ChildItem) {
#$stats = $sts.ToString()
#$stats = $stats.replace("[","")
#$stats = $stats.replace("]","")
#$Scripter.Options.FileName="$path\$db\Tables\Statistics\" +$stats + ".sql"
#$Scripter.Options.AppendToFile=$False
#$Scripter.Options.ScriptDrops=$True;
#$Scripter.Options.IncludeIfNotExists=$True;
#$Scripter.Script($sts)
#$Scripter.Options.ScriptDrops=$False;
#$Scripter.Options.IncludeIfNotExists=$False;
#$Scripter.Options.AppendToFile=$True
#$Scripter.Script($sts)}
#
#
#cd SQLSERVER:\SQL\$server\DEFAULT\Databases\$db\Tables\$table\Checks
#
#
#foreach ($chk in Get-ChildItem) {
#$checks = $chk.ToString()
#$checks = $checks.replace("[","")
#$checks = $checks.replace("]","")
#$Scripter.Options.FileName="$path\$db\Tables\Checks\" +$checks + ".sql"
#$Scripter.Options.AppendToFile=$False
#$Scripter.Options.ScriptDrops=$True;
#$Scripter.Options.IncludeIfNotExists=$True;
#$Scripter.Script($chk)
#$Scripter.Options.ScriptDrops=$False;
#$Scripter.Options.IncludeIfNotExists=$False;
#$Scripter.Options.AppendToFile=$True
#$Scripter.Script($chk)}


cd SQLSERVER:\SQL\$server\DEFAULT\Databases\$db\Tables\$table\ForeignKeys


foreach ($fk in Get-ChildItem) {
$keys = $fk.ToString()
$keys = $keys.replace("[","")
$keys = $keys.replace("]","")
$Scripter.Options.FileName="$path\$db\Tables\ForeignKeys\" +$keys + ".sql"
$Scripter.Options.AppendToFile=$False
$Scripter.Options.ScriptDrops=$True;
$Scripter.Options.IncludeIfNotExists=$True;
$Scripter.Script($fk)
$Scripter.Options.ScriptDrops=$False;
$Scripter.Options.IncludeIfNotExists=$False;
$Scripter.Options.AppendToFile=$True
$Scripter.Script($fk)}



}

cd SQLSERVER:\SQL\$server\DEFAULT\Databases\$db\Views


foreach ($tbl in Get-ChildItem) {
$Scripter.Options.FileName="$path\$db\Views\" +$tbl + ".sql"
$Scripter.Options.AppendToFile=$False
$Scripter.Options.ScriptDrops=$True;
$Scripter.Options.IncludeIfNotExists=$True;
$Scripter.Script($tbl)
$Scripter.Options.ScriptDrops=$False;
$Scripter.Options.IncludeIfNotExists=$False;
$Scripter.Options.AppendToFile=$True
$Scripter.Script($tbl)
}

cd SQLSERVER:\SQL\$server\DEFAULT\Databases\$db\UserDefinedFunctions


foreach ($tbl in Get-ChildItem) {
$Scripter.Options.FileName="$path\$db\Functions\" +$tbl + ".sql"
$Scripter.Options.AppendToFile=$False
$Scripter.Options.ScriptDrops=$True;
$Scripter.Options.IncludeIfNotExists=$True;
$Scripter.Script($tbl)
$Scripter.Options.ScriptDrops=$False;
$Scripter.Options.IncludeIfNotExists=$False;
$Scripter.Options.AppendToFile=$True
$Scripter.Script($tbl)
}

cd SQLSERVER:\SQL\$server\DEFAULT\Databases\$db\StoredProcedures


foreach ($tbl in Get-ChildItem) {
$Scripter.Options.FileName="$path\$db\StoredProcedures\" +$tbl.displayname + ".sql"
$Scripter.Options.AppendToFile=$False
$Scripter.Options.ScriptDrops=$True;
$Scripter.Options.IncludeIfNotExists=$True;
$Scripter.Script($tbl)
$Scripter.Options.ScriptDrops=$False;
$Scripter.Options.IncludeIfNotExists=$False;
$Scripter.Options.AppendToFile=$True
$Scripter.Script($tbl)
}

cd SQLSERVER:\SQL\$server\DEFAULT\Databases\$db\Roles


foreach ($tbl in Get-ChildItem) {
$Scripter.Options.FileName="$path\$db\Roles\" +$tbl + ".sql"
$Scripter.Options.AppendToFile=$False
$Scripter.Options.ScriptDrops=$True;
$Scripter.Options.IncludeIfNotExists=$True;
$Scripter.Script($tbl)
$Scripter.Options.ScriptDrops=$False;
$Scripter.Options.IncludeIfNotExists=$False;
$Scripter.Options.AppendToFile=$True
$Scripter.Script($tbl)
}

cd SQLSERVER:\SQL\$server\DEFAULT\Databases\$db\Triggers

foreach ($tbl in Get-ChildItem) {
$Scripter.Options.FileName="$path\$db\Triggers\" +$tbl + ".sql"
$Scripter.Options.AppendToFile=$False
$Scripter.Options.ScriptDrops=$True;
$Scripter.Options.IncludeIfNotExists=$True;
$Scripter.Script($tbl)
$Scripter.Options.ScriptDrops=$False;
$Scripter.Options.IncludeIfNotExists=$False;
$Scripter.Options.AppendToFile=$True
$Scripter.Script($tbl)
}

cd SQLSERVER:\SQL\$server\DEFAULT\Databases\$db\Users


foreach ($tbl in Get-ChildItem) {
$usr = $tbl.ToString()
$usr = $usr.replace("\","_")
$Scripter.Options.FileName="$path\$db\Users\" +$usr + ".sql"
$Scripter.Options.AppendToFile=$False
$Scripter.Options.ScriptDrops=$True;
$Scripter.Options.IncludeIfNotExists=$True;
$Scripter.Script($tbl)
$Scripter.Options.ScriptDrops=$False;
$Scripter.Options.IncludeIfNotExists=$False;
$Scripter.Options.AppendToFile=$True
$Scripter.Script($tbl)
}

cd SQLSERVER:\SQL\$server\DEFAULT\Databases\$db\Assemblies


foreach ($tbl in Get-ChildItem) {
$Scripter.Options.FileName="$path\$db\Assemblies\" +$tbl + ".sql"
$Scripter.Options.AppendToFile=$False
$Scripter.Options.ScriptDrops=$True;
$Scripter.Options.IncludeIfNotExists=$True;
$Scripter.Script($tbl)
$Scripter.Options.ScriptDrops=$False;
$Scripter.Options.IncludeIfNotExists=$False;
$Scripter.Options.AppendToFile=$True
$Scripter.Script($tbl)
}

cd SQLSERVER:\SQL\$server\DEFAULT\Databases\$db\Synonyms


foreach ($tbl in Get-ChildItem) {
$Scripter.Options.FileName="$path\$db\Synonyms\" +$tbl + ".sql"
$Scripter.Options.AppendToFile=$False
$Scripter.Options.ScriptDrops=$True;
$Scripter.Options.IncludeIfNotExists=$True;
$Scripter.Script($tbl)
$Scripter.Options.ScriptDrops=$False;
$Scripter.Options.IncludeIfNotExists=$False;
$Scripter.Options.AppendToFile=$True
$Scripter.Script($tbl)
}

}
cd SQLSERVER:\SQL\$server\DEFAULT\LinkedServers

foreach ($Item in Get-ChildItem) {
$filename = $Item.ToString()
$filename = $filename.Replace("\","_")
$Scripter.Options.FileName="$path\LinkedServers\" +$filename + ".sql"
$Scripter.Options.AppendToFile=$False
$Scripter.Options.ScriptDrops=$True;
$Scripter.Options.IncludeIfNotExists=$True;
$Scripter.Script($Item)
$Scripter.Options.ScriptDrops=$False;
$Scripter.Options.IncludeIfNotExists=$False;
$Scripter.Options.AppendToFile=$True
$Scripter.Script($Item)
}

cd SQLSERVER:\SQL\$server\DEFAULT\BackupDevices


foreach ($Item in Get-ChildItem) {
$Scripter.Options.FileName="$path\BackupDevices\" +$Item + ".sql"
$Scripter.Options.AppendToFile=$False
$Scripter.Options.ScriptDrops=$True;
$Scripter.Options.IncludeIfNotExists=$True;
$Scripter.Script($Item)
$Scripter.Options.ScriptDrops=$False;
$Scripter.Options.IncludeIfNotExists=$False;
$Scripter.Options.AppendToFile=$True
$Scripter.Script($Item)
}

cd SQLSERVER:\SQL\$server\DEFAULT\Logins


foreach ($Item in Get-ChildItem) {
$filename = $Item.ToString()
$filename = $filename.Replace("\","_")
$Scripter.Options.FileName="$path\Logins\" +$filename + ".sql"
$Scripter.Options.AppendToFile=$False
$Scripter.Options.ScriptDrops=$True;
$Scripter.Options.IncludeIfNotExists=$True;
$Scripter.Script($Item)
$Scripter.Options.ScriptDrops=$False;
$Scripter.Options.IncludeIfNotExists=$False;
$Scripter.Options.AppendToFile=$True
$Scripter.Script($Item)
}


cd SQLSERVER:\SQL\$server\DEFAULT\Mail\Accounts


foreach ($Item in Get-ChildItem) {
$filename = $Item.ToString()
$filename = $filename.Replace("\","_")
$Scripter.Options.FileName="$path\Mail\Accounts\" +$filename + ".sql"
$Scripter.Options.AppendToFile=$False
$Scripter.Options.ScriptDrops=$True;
$Scripter.Options.IncludeIfNotExists=$True;
$Scripter.Script($Item)
$Scripter.Options.ScriptDrops=$False;
$Scripter.Options.IncludeIfNotExists=$False;
$Scripter.Options.AppendToFile=$True
$Scripter.Script($Item)
}

cd SQLSERVER:\SQL\$server\DEFAULT\Mail\Profiles


foreach ($Item in Get-ChildItem) {
$filename = $Item.ToString()
$filename = $filename.Replace("\","_")
$Scripter.Options.FileName="$path\Mail\Profiles\" +$filename + ".sql"
$Scripter.Options.AppendToFile=$False
$Scripter.Options.ScriptDrops=$True;
$Scripter.Options.IncludeIfNotExists=$True;
$Scripter.Script($Item)
$Scripter.Options.ScriptDrops=$False;
$Scripter.Options.IncludeIfNotExists=$False;
$Scripter.Options.AppendToFile=$True
$Scripter.Script($Item)
}

cd SQLSERVER:\SQL\$server\DEFAULT\Triggers


foreach ($Item in Get-ChildItem) {
$filename = $Item.ToString()
$filename = $filename.Replace("\","_")
$Scripter.Options.FileName="$path\Triggers\" +$filename + ".sql"
$Scripter.Options.AppendToFile=$False
$Scripter.Options.ScriptDrops=$True;
$Scripter.Options.IncludeIfNotExists=$True;
$Scripter.Script($Item)
$Scripter.Options.ScriptDrops=$False;
$Scripter.Options.IncludeIfNotExists=$False;
$Scripter.Options.AppendToFile=$True
$Scripter.Script($Item)
}

cd SQLSERVER:\SQL\$server\DEFAULT\jobserver\jobs

foreach ($Item in Get-ChildItem) {
$filename = $Item.ToString()
$filename = $filename.Replace("\","_")
$Scripter.Options.FileName="$path\Jobs\" +$filename + ".sql"
$Scripter.Options.AppendToFile=$False
$Scripter.Options.ScriptDrops=$True;
$Scripter.Options.IncludeIfNotExists=$True;
$Scripter.Script($Item)
$Scripter.Options.ScriptDrops=$False;
$Scripter.Options.IncludeIfNotExists=$False;
$Scripter.Options.AppendToFile=$True
$Scripter.Script($Item)
}
}
#
#Credentials
#CryptographicProviders
#Endpoints
#JobServer
#Languages
#ResourceGovernor
#ServerAuditSpecifications
#SystemDataTypes
#SystemMessages
#UserDefinedMessages

No comments:

Post a Comment