Skip to content

Commit

Permalink
Initial check-in.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael J. Messano committed Oct 7, 2012
0 parents commit 2c61c3c
Show file tree
Hide file tree
Showing 68 changed files with 9,238 additions and 0 deletions.
54 changes: 54 additions & 0 deletions Add-SqlServerStartupParameter.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
##############################################################################
##
## Add-SqlServerStartupParameter
##
## by Eric Humphrey (http://www.erichumphrey.com/category/powershell/)
##
## http://www.sqlservercentral.com/blogs/erichumphrey/archive/2011/3/31/change-sql-startup-parameters-with-powershell.aspx
##############################################################################

<#
.SYNOPSIS
Adds an entry to the startup parameters list for all instances of SQL Server
on a computer
.EXAMPLE
PS >Add-SqlServerStartupParameter '-T3226'
#>

param(
## The parameter you wish to add
[Parameter(Mandatory = $true)]
$StartupParameter
)

$hklmRootNode = "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server"

$props = Get-ItemProperty "$hklmRootNode\Instance Names\SQL"
$instances = $props.psobject.properties | ?{$_.Value -like 'MSSQL*'} | select Value

$instances | %{
$inst = $_.Value;
$regKey = "$hklmRootNode\$inst\MSSQLServer\Parameters"
$props = Get-ItemProperty $regKey
$params = $props.psobject.properties | ?{$_.Name -like 'SQLArg*'} | select Name, Value
#$params | ft -AutoSize
$hasFlag = $false
foreach ($param in $params) {
if($param.Value -eq $StartupParameter) {
$hasFlag = $true
break;
}
}
if (-not $hasFlag) {
"Adding $StartupParameter"
$newRegProp = "SQLArg"+($params.Count)
Set-ItemProperty -Path $regKey -Name $newRegProp -Value $StartupParameter
} else {
"$StartupParameter already set"
}
}
142 changes: 142 additions & 0 deletions Audit-DatabasesPerServer.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | Out-Null;
$smoObj = [Microsoft.SqlServer.Management.Smo.SmoApplication];

# functions
function Run-Query()
{
param (
$SqlQuery,
$SqlServer,
$SqlCatalog
)

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection("Data Source=$SqlServer;Integrated Security=SSPI;Initial Catalog=$SqlCatalog;");

$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection

$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd

$DataSet = New-Object System.Data.DataSet
$a = $SqlAdapter.Fill($DataSet)

$SqlConnection.Close()

$DataSet.Tables | Select-Object -ExpandProperty Rows
}
################################################################################

# SQL query to pull back the list of SQL servers from XSQLUTIL18.Status
$ServerQuery =
"SELECT distinct RTRIM(LTRIM([server_name])) AS ServerName
, ssd.InstanceName
, ssd.ProductVersion
FROM [t_server] s
INNER JOIN [t_server_type_assoc] sta on s.server_id = sta.server_id
INNER JOIN [t_server_type] st on sta.type_id = st.type_id
INNER JOIN [t_environment] e on s.environment_id = e.environment_id
INNER JOIN [t_monitoring] m on s.server_id = m.server_id
INNER JOIN [SQLServerDetails] ssd on s.server_id = ssd.serverid
where type_name = 'DB'
AND active = 1
ORDER BY 1"

# This gets the sql servers
#$sql = $smoObj::EnumAvailableSqlServers($false);
$sqlservers = Run-Query -SqlQuery $ServerQuery -SqlServer XSQLUTIL18 -SqlCatalog Status | Select-Object -Property ServerName, InstanceName, ProductVersion | Sort-Object -Property ServerName
foreach ($sqlserver IN $sqlservers) {
Write-Host "Server: $($sqlserver.ServerName)"
}
# Automate Excel
$xl = New-Object -ComObject Excel.Application;
$xl.Visible = $true;
$xl = $xl.Workbooks.Add();
$Sheet = $xl.Worksheets.Item(1);

$row = 1;

foreach($sqlserver in $sqlservers)
{
# headers
$Sheet.Cells.Item($row, 1) = "Sql Server:";
$Sheet.Cells.Item($row, 2) = $($sqlserver.ServerName);
$Sheet.Cells.Item($row, 1).Font.Bold = $true;
$Sheet.Cells.Item($row, 2).Font.Bold = $true;
$Sheet.Cells.Item($row, 3) = "";
$Sheet.Cells.Item($row, 4) = "Instance:";
$Sheet.Cells.Item($row, 5) = $($sqlserver.InstanceName);
$Sheet.Cells.Item($row, 4).Font.Bold = $true;
$Sheet.Cells.Item($row, 5).Font.Bold = $true;
$Sheet.Cells.Item($row, 6) = "";
$Sheet.Cells.Item($row, 7) = "Version: ";
$Sheet.Cells.Item($row, 8) = $($sqlserver.ProductVersion);
$Sheet.Cells.Item($row, 7).Font.Bold = $true;
$Sheet.Cells.Item($row, 8).Font.Bold = $true;

# Prettify headers
for($i = 1; $i -le 8; $i++)
{
$Sheet.Cells.Item($row,$i).Interior.ColorIndex = 50;
$Sheet.Cells.Item($row,$i).Font.ColorIndex = 20;
}

# Create obj for this sql server
$srv = New-Object "Microsoft.SqlServer.Management.Smo.Server" $($sqlserver.ServerName);
# Get the databases on this sql server
$databases = $srv.Databases;

# Increase rowcount for formatting
$row += 2;

# Add column headers for databases
$Sheet.Cells.Item($row, 1) = "Database";
$Sheet.Cells.Item($row, 2) = "Size";
$Sheet.Cells.Item($row, 3) = "SpaceAvailable";
$Sheet.Cells.Item($row, 4) = "State";
$Sheet.Cells.Item($row, 5) = "Table Count";
$Sheet.Cells.Item($row, 6) = "Collation";
$Sheet.Cells.Item($row, 7) = "Compatibility Level";
$Sheet.Cells.Item($row, 8) = "Create Date";
$Sheet.Cells.Item($row, 9) = "Index Space Usage";
$Sheet.Cells.Item($row, 10) = "Owner";
$Sheet.Cells.Item($row, 11) = "Last Backup";
$Sheet.Cells.Item($row, 12) = "Trigger Count";
$Sheet.Cells.Item($row, 13) = "UDF Count";
for($i = 1; $i -le 13; $i++)
{
$Sheet.Cells.Item($row,$i).Interior.ColorIndex = 35;
$Sheet.Cells.Item($row,$i).Font.ColorIndex = 0;
$Sheet.Cells.Item($row, $i).Font.Bold = $true;
}
$row++;
# Work through each database in the collection
foreach($db in $databases)
{
$Sheet.Cells.Item($row, 1) = $db.Name;
$Sheet.Cells.Item($row, 2) = $db.Size;
$Sheet.Cells.Item($row, 3) = $db.SpaceAvailable;
$Sheet.Cells.Item($row, 4) = $db.State;
$Sheet.Cells.Item($row, 5) = $db.Tables.Count;
$Sheet.Cells.Item($row, 6) = $db.Collation;
$Sheet.Cells.Item($row, 7) = $db.CompatibilityLevel;
$Sheet.Cells.Item($row, 8) = $db.CreateDate;
$Sheet.Cells.Item($row, 9) = $db.IndexSpaceUsage;
$Sheet.Cells.Item($row, 10) = $db.Owner;
$Sheet.Cells.Item($row, 11) = $db.LastBackupDate;
$Sheet.Cells.Item($row, 12) = $db.Triggers.Count;
$Sheet.Cells.Item($row, 13) = $db.UserDefinedFunctions.Count;
for($i = 1; $i -le 13; $i++)
{
$Sheet.Cells.Item($row,$i).Interior.ColorIndex = 0;
$Sheet.Cells.Item($row,$i).Font.ColorIndex = 0;
}
$row++;
}

$row++;
}

# Apply autoformat
$Sheet.UsedRange.EntireColumn.AutoFit();
167 changes: 167 additions & 0 deletions BODataCacheFlush.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
# Stops and starts the asoociated services and flushes the Data directory
# which holds all the cached reports
param(
$Environment
)

#$BOServers = @{
# "PROD" = @{ "BOServer" = 'PAPPBO10';
# "BOServiceName" = 'BOE120SIAPAPPBO10';
# "BOServiceDisplaName" = 'Server Intelligence Agent (PAPPBO10)';
# "BODataFolderPath" = '\\pappbo10\e$\Program Files\Business Objects\BusinessObjects Enterprise 12.0\Data'
# "BOWebService" = 'World Wide Web Publishing Service';
# };
# "IMP" = @{ "BOServer" = 'IAPPBO510';
# "BOServiceName" = 'BOE120SIAIAPPBO510';
# "BOServiceDisplaName" = 'Server Intelligence Agent (IAPPBO510)';
# "BODataFolderPath" = '\\IAPPBO510\e$\Program Files\Business Objects\BusinessObjects Enterprise 12.0\Data\'
# "BOWebService" = 'W3SVC';
# };
# }

switch ($Environment)
{
PROD1 { $Server = 'PAPPBO20';
$BOService = 'BOE120SIAPAPPBO20';
$BOWebService = 'W3SVC';
$BODirectory = 'E:\Business Objects\BusinessObjects Enterprise 12.0\Data';
$BODirOld = 'E:\Business Objects\BusinessObjects Enterprise 12.0\DataOld';
}
PROD2 { $Server = 'PAPPBO21';
$BOService = 'BOE120SIAPAPPBO21';
$BOWebService = 'W3SVC';
$BODirectory = 'E:\Business Objects\BusinessObjects Enterprise 12.0\Data';
$BODirOld = 'E:\Business Objects\BusinessObjects Enterprise 12.0\DataOld';
}
IMP { $Server = 'IAPPBO510';
$BOService = 'BOE120SIAIAPPBO510';
$BOWebService = 'W3SVC';
$BODirectory = 'E:\Program Files\Business Objects\BusinessObjects Enterprise 12.0\Data';
$BODirOld = 'E:\Program Files\Business Objects\BusinessObjects Enterprise 12.0\DataOld';
}
}

# Functions
Function BORemoveDataFolder
{
param ($Dir);
$TargetFolder = $Dir;
if (Test-Path $TargetFolder)
{
try
{
Remove-Item $TargetFolder -Recurse;
}
catch
{
Write-Host "`tTried to delete $TargetFolder and failed!";
}
finally
{
Write-Host "`tThe '$TargetFolder' folder was deleted successfully.";
}
}
Else
{
Write-Host "`tThe Folder $TargetFolder does not Exist!";
}
}

Function StopBOService
{
try
{
Stop-Service $BOService;
}
catch
{
Write-Host "The $BOService service failed to stop!";
}
finally
{
Write-Host "`tThe $BOService service was stopped successfully.";
}
}

Function StopBOWebService
{
try
{
Stop-Service $BOWebService;
}
catch
{
Write-Host "The $BOWebService service failed to stop!";
}
finally
{
Write-Host "`tThe $BOWebService service was stopped successfully.";
}
}

Function SendMail
{
$emailFrom = $Server + "@dexma.com";
$emailTo = "[email protected]";
$subject = $Server + " Data Folder Maintenance";
$smtpServer = "Outbound.smtp.dexma.com";
#$body = "The " + '"' + "$BODirectory" + '"' + " folder has been deleted to clear the old cache files.";
$smtp = new-object Net.Mail.SmtpClient($smtpServer);
$smtp.Send($emailFrom, $emailTo, $subject, $body);
}

# get the last day of the month
$LastDayOfMonth = (Get-Date -Year (Get-Date).Year -Month (Get-Date).Month -Day 1).AddMonths(1).AddDays(-1);
$DateDiff = New-TimeSpan $(Get-Date) $lastDayOfMonth;

# used for testing
#$DateDiff = New-TimeSpan $(Get-Date -month 9 -day 26 -year 2011) $lastDayOfMonth


if ( (Get-Date).DayOfWeek -eq 'Sunday' ) {
# do it all
# stop the BO and Web services
Write-Host "Stopping services...";
StopBOService;
StopBOWebService;
# Move the Data folder
Write-Host "Moving folder...";
Move-Item $BODirectory $BODirOld;
# start the BO and Web services
Write-Host "Starting Services...";
Write-Host "`t...$BOService...";
Start-Service -Name $BOService;
Write-Host "`t...$BOWebService...";
Start-Service -Name $BOWebService;
# Remove the Data folder
Write-Host "Removing folder..."
BORemoveDataFolder $BODirOld;
# Send email
$body = "The " + '"' + "$BODirectory" + '"' + " folder has been deleted to clear the old cache files.";
SendMail;
}
elseif ( ((Get-Date).Day -eq '1') -or (($DateDiff).days -eq '0') -or (($DateDiff).days -eq '1') ) {
# stop the BO service
Write-Host "The date is within two days of the end of the month or is the first of the month.";
Write-Host "Stopping BO service...";
StopBOService;
# start the BO service
Write-Host "Starting BO Service...";
Write-Host "`t...$BOService...";
Start-Service -Name $BOService;
# send email
$body = "The Server Intelligence Agent " + '"' + "$BOService" + '"' + " service has been restarted for month end reporting availability.";
SendMail;
}
elseif ( (($DateDiff).days -ge '2') ) {
Write-Host "The end-of-month date is greater than 2 days away and it is not Sunday.";
$body = "No changes were made.";
#SendMail;
}

# check service status
Write-Host "Verifying services are started......";
Get-Service -ComputerName $Server -Name $BOService;
Get-Service -ComputerName $Server -Name $BOWebService;


25 changes: 25 additions & 0 deletions BackupDates.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
$a = "XSQLUTIL18", "XSQLUTIL19"

$a | % `
{
$ServerName = $_;

cd sqlserver:\
cd sql\$ServerName\default\databases

$DBs = dir;

$DBs | % `
{
$LastBackupDate = $_.LastBackupDate;
$LastLogBackupDate = $_.LastLogBackupDate;
$LastDiffBackupDate = $_.LastDifferentialBackupDate;

invoke-sqlcmd -ServerInstance "XSQLUTIL18" -Database "dbamaint" -query "Insert BackupInfo SELECT '$ServerName', '$lastBackupDate', '$LastLogBackupDate', '$LastDiffBackupDate'" -SuppressProviderContextWarning
$ServerName;
$_.Name;
}

#ft name, LastBackupDate, LastLogBackupDate -autosize

}
Loading

0 comments on commit 2c61c3c

Please sign in to comment.