Академический Документы
Профессиональный Документы
Культура Документы
$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"
function Global:Connect-SqlServer{
param(
$serverName,
$userName
)
process {
Trap {
}
$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"
Get-Content $DetachDBList | % {
Trap {
print-exception $_
continue
}
$intend = ""
Write-Host $intend"**********"
Write-Host $intend"Processing DB: `"${_}`" ${loopcount} Of ${totalDBs}"
$intend = "`t"
$SmoDatabase = $smoServer.Databases.item($_)
}
}
#e.g
#.\Attach.ps1 "DESKTOP-94KF83B" "C:\Program Files\Microsoft SQL
Server\MSSQL14.MSSQLSERVER\MSSQL\DATA"