Push to SQL Server
</pre>
# Local Variables
$base_path = "C:\Temp"
$local_repo_name = "Blitz"
$local_repo_directory = $base_path + "\" + $local_repo_name
# Remote Variables
$remote_repo_url = "https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit.git"
$branch_name = "master"
# Git commands
$clone_cmd = "git clone " + $remote_repo_url + " --branch " + $branch_name + " " + $local_repo_name;
$merge_cmd = "git pull origin"
$branch_check_cmd = "git ls-remote --heads " + $remote_repo_url + " " + $branch_name
# Change the working directory
CD $base_path
# Check the existence of the remote repo and capture the number of rows that match
# in the result set of the query in $branch_check_cmd.
# Then clone or merge, depending if the local repo exists.
$number_of_matches = Invoke-Expression $branch_check_cmd | Measure-Object
IF ( $number_of_matches.Count -eq 1 ) {
IF ( -not (Test-Path( $local_repo_name )) ) {
# The local repo does not exist, let's grab a copy
Invoke-Expression $clone_cmd
CD $local_repo_name
} ELSE {
# The local repo exists, commence merge to pull in most recent changes
CD $local_repo_name
Invoke-Expression $merge_cmd
}
# Database Variables
$instance_name = "localhost\sql16"
$database_name = "master"
# Retrieves SQL scripts that match desired criteria and executes them in the target database
Get-ChildItem |
Where-Object { ($_.Name -eq "sp_Blitz.sql") } |
ForEach-Object {Invoke-Sqlcmd -ServerInstance $instance_name -Database $database_name -QueryTimeout 65535 -InputFile $_.FullName}
} ELSE {
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("Crap, something went wrong.",0,"Error",0x1)
}