Вы находитесь на странице: 1из 3

param (

$serverName = $(Throw "Server Name is a mandatory parameter"),


$mdfVolumePath = $(Throw "Mdf Volumne Path is a mandatory parameter")
)
begin {

$assemblylist =
"Microsoft.SqlServer.Management.Common",
"Microsoft.SqlServer.Smo",
"Microsoft.SqlServer.Dmf ",
"Microsoft.SqlServer.Instapi ",
"Microsoft.SqlServer.SqlWmiManagement ",
"Microsoft.SqlServer.ConnectionInfo ",
"Microsoft.SqlServer.SmoExtended ",
"Microsoft.SqlServer.SqlTDiagM ",
"Microsoft.SqlServer.SString ",
"Microsoft.SqlServer.Management.RegisteredServers ",
"Microsoft.SqlServer.Management.Sdk.Sfc ",
"Microsoft.SqlServer.SqlEnum ",
"Microsoft.SqlServer.RegSvrEnum ",
"Microsoft.SqlServer.WmiEnum ",
"Microsoft.SqlServer.ServiceBrokerEnum ",
"Microsoft.SqlServer.ConnectionInfoExtended ",
"Microsoft.SqlServer.Management.Collector ",
"Microsoft.SqlServer.Management.CollectorEnum",
"Microsoft.SqlServer.Management.Dac",
"Microsoft.SqlServer.Management.DacEnum",
"Microsoft.SqlServer.Management.Utility"

foreach ($asm in $assemblylist)


{
$asm = [Reflection.Assembly]::LoadWithPartialName($asm)
}

function Global:Connect-SqlServer{
param(
$serverName,
$userName
)
process {
Trap {
}

$smoServer = New-Object ("microsoft.sqlserver.management.smo.server")


$serverName
$smoServer.ConnectionContext.LoginSecure = $True

$smoServer.ConnectionContext.Connect()

return $smoServer
}
}

function Global:Execute-SqlQuery {
param (
$smoServer,
$database,
$query
)
trap {
}

$smodatabase = $smoServer.databases.item($database)

return $smodatabase.ExecuteQuery($query)
}

function Global:print-exception {
param(
$exception
)
$exception = $exception.Exception
Write-Host "`n**********Exception**********"
While ($exception) {
Write-Host $exception.Message
$exception = $exception.InnerException
}
Write-Host "*****************************`n"
}
}
process {

$DetachDBList = "AttachDBList.txt"

$totalDBs = (Get-Content $DetachDBList | Measure-Object -Line).Lines


$loopcount = 1

$smoServer = Connect-SqlServer $serverName

Get-Content $DetachDBList | % {
Trap {
print-exception $_
continue
}

$intend = ""
Write-Host $intend"**********"
Write-Host $intend"Processing DB: `"${_}`" ${loopcount} Of ${totalDBs}"
$intend = "`t"

$mdffilePath = Join-Path $mdfVolumePath "${_}.mdf"

Write-Host $intend"Attaching Database - STARTED"


$smoServer.AttachDatabase($_, $mdffilePath)
Write-Host $intend"Attaching Database - COMPLETED"

$SmoDatabase = $smoServer.Databases.item($_)

Write-Host $intend"***DB Owner Check***"


if ($SmoDatabase.Owner -ne "sa") {
Write-Host $intend"DB Owner is not `"sa`". Changing it now to sa"
$query = "exec sp_changedbowner 'sa'"
Write-Host $intend$query
$smoDatabase.ExecuteNonQuery($query)
}
else {
Write-Host $intend"DB Owner is already `"sa`". No Change required"
}

Write-Host $intend"***DB CompatibilityLevel Check***"


if ($SmoDatabase.CompatibilityLevel -ne "Version140") {
Write-Host $intend"DB CompatibilityLevel is not `"140`". Changing it
now to 140"
$SmoDatabase.CompatibilityLevel = "Version140"
$SmoDatabase.Alter()
}
else {
Write-Host $intend"DB CompatibilityLevel is already `"140`". No Change
required"
}

}
}

#e.g
#.\Attach.ps1 "DESKTOP-94KF83B" "C:\Program Files\Microsoft SQL
Server\MSSQL14.MSSQLSERVER\MSSQL\DATA"

Вам также может понравиться