Commit d521b54f authored by carplaptop's avatar carplaptop

renaming three files

parent 06faa9b2
param (
[Parameter(Position=0, Mandatory=$true)] [string]$servername #(local)
)
################################################################################
# File : Deploy-PerformanceCollector.ps1
#
# Summary : A PowerShell script which will deploy PerformanceCollector
#
# Requirements : The script must be run from within the root of the directory containing the PerformanceCollector scripts
#
# History : 2012-04-03 Dave Wentzel released version as Open Source
#
################################################################################
cls
# Set the ErrorAction to "Stop" so that commands outside of the try..catch block will still end the script
# and an error will be reported back to the calling process
$script:ErrorActionPreference = "Stop"
$databaseName = "PerformanceCollector"
$workingDirectory = (Split-Path $MyInvocation.MyCommand.Path)
$sqlScriptDirectory = $workingDirectory
$sqlpsxModuleDirectoryPath = $workingDirectory + "\ThirdParty\SQLPSX\"
# We need to add the current SQLPSX path to the Powershell modules path since it imports by module and not direct path
# This method for appending to the PSMODULEPATH environment variable will append to it only in the context of the current session
if ($env:PSModulePath -notlike "*$sqlpsxModuleDirectoryPath*") {$env:PSModulePath = "$sqlpsxModuleDirectoryPath;" + $env:PSModulePath}
# Import the necessary modules
Import-Module SQLServer
function New-SqlServerWithEvents {
param (
[Parameter(Position=0, Mandatory=$true)] [string]$servername, #(local)
[Parameter(Position=1, Mandatory=$false)] [string]$username,
[Parameter(Position=2, Mandatory=$false)] [string]$password,
[Parameter(Position=3, Mandatory=$false)] [string]$applicationName
)
# Get a connection to the SQL Server. If no username was passed in then use integrated security
if ($userName) {$server = Get-SqlServer -sqlServer $servername -username $username -password $password -applicationName $applicationName}
else {$server = Get-SqlServer -sqlServer $servername -applicationName $applicationName}
# Add an event handler to the $server so that we can capture PRINT statements and low level error
# output
$sqlEventHandler = [System.Data.SqlClient.SqlInfoMessageEventHandler] {
param($sender, $event) Write-Host $event.Message
}
$server.ConnectionContext.add_InfoMessage($sqlEventHandler)
return $server
}
function Recurse-Folders {
param (
[Parameter(Position=0, Mandatory=$true)] [string]$folderspec
)
[string]$applicationName = 'PerformanceCollectorInstaller'
$server = New-SqlServerWithEvents $servername $username $password $applicationName
# starting with folderspec, traverse the directory tree
foreach ($subfolder in (Get-ChildItem $folderspec | Where-Object {$_.PSIsContainer -and $_.Name -ne "ThirdParty"}) | Sort-Object Name){
$subfolder = $subfolder.FullName
"Running folder $subfolder" | Out-Host
Recurse-Folders $subfolder
# within a folder, subfolders are processed *before* files.
foreach ($sqlScript in Get-ChildItem -Path $subfolder -Filter "*.sql" | Sort-Object Name) {
$sqlScript = $sqlScript.FullName
" Running script $sqlScript" | Out-Host
# Get the text of the script so that we can run it
[string]$scriptText = [System.IO.File]::ReadAllText("$sqlScript")
# Run the script on the server
Set-SqlData -sqlserver $server -dbname $databaseName -qry $scriptText
}
}
}
function Run-CreateDbScript {
#needs to be a separate script and process because we are connecting to master to do the work.
$sqlScript = Join-Path $sqlScriptDirectory "CreateDatabase.sql"
" Running script $sqlScript" | Out-Host
# Get the text of the script so that we can run it
[string]$scriptText = [System.IO.File]::ReadAllText("$sqlScript")
[string]$applicationName = 'PerformanceCollectorInstaller'
$server = New-SqlServerWithEvents $servername $username $password $applicationName
# Run the script on the server
Set-SqlData -sqlserver $server -dbname "master" -qry $scriptText
}
################################################################################
# BEGINNING OF MAIN SCRIPT
################################################################################
Try
{
Run-CreateDbScript
# Loop through all subdirectories
# Loop through all of the .sql scripts in the directory above
Recurse-Folders $sqlScriptDirectory
Write-Host "PerformanceCollector Deploy process has completed"
Exit 0
}
Catch [Microsoft.SqlServer.Management.Common.ConnectionFailureException]
{
Write-Host "Error connecting to the database. Please check the username and password and make sure that the server is available." -ForegroundColor Red
Exit 2
}
Catch
{
Write-Host "Error running script: $sqlScript" -ForegroundColor Red
# Iterate through the InnerExceptions until we get to the DbException
$currentException = $_.Exception
while ($currentException) {
Write-Host $currentException.Message
$lastException = $currentException
if ($currentException.GetType().Name -eq "SqlException") {break}
$currentException = $currentException.InnerException
}
# If a DbException was found then we can report SQL error information
if ($lastException.GetType().Name -eq "SqlException") {
[System.Data.Common.DbException]$sqlError = $lastException
[string]$loggingText = "Unhandled exception in script $sqlScript. Error message: " + $sqlError.Message
[byte]$sqlErrorState = $sqlError.get_State()
[int]$sqlErrorNumber = $sqlError.get_Number()
[int]$sqlErrorSeverity = $sqlError.get_Class()
[int]$sqlErrorLine = $sqlError.get_LineNumber()
Write-Host "SQL Error: $loggingText, Error Number: $sqlErrorNumber, Error Severity: $sqlErrorSeverity, Error State: $sqlErrorState, Error Line: $sqlErrorLine"
}
Exit 3
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment