Powershell script equivalent of database -> Generate scripts - sql-server

I wrote a PowerShell script which stores schema, and data info in .sql file.
But in that file I am missing the initiate part of create database. The file starts with queries of create table.
PowerShell script:
$Filepath= 'C:\Temp' # local directory to save build-scripts to
$DataSource= 'ServerName' #'SERVERNAME' # server name and instance
$Database= 'DBName'
$DateTime = Get-Date -Format s
$dateStamp = $(get-date).ToString("yyyyMMdd");
$ErrorActionPreference = "stop"
$ms='Microsoft.SqlServer'
$v = [System.Reflection.Assembly]::LoadWithPartialName( "$ms.SMO")
if ((($v.FullName.Split(','))[1].Split('='))[1].Split('.')[0] -ne '9') {
[System.Reflection.Assembly]::LoadWithPartialName("$ms.SMOExtended") | out-null
}
$My="$ms.Management.Smo" #
$s = new-object ("$My.Server") $DataSource
if ($s.Version -eq $null ){Throw "Can't find the instance $Datasource"}
$db= $s.Databases[$Database]
if ($db.name -ne $Database){Throw "Can't find the database '$Database' in $Datasource"};
$transfer = new-object ("$My.Transfer") $db
$transfer.Options.ScriptBatchTerminator = $true
$transfer.Options.ToFileOnly = $true
$transfer.Options.ScriptData = $true
$transfer.Options.IncludeHeaders = $true
$transfer.Options.ExtendedProperties = $true
$transfer.Options.IncludeDatabaseContext = $true
$transfer.Options.DriForeignKeys = $true
$transfer.Options.DriIndexes = $true
$transfer.Options.DriPrimaryKey = $true
$transfer.Options.DriUniqueKeys = $true
$transfer.CreateTargetDatabase = $true
$transfer.Options.Filename = "$($FilePath)\$($Database)_$($dateStamp)_Build.sql";
$transfer.EnumScriptTransfer()
Any help on how this can be achieved?
Basically I am missing create database block in my .sql file.

Related

Powershell: Click button on GUI to move to next item in array

I have created a GUI with the purpose of deleting a row from a SQL table.
I am now stuck on having the results show one at a time as a button is pressed.
Screenshot of GUI:
If I have three results come back, it will show the first result in the text box, then when the Next button is pressed, it skips over the second result and shows the last one.
Debugging shows that it definitely finds three results, but its like I need to stop it between results until the button is pressed to move to the next one. I have tried many things, but have had no luck.
Here is the code (sorry there is quite a bit. The part I am stuck on it near the bottom):
<#
.NAME
Remove A File
#>
Start-Transcript -path "c:\path\RemoveScipt.txt"
$StaticData = "C:\path\Static Data.csv"
$SQLServer = "SERVERNAME"
$DB = "DATABASENAME"
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$RemoveFile = New-Object system.Windows.Forms.Form
$RemoveFile.ClientSize = New-Object System.Drawing.Point(530,510)
$RemoveFile.text = "Remove a script"
$RemoveFile.TopMost = $false
$ScriptName = New-Object system.Windows.Forms.Label
$ScriptName.text = "Script Name"
$ScriptName.AutoSize = $true
$ScriptName.width = 25
$ScriptName.height = 10
$ScriptName.location = New-Object System.Drawing.Point(14,20)
$ScriptName.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$ReportName = New-Object system.Windows.Forms.Label
$ReportName.text = "Report Name"
$ReportName.AutoSize = $true
$ReportName.width = 25
$ReportName.height = 10
$ReportName.location = New-Object System.Drawing.Point(14,50)
$ReportName.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Result = New-Object system.Windows.Forms.Label
$Result.text = "Is this the script you wish to remove?"
$Result.AutoSize = $true
$Result.width = 25
$Result.height = 10
$Result.location = New-Object System.Drawing.Point(14,110)
$Result.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$ScriptNameCombo = New-Object system.Windows.Forms.ComboBox
$ScriptNameCombo.width = 337
$ScriptNameCombo.height = 20
$ScriptNameCombo.location = New-Object System.Drawing.Point(120,19)
$ScriptNameCombo.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$ScriptNameSQL ="SELECT distinct Script_Name FROM TABLE"
$ScriptNameCall = Invoke-Sqlcmd -ServerInstance $SQLServer -Database $DB -Query $ScriptNameSQL
$ScriptNameCombo.Items.AddRange($ScriptNameCall.Script_Name) #ADDS ITEMS TO A DROPDOWN LIST
$ReportNameCombo = New-Object system.Windows.Forms.ComboBox
$ReportNameCombo.width = 337
$ReportNameCombo.height = 20
$ReportNameCombo.location = New-Object System.Drawing.Point(120,49)
$ReportNameCombo.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$GoBut = New-Object system.Windows.Forms.Button
$GoBut.text = "Go!"
$GoBut.width = 45
$GoBut.height = 25
$GoBut.location = New-Object System.Drawing.Point(470,18)
$GoBut.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$GoBut.BackColor =[System.Drawing.Color]::LightGreen
$Go2But = New-Object system.Windows.Forms.Button
$Go2But.text = "Go!"
$Go2But.width = 45
$Go2But.height = 25
$Go2But.location = New-Object System.Drawing.Point(470,48)
$Go2But.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Go2But.BackColor =[System.Drawing.Color]::LightGreen
$NextBut = New-Object system.Windows.Forms.Button
$NextBut.text = "Next"
$NextBut.width = 70
$NextBut.height = 30
$NextBut.location = New-Object System.Drawing.Point(242,103)
$NextBut.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$NextBut.BackColor =[System.Drawing.Color]::FloralWhite
$ResultText = New-Object system.Windows.Forms.TextBox
$ResultText.multiline = $true
$ResultText.width = 500
$ResultText.height = 325
$ResultText.scrollbars = "Both"
$ResultText.WordWrap = $false
$ResultText.location = New-Object System.Drawing.Point(14,135)
$ResultText.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$RemoveScript = New-Object system.Windows.Forms.Button
$RemoveScript.text = "Remove the script"
$RemoveScript.width = 150
$RemoveScript.height = 30
$RemoveScript.location = New-Object System.Drawing.Point(14,470)
$RemoveScript.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$CancelBut = New-Object system.Windows.Forms.Button
$CancelBut.text = "Cancel"
$CancelBut.width = 150
$CancelBut.height = 30
$CancelBut.location = New-Object System.Drawing.Point(365,470)
$CancelBut.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$CancelBut.BackColor =[System.Drawing.Color]::IndianRed
$RemoveFile.controls.AddRange(#($ScriptName,$ReportName,$RemoveScript,$ScriptNameCombo,$ReportNameCombo,$GoBut,$ResultText,$Result,$Go2But,$CancelBut,$NextBut))
$GoBut.Add_Click({
#USES WHAT WAS CHOSEN FROM THE FIRST DROPDOWN LIST TO FIND ANY ENTRIES WITH THAT ITEM IN THE FIRST COLUMN TO CREATE A SECOND DROPDOWN LIST. THIS IS TO SHORTEN THE RESULTS
$ReportNameLinkedSQL="SELECT distinct Report_Name FROM TABLE where Script_Name = '$($ScriptNameCombo.text)'"
$ReportNameCallLinked= Invoke-Sqlcmd -ServerInstance $SQLServer -Database $DB -Query $ReportNameLinkedSQL
if ($ScriptNameCombo.text.length -eq 0){[System.Windows.Forms.MessageBox]::Show('Ya gotta select a Script, buddy!','Uh oh','OK','Error')}else{
$ReportNameCombo.Items.AddRange($ReportNameCallLinked.Report_Name)}
})
$Go2But.Add_Click({
#THIS USES THE PREVIOUS TWO SELECTIONS TO FIND ALL MATCHING ROWS IN THE DATABASE WITH THOSE TWO ITEMS IN THE FIRST TWO COLUMNS. IT WILL SHOW THE FIRST RESULT IN THE TEXT BOX
$FullScriptSQL="SELECT * FROM TABLE where Script_Name = '$($ScriptNameCombo.text)' and Report_Name = '$($ReportNameCombo.text)'"
$global:FullScriptCallLinked= #(Invoke-Sqlcmd -ServerInstance $SQLServer -Database $DB -Query $FullScriptSQL)
$count=$FullScriptCallLinked.count
$nl = "`r`n"
if ($ScriptNameCombo.text.length -eq 0){[System.Windows.Forms.MessageBox]::Show('Ya gotta select a Script, buddy!','Uh oh','OK','Error')}
elseif($ReportNameCombo.text.length -eq 0){[System.Windows.Forms.MessageBox]::Show('Ya gotta select a Report, buddy!','Uh oh','OK','Error')}
elseif($count -eq $null -and $FullScriptCallLinked.Email_or_file -eq 'E'){$ResultText.text = ($nl),"Script Name = ",$FullScriptCallLinked.script_name,($nl),($nl),"Report Name = ",$FullScriptCallLinked.Report_name,($nl),($nl),"View Name = ",$FullScriptCallLinked.View_name,($nl),($nl),"Module = ",$FullScriptCallLinked.Module,($nl),($nl),"Email Address = ",$FullScriptCallLinked.email,($nl),($nl),"Email Subject = ",$FullScriptCallLinked.email_subject,($nl),($nl),"Email Body = ",$FullScriptCallLinked.email_message,($nl),($nl),"File Name = ",$FullScriptCallLinked.filename,($nl),($nl),"File Type = ",$FullScriptCallLinked.file_type}
elseif($count -eq $null -and $FullScriptCallLinked.Email_or_file -eq 'F'){$ResultText.text = ($nl),"Script Name = ",$FullScriptCallLinked.script_name,($nl),($nl),"Report Name = ",$FullScriptCallLinked.Report_name,($nl),($nl),"View Name = ",$FullScriptCallLinked.View_name,($nl),($nl),"Module = ",$FullScriptCallLinked.Module,($nl),($nl),"File Name = ",$FullScriptCallLinked.filename,($nl),($nl),"File Path = ",$FullScriptCallLinked.file_path,($nl),($nl),"Todays Date = ",$FullScriptCallLinked.today_date,($nl),($nl),$FullScriptCallLinked.yesterday_date,($nl),($nl),"File Type = ",$FullScriptCallLinked.file_type}
elseif($FullScriptCallLinked[0].Email_or_file -eq 'E'){$ResultText.text = ($nl),"Script Name = ",$FullScriptCallLinked[0].script_name,($nl),($nl),"Report Name = ",$FullScriptCallLinked[0].Report_name,($nl),($nl),"View Name = ",$FullScriptCallLinked[0].View_name,($nl),($nl),"Module = ",$FullScriptCallLinked[0].Module,($nl),($nl),"Email Address = ",$FullScriptCallLinked[0].email,($nl),($nl),"Email Subject = ",$FullScriptCallLinked[0].email_subject,($nl),($nl),"Email Body = ",$FullScriptCallLinked[0].email_message,($nl),($nl),"File Name = ",$FullScriptCallLinked[0].filename,($nl),($nl),"File Type = ",$FullScriptCallLinked[0].file_type}
elseif($FullScriptCallLinked[0].Email_or_file -eq 'F'){$ResultText.text = ($nl),"Script Name = ",$FullScriptCallLinked[0].script_name,($nl),($nl),"Report Name = ",$FullScriptCallLinked[0].Report_name,($nl),($nl),"View Name = ",$FullScriptCallLinked[0].View_name,($nl),($nl),"Module = ",$FullScriptCallLinked[0].Module,($nl),($nl),"File Name = ",$FullScriptCallLinked[0].filename,($nl),($nl),"File Path = ",$FullScriptCallLinked[0].file_path,($nl),($nl),"Todays Date = ",$FullScriptCallLinked[0].today_date,($nl),($nl),$FullScriptCallLinked[0].yesterday_date,($nl),($nl),"File Type = ",$FullScriptCallLinked[0].file_type}
})
$Nextbut.Add_Click({
#HERE IS WHERE I AM STUCK. MY INTENT IS TO SCROLL THROUGH EACH RESULT WHEN THE BUTTON IS CLICKED
function onClick {
foreach($i in $FullScriptCallLinked){
$ResultText.text = ($nl),"Script Name = ",$i.script_name,($nl),($nl),"Report Name = ",$i.Report_name,($nl),($nl),"View Name = ",$i.View_name,($nl),($nl),"Module = ",$i.Module,($nl),($nl),"Email Address = ",$i.email,($nl),($nl),"Email Subject = ",$i.email_subject,($nl),($nl),"Email Body = ",$i.email_message,($nl),($nl),"File Name = ",$i.filename,($nl),($nl),"File Type = ",$i.file_type
}
}
onClick
})
$RemoveScript.Add_Click({
#NOT COMPLETE
$deleteSQL = "DELETE FROM TABLE WHERE Script_Name = '$($FullScriptCallLinked.script_name)' and Report_Name = '$($FullScriptCallLinked.Report_name)' and View_Name = '$($FullScriptCallLinked.View_name)' and FILENAME = '$($FullScriptCallLinked.filename)' and File_type = '$($FullScriptCallLinked.file_type)'"
write-host $deleteSQL
#[environment]::exit(0)
})
$CancelBut.Add_Click({
#CLEARS ALL FIELDS IN THE FORM
$ScriptNameCombo.text=$null
$ReportNameCombo.items.clear()
$ReportNameCombo.resettext()
$ResultText.clear()
})
[void]$RemoveFile.ShowDialog()
Any help with this would be amazing! Thanks in advance
Here is the problem:
foreach($i in $FullScriptCallLinked){
$ResultText.text = ....
}
The foreach statement iterates through all the items, one by one. So if you have three items in $FullScriptCallLinked, the above code will set $ResultText.text three times. The first two values will be overwritten by the last one. So what you see in the UI is just the last item.
You can do something like this:
$currentResultIndex = 0
$Nextbut.Add_Click({
function onClick {
$currentResultIndex = $currentResultIndex+1
if($currentResultIndex -ge $FullScriptCallLinked.Length) {
$currentResultIndex = 0;
}
$i = $FullScriptCallLinked[$currentResultIndex]
$ResultText.text = ($nl),"Script Name = ",$i.script_name,($nl),($nl),"Report Name = ",$i.Report_name,($nl),($nl),"View Name = ",$i.View_name,($nl),($nl),"Module = ",$i.Module,($nl),($nl),"Email Address = ",$i.email,($nl),($nl),"Email Subject = ",$i.email_subject,($nl),($nl),"Email Body = ",$i.email_message,($nl),($nl),"File Name = ",$i.filename,($nl),($nl),"File Type = ",$i.file_type
}
onClick
})
Please read more about PowerShell collections and the foreach statement.
Oops! I forgot about this one. I did figure out how to both scroll through each result, then cycle back to the first result when it reaches the end.
I made some changes to the GUI to make it easier for me.
I have completed the script. I updated the 'Go' button and finished the 'Next' button:
$GoBut.Add_Click({
#Establish SQL statements which include a search and a count to verify it completed successfully at the end
$FullScriptSQLEmail = "SELECT * FROM TABLE where email = '$($EmailAddressCombo.text)' and Email_or_file = 'E'"
$FullScriptSQLFile = "SELECT * FROM TABLE where Filename = '$($fileNameCombo.text)' and Email_or_file = 'F'"
$CountSelectE = "SELECT count (*) FROM TABLE where email = '$($EmailAddressCombo.text)' and Email_or_file = 'E'"
$CountSelectF = "SELECT count (*) FROM TABLE where Filename = '$($fileNameCombo.text)' and Email_or_file = 'F'"
$FullScriptCallEmail = #(Invoke-Sqlcmd -ServerInstance $SQLServer -Database $DB -Query $FullScriptSQLEmail)
$FullScriptCallFile = #(Invoke-Sqlcmd -ServerInstance $SQLServer -Database $DB -Query $FullScriptSQLFile)
$CountResultE = Invoke-Sqlcmd -ServerInstance $SQLServer -Database $DB -Query $CountSelectE
$CountResultF = Invoke-Sqlcmd -ServerInstance $SQLServer -Database $DB -Query $CountSelectF
$nl = "`r`n"
#Get a count. This works for a 'sanity check', but counts one as zero if you attempt to output it
$countEmail = $FullScriptCallEmail.count
$countFile = $FullScriptCallFile.count
#Bit of a cleanup to prevent errors
$IfExist = Test-Path "./artifacts/*.txt"
if ($IfExist -eq $true){[System.Windows.Forms.MessageBox]::Show('Whoops! Just gonna clear out some stuff to give you a fresh start Bucko.','Oh dear','OK','Error')
$EmailAddressCombo.text=$null
$fileNameCombo.text=$null
$ScriptCount.text=$null
$ResultText.clear()
Remove-Item ".\Artifacts\*.txt"
return}
#Only allow one combobox to be chosen, but must be one
Function Clear_fields{
$EmailAddressCombo.text=$null
$fileNameCombo.text=$null
}
if ($EmailAddressCombo.text.length -ne 0 -and $fileNameCombo.text.length -ne 0){[System.Windows.Forms.MessageBox]::Show('THERE CAN ONLY BE ONE! You can only choose an Email OR a File Path, champ!','Uh oh','OK','Error'); Clear_fields; return}
elseif($EmailAddressCombo.text.length -eq 0 -and $fileNameCombo.text.length -eq 0){[System.Windows.Forms.MessageBox]::Show('Ya gotta select a Report, buddy!','Uh oh','OK','Error'); return}
#Using the Count SQL statment to get a real count and then putting the number of results into a box so you know how many you got
if ($CountResultE.Column1 -gt 0){$ScriptCount.text = $CountResultE.Column1}else
{$ScriptCount.text = $CountResultF.Column1}
#Put all the data into a text file and format it at the same time
if($countEmail -gt 0)
{foreach($i in $FullScriptCallEmail){
add-content -path "./Artifacts/results.txt" -value "$($nl) Script Name = $($i.script_name)$($nl)$($nl) Report Name = $($i.Report_name)$($nl)$($nl) View Name = $($i.View_name)$($nl)$($nl) Module = $($i.Module)$($nl)$($nl) Email Address = $($i.email)$($nl)$($nl) Email Subject = $($i.email_subject)$($nl)$($nl) Email Body = $($i.email_message)$($nl)$($nl) File Name = $($i.filename)$($nl)$($nl) File Type = $($i.file_type)"}
}
if($countFile -gt 0)
{foreach($i in $FullScriptCallFile){
add-content -path "./Artifacts/results.txt" -value "$($nl) Script Name = $($i.script_name)$($nl)$($nl) Report Name = $($i.Report_name)$($nl)$($nl) View Name = $($i.View_name)$($nl)$($nl) Module = $($i.Module)$($nl)$($nl) File Name = $($i.filename)$($nl)$($nl) File Path = $($i.file_path)$($nl)$($nl) Todays Date = $($i.today_date)$($nl)$($nl) Yesterdays Date = $($i.yesterday_date)$($nl)$($nl) File Type = $($i.file_type)"}
}
#Establish the results file and create a copy. Show the first 18 lines in the result box
$resultfile = "./Artifacts/results.txt"
copy-item "./Artifacts/results.txt" -destination "./Artifacts/result_copy.txt"
#($content = get-content $resultfile)
$ResultText.Lines = $content[0..17]
})
$Nextbut.Add_Click({
$resultfile = "./Artifacts/results.txt"
#Function to delete the top 18 lines of the result file so the next 18 are grabbed. If there is only one set left (counts how many times Script Name appears), then the data in the copy is copied and pasted into the results file, starting it all over again
function del-line {
$Check1 = get-content $resultfile
$Check2 = $Check1 -match "Script Name"
$check2.count
if ($check2.count -eq 1){
$copy = "./Artifacts/result_copy.txt"
get-content $copy | set-content $resultfile
$ResultText.clear()
}else{
$ResultText.clear()
(Get-Content $resultfile | Select-Object -Skip 18) | Set-Content $resultfile
}
}
#Funciton to get the first 18 lines of the result file and display it after using the del-line function
function onClick{
del-line
$LineCount = get-content $resultfile
if (($LineCount.count) -eq 1){
$content = get-content $resultfile
$ResultText.text = $content}
else{
$content = get-content $resultfile
$ResultText.Lines = $content[0..17]
}
}
#Start the loop
onClick
})
If anyone wants the whole script let me know.
I have written 5 GUI scripts to make life easier for my team to:
Add a SQL entry
Remove a SQL entry
Amend a SQL entry
Search for and export a SQL entry or multiple entries
I am no expert and I know it is a little clunky, but works for us. I would think large DB's might cause some strain on resources

How do you script out SQL Server agent jobs to single or individual files

I've tried various Powershell scripts but they fail with:
The following exception occurred while trying to enumerate the collection: "An exception occurred while executing a Transact-SQL statement or batch.".
At H:\Create_SQLAgentJobSripts2.ps1:89 char:22
foreach ($job in $s.JobServer.Jobs)
~~~~~~~~~~~~~~~~~
CategoryInfo : NotSpecified: (:) [], ExtendedTypeSystemException
FullyQualifiedErrorId : ExceptionInGetEnumerator
What has gone wrong or how can I get better debugging on this error?
I executed this script:
.\Create_SQLAgentJobSripts2.ps1 .\ServerNameList.txt
Here's the script
param([String]$ServerListPath)
#write-host "Parameter: $ServerListPath"
#Load the input file into an Object array
$ServerNameList = get-content -path $ServerListPath
#Load the SQL Server SMO Assemly
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | Out-Null
#Create a new SqlConnection object
$objSQLConnection = New-Object System.Data.SqlClient.SqlConnection
#For each server in the array do the following.
foreach($ServerName in $ServerNameList)
{
Write-Host "Beginning with Server: $ServerName"
Try
{
$objSQLConnection.ConnectionString = "Server=$ServerName;Initial Catalog=CED_NCT_RESOURCE_TRACK;Persist Security Info=True;User ID=CEDNCTAdmin;Password=CEDNCTAdmin;"
Write-Host "Trying to connect to SQL Server instance on $ServerName..." -NoNewline
$objSQLConnection.Open() | Out-Null
Write-Host "Success."
$objSQLConnection.Close()
}
Catch
{
Write-Host -BackgroundColor Red -ForegroundColor White "Fail"
$errText = $Error[0].ToString()
if ($errText.Contains("network-related"))
{Write-Host "Connection Error. Check server name, port, firewall."}
Write-Host $errText
continue
}
# Won't be using this object again
Remove-Variable -Name objSQLConnection
#If the output folder does not exist then create it
$OutputFolder = ".\$ServerName"
if (!(Test-Path $OutputFolder))
{
write-host ("Creating directory: " + $OutputFolder)
New-Item -ItemType directory -Path $OutputFolder
}
else
{
write-host ("Directory already exists: " + $OutputFolder)
}
write-host "File: $(".\$OutputFolder\" + $($_.Name -replace '\\', '') + ".job.sql")"
# Connect to the instance using SMO
$s = new-object ('Microsoft.SqlServer.Management.Smo.Server') $ServerName
write-host ("SQL Server Edition: " + $s.Edition)
write-host ("SQL Agent ErrorLogFile: " + $s.JobServer.ErrorLogFile)
# Instantiate the Scripter object and set the base properties
$scrp = new-object ('Microsoft.SqlServer.Management.Smo.Scripter') ($ServerName)
write-host ("SCRP ToString():" + $scrp.ToString())
write-host ("Test scrp - Server: " + $scrp.Server)
#The next step is to set the properties for the script files:
$scrp.Options.ScriptDrops = $False
$scrp.Options.WithDependencies = $False
$scrp.Options.IncludeHeaders = $True
$scrp.Options.AppendToFile = $False
$scrp.Options.ToFileOnly = $True
$scrp.Options.ClusteredIndexes = $True
$scrp.Options.DriAll = $True
$scrp.Options.Indexes = $False
$scrp.Options.Triggers = $False
$scrp.Options.IncludeIfNotExists = $True
#Now, we can cycle through the jobs and create scripts for each job on the server.
# Create the script file for each job
foreach ($job in $s.JobServer.Jobs)
{
$jobname = $job.Name
write-host ("Job: " + $jobname)
$jobfilename = ($OutputFolder + "\" + $jobname + ".job.sql")
$scrp.Options.FileName = $jobfilename
write-host "Filename: $jobfilename"
#This line blows up
$scrp.Script($job)
}
}
Possibly you're not instantiating the Server object correctly. Try the following instead...
# Alternative 1: With servername and port, using Trusted Connection...
$ServerName = 'YourServerName,1433'
$ServerConnection = New-Object Microsoft.SqlServer.Management.Common.ServerConnection -ArgumentList #( $ServerName )
# Alternative 2: With an SqlConnection object
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=$ServerName;Initial Catalog=CED_NCT_RESOURCE_TRACK;Persist Security Info=True;User ID=CEDNCTAdmin;Password=CEDNCTAdmin;"
$SqlConnection.Open() | Out-Null
$ServerConnection = New-Object Microsoft.SqlServer.Management.Common.ServerConnection -ArgumentList #( $SqlConnection )
# Then...
$Server = New-Object Microsoft.SqlServer.Management.Smo.Server -ArgumentList #( $ServerConnection )
$Server.JobServer.Jobs | ForEach-Object {
Write-Host "Job: $($_.Name)"
}

SMO to script just the database and it settings (not the objects in it)

I have put together a nice PowerShell script to script out the objects (tables, functions, sprocs etc) from a database, limiting it to the ones in a list.
But I am stuck trying to find a way to script the database itself. Each time I do that, it seems to try to script out the whole database (it is way to large for that to go well).
Assuming I have a $db variable that is a reference to my database, how can I use SMO to script out that database, creating it with the same Properties and DatabaseScopedConfigurations, but none of the actual objects in it?
Update:
For reference here is my current script. It takes a server and database name and will script out all the objects found in a file called DbObjectsList.txt (assuming they are in the database). But this does not actually make the database. The database I am running this on is a legacy one, and it has a bunch of odd options set. I would like to preserve those.
$serverName = "MyServerName"
$databaseName = "MyDbName"
$date_ = (date -f yyyyMMdd)
$path = ".\"+"$date_"
# Load the Sql Server Management Objects (SMO) and output to null so we don't show the dll details.
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') > $null
# Setup the scripting options
$scriptOptions = new-object ('Microsoft.SqlServer.Management.Smo.ScriptingOptions')
$scriptOptions.ExtendedProperties = $true
$scriptOptions.AnsiPadding = $true
$scriptOptions.ClusteredIndexes = $true
# Dri = Declarative Referential Integrity
$scriptOptions.DriAll = $true
$scriptOptions.Triggers = $true
$scriptOptions.NoCollation = $false
$scriptOptions.SchemaQualify = $true
$scriptOptions.ScriptSchema = $true
$scriptOptions.EnforceScriptingOptions = $true
$scriptOptions.SchemaQualifyForeignKeysReferences = $true
$scriptOptions.NonClusteredIndexes = $true
$scriptOptions.Statistics = $true
$scriptOptions.Permissions = $true
$scriptOptions.OptimizerData = $true
# get a reference to the database we are going to be scripting from
$serverInstance = New-Object ('Microsoft.SqlServer.Management.Smo.Server') $serverName
$db=$serverInstance.Databases | Where-Object {$_.Name -eq $databaseName}
$dbname = "$db".replace("[","").replace("]","")
$dbpath = "$path"+ "\"+"$dbname" + "\"
if ( !(Test-Path $dbpath))
{
$null=new-item -type directory -name "$dbname"-path "$path"
}
# Load the list of db objects we want to script.
$listPath = ".\DbObjectList.txt"
if ((Test-Path $listPath))
{
$dbListItems = Get-Content -Path $listPath
}
else
{
throw "Could not find DbObjectst.txt file (it should have a list of what to script)."
}
# Setup the output file, removing any existing one
$outFile = "$dbpath" + "FullScript.sql"
if ((Test-Path $outFile)){Remove-Item $outFile }
$typeDelimiter = "=========="
foreach ($dbListItem in $dbListItems)
{
# Let the caller know which one we are working on.
echo $dbListItem
if ($dbListItem.StartsWith($typeDelimiter))
{
# Pull the type out of the header
$startIndex = $typeDelimiter.Length;
$stopIndex = $dbListItem.LastIndexOf($typeDelimiter)
$type = $dbListItem.Substring($startIndex, $stopIndex - $startIndex).Trim()
continue;
}
if ($type -eq $null)
{
throw "Types not included DbObjectsList.txt. Add types before groups of objects, surrounded by " + $typeDelimiter
}
foreach ($dbObjectToScript in $db.$type)
{
$objName = "$dbObjectToScript".replace("[","").replace("]","")
$compareDbListItem = "$dbListItem".replace("[","").replace("]","")
if ($compareDbListItem -eq $objName)
{
"-- " + $dbListItem | out-File -Append $outFile
$dbObjectToScript.Script($scriptOptions)+"GO" | out-File -Append $outFile
}
}
}

Scripting DBs backup through Powershell

I have created a PowerShell script that takes the back up of entire structure of database. When it comes to jobs backup, I cannot find a possible solution to that.
$v = [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO')
if ((($v.FullName.Split(','))[1].Split('='))[1].Split('.')[0] -ne '9')
{
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMOExtended') | out-null
}
[System.Reflection.Assembly]:: LoadWithPartialName('Microsoft.SqlServer.SmoEnum') | out-null
set-psdebug -strict # catch a few extra bugs
$ErrorActionPreference = "stop"
$My = 'Microsoft.SqlServer.Management.Smo'
$srv = new-object ("$My.Server") $ServerName # attach to the server
foreach($sqlDatabase in $srv.databases)
{
$databaseName=$sqlDatabase.name
if ($databaseName.count)
{
$scripter = new-object ("$My.Scripter") $srv # create the scripter
$scripter.Options.ToFileOnly = $true
# we now get all the object types except extended stored procedures
# first we get the bitmap of all the object types we want
$all =[long]
[Microsoft.SqlServer.Management.Smo.DatabaseObjectTypes]:: all -bxor
[Microsoft.SqlServer.Management.Smo.DatabaseObjectTypes]:: ExtendedStoredProcedure
# and we store them in a datatable
$d = new-object System.Data.Datatable
# get everything except the servicebroker object, the information schema and system views
$d = $srv.databases[$databaseName].EnumObjects([long]0x1FFFFFFF -band $all) | Where-Object {$_.Schema -ne 'sys'-and $_.Schema "information_schema"
#Saving it in a directory
}
}
This scripts takes the back up of the db but take the structural back up of msdb. I studied Microsoft.SqlServer.SMO that says it has a job server agent and job collection function but it doesn't seem to work.
For jobs, use the JobServer.Jobs collection. You can similarly script other server-level objects.
Below is an example.
$jobsCollection = $srv.JobServer.Jobs
$scriptingOptions = New-Object Microsoft.SqlServer.Management.Smo.ScriptingOptions
$scriptingOptions.IncludeIfNotExists = $true
$scriptingOptions.AppendToFile = $false
$scriptingOptions.ToFileOnly = $true
foreach ($job in $jobsCollection) {
Write-Host "Scripting $($job.Name)"
$scriptingOptions.FileName = "C:\ScriptFolder\Jobs.sql"
$job.Script($scriptingOptions)
$scriptingOptions.AppendToFile = $true
}
Although the answer given by Dan helped me but it wasn't creating a script in the folders. It was just creating folders with the jobs names. So, I did something like this :
foreach($sqlDatabase in $srv.JobServer.Jobs)
{ $databaseName=$sqlDatabase.name
write-host("I am her '$databaseName' ");
$scripter = new-object ("$My.Scripter") $srv # create the scripter
$scripter.Options.ToFileOnly = $true
$d = new-object System.Data.Datatable
$d=$srv.JobServer.Jobs[$databaseName]
$d| FOREACH-OBJECT {
trap [System.Management.Automation.MethodInvocationException]{
write-host ("ERROR: " + $_) -Foregroundcolor Red; Continue
}
# for every object we have in the datatable.
$SavePath="$($DirectoryToSaveTo)\$($ServerName)\$($databaseName)\$($_.DatabaseObjectTypes)"
# create the directory if necessary (SMO doesn't).
if (!( Test-Path -path $SavePath )) # create it if not existing
{Try { New-Item $SavePath -type directory | out-null }
Catch [system.exception]{
Write-Error "error while creating '$SavePath' $_"
return
}
}
# tell the scripter object where to write it
$scripter.Options.Filename = "$SavePath\$($_.name -replace '[\\\/\:\.]','-').sql";
# Create a single element URN array
$UrnCollection = new-object ('Microsoft.SqlServer.Management.Smo.urnCollection')
$URNCollection.add($_.urn)
# and write out the object to the specified file
$scripter.script($URNCollection)
}
}

Sql Server Script data: SMO.Scripter not working when output to file

I get this error message when I run the Powershell script at the bottom:
Exception calling "EnumScript" with "1" argument(s): "Script failed for Table 'dbo.Product'. "
At :line:48 char:35
+ foreach ($s in $scripter.EnumScript <<<< ($tbl)) { write-host $s }
However, when I comment out the output_file line
#$output_file="C:\Product.sql"
(which won't set the Scripter options to write to file), it works fine and outputs the INSERT statments to the console.
Here's the failing script, is there something I'm missing?
# Script INSERTs for given table
param
(
[string] $server,
[string] $database,
[string] $schema,
[string] $table,
[string] $output_file
)
$server="devdidb02"
$database="EPCTrunk_EPC"
$schema="dbo"
$table="Product"
$output_file="C:\Product.sql"
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | out-null
$srv = New-Object "Microsoft.SqlServer.Management.SMO.Server" $server
$db = New-Object ("Microsoft.SqlServer.Management.SMO.Database")
$tbl = New-Object ("Microsoft.SqlServer.Management.SMO.Table")
$scripter = New-Object ("Microsoft.SqlServer.Management.SMO.Scripter") ($server)
# Get the database and table objects
$db = $srv.Databases[$database]
$tbl = $db.tables | Where-object {$_.schema -eq $schema-and$_.name -eq $table}
# Set scripter options to ensure only data is scripted
$scripter.Options.ScriptSchema = $false;
$scripter.Options.ScriptData = $true;
#Exclude GOs after every line
$scripter.Options.NoCommandTerminator = $true;
if ($output_file -gt "")
{
$scripter.Options.FileName = $output_file
$scripter.Options.ToFileOnly = $true
}
# Output the script
foreach ($s in $scripter.EnumScript($tbl)) { write-host $s }
I ran both yours and Keith's and it looks like the issue is in the path you are setting for the file. I was able to reproduce your error. You were using $output_file="C:\Product.sql". Then I changed the path to: $output_file="$home\Product.sql" it ran just fine and gave me the file.
I am guessing that the reason for this is that I don't have permission to write to c:\ which may be the problem you are having.
BTW - my home dir in this case for me is my user folder for my login so I was able to find it there.
FWIW I'm not able to repro the error you see using the AdventureWorks DB. The following generates the foo.sql file without any errors:
Add-Type -AssemblyName ('Microsoft.SqlServer.Smo, Version=10.0.0.0, ' + `
'Culture=neutral, PublicKeyToken=89845dcd8080cc91')
$serverName = '.\SQLEXPRESS'
$smo = new-object Microsoft.SqlServer.Management.Smo.Server $serverName
$db = $smo.Databases['AdventureWorks']
$tbl = $db.Tables | Where {$_.Schema -eq 'Production' -and `
$_.Name -eq 'Product'}
$output_file = "$home\foo.sql"
$scripter = new-object Microsoft.SqlServer.Management.Smo.Scripter $serverName
$scripter.Options.ScriptSchema = $false;
$scripter.Options.ScriptData = $true;
$scripter.Options.NoCommandTerminator = $true;
if ($output_file -gt "")
{
$scripter.Options.FileName = $output_file
$scripter.Options.ToFileOnly = $true
}
# Output the script
foreach ($s in $scripter.EnumScript($tbl)) { write-host $s }

Resources