Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. # Date: 16/02/14
  2. # Author: John Sansom
  3. # Description: PS script to generate all SQL Server Agent jobs on the given instance.
  4. # The script accepts an input file of server names.
  5. # Version: 1.1
  6. #
  7. # Example Execution: .\Create_SQLAgentJobSripts.ps1 .\ServerNameList.txt
  8.  
  9. param([String]$ServerListPath)
  10.  
  11. #Load the input file into an Object array
  12. $ServerNameList = get-content -path $ServerListPath
  13.  
  14. #Load the SQL Server SMO Assemly
  15. [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | Out-Null
  16.  
  17. #Create a new SqlConnection object
  18. $objSQLConnection = New-Object System.Data.SqlClient.SqlConnection
  19.  
  20. #For each server in the array do the following..
  21. foreach($ServerName in $ServerNameList)
  22. {
  23. Try
  24. {
  25. $objSQLConnection.ConnectionString = "Server=$ServerName;Integrated Security=SSPI;"
  26. Write-Host "Trying to connect to SQL Server instance on $ServerName..." -NoNewline -ForegroundColor Yellow
  27. $objSQLConnection.Open() | Out-Null
  28. Write-Host "Success." -ForegroundColor Green
  29. $objSQLConnection.Close()
  30. }
  31. Catch
  32. {
  33. Write-Host -BackgroundColor Red -ForegroundColor White "Fail"
  34. $errText = $Error[0].ToString()
  35. if ($errText.Contains("network-related"))
  36. {
  37. Write-Host -BackgroundColor Red -ForegroundColor White "Connection Error. Check server name, port, firewall."
  38. }
  39.  
  40. Write-Host -BackgroundColor Red -ForegroundColor White $errText
  41. continue
  42. }
  43.  
  44. #IF the output folder does not exist then create it
  45. $OutputFolder = ".\$ServerName"
  46. $DoesFolderExist = Test-Path $OutputFolder
  47. $null = if (!$DoesFolderExist){MKDIR "$OutputFolder"; MKDIR "$OutputFolder\MaintenanceSolution";MKDIR "$OutputFolder\default"; MKDIR "$OutputFolder\Custom";}
  48.  
  49. #Create a new SMO instance for this $ServerName
  50. $srv = New-Object "Microsoft.SqlServer.Management.Smo.Server" $ServerName
  51.  
  52. #Script out each SQL Server Agent Job for the server
  53. $srv.JobServer.Jobs | foreach {
  54. $agentJobNotifications = New-Object ('Microsoft.SqlServer.Management.Smo.ScriptingOptions');
  55. $agentJobNotifications.AgentJobId = $true;
  56. $agentJobNotifications.AgentNotify = $true;
  57. $agentJobNotifications.AppendToFile = $false;
  58.  
  59. if (@("CommandLog Cleanup",
  60. "DatabaseIntegrityCheck - SYSTEM_DATABASES",
  61. "DatabaseIntegrityCheck - USER_DATABASES",
  62. "IndexOptimize - USER_DATABASES",
  63. "Output File Cleanup",
  64. "sp_delete_backuphistory",
  65. "sp_purge_jobhistory",
  66. "Truncate Logs") -contains $_)
  67. {
  68. Write-Host -ForegroundColor White $_.Name; $_.Script() + "GO`r`n" | out-file ".\$OutputFolder\MaintenanceSolution\$_`.sql"
  69. }
  70. elseif ("syspolicy_purge_history" -eq $_)
  71. {
  72. Write-Host -ForegroundColor White $_.Name; $_.Script() + "GO`r`n" | out-file ".\$OutputFolder\default\$_`.sql"
  73. }
  74. else
  75. {
  76. Write-Host -ForegroundColor White $_.Name; $_.Script() + "GO`r`n" | out-file ".\$OutputFolder\Custom\$_`.sql"
  77. }
  78. }
  79.  
  80. #Use the command below to output each SQL Agent Job to a separate file. Remember to comment out the line above.
  81. #Removed backslash character, typically seen in Replication Agent jobs, to avoid invalid filepath issue
  82. #$srv.JobServer.Jobs | foreach-object -process {out-file -filepath $(".\$OutputFolder\" + $($_.Name -replace '\\', '') + ".sql") -inputobject $_.Script() }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement