-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRMUtils.psm1
More file actions
46 lines (43 loc) · 2.09 KB
/
RMUtils.psm1
File metadata and controls
46 lines (43 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Set-Variable DBPath -option Constant -value "\\tecnologiabh\VersoesHomologacao\BaseDeDadosSQL\"
function Get-RMDBVersions {
param(
[Parameter(Mandatory=$false)][string]$Version
)
Get-ChildItem $DBPath -Filter "Versao*" -Directory | ForEach-Object{
[pscustomobject]@{
Version=$_
Path=Join-Path -Path $DBPath -ChildPath $_
}
} | Where-Object {!($Version) -or $_.Version -match $Version}
}
function New-TemporaryDirectory {
$parent = "C:\"
$name = [System.IO.Path]::GetRandomFileName()
New-Item -ItemType Directory -Path (Join-Path $parent $name)
}
function Restore-RMDB {
[cmdletbinding()]
param (
[Parameter(ParameterSetName="FromParameter", Mandatory=$true)][string]$Version,
[Parameter(ParameterSetName="FromInput", Mandatory=$true, ValueFromPipeline=$true)][pscustomobject]$InputObject,
[Parameter(Mandatory=$true)][string]$Name
)
if(!($InputObject)) {
$InputObject = Get-RMDBVersions -Version $Version | Select-Object -Index 0
}
$TempDir = (New-TemporaryDirectory).FullName
$ZipDir = Join-Path $InputObject.Path "\CorporeRM\Dados"
$ZipFile = (Get-ChildItem $ZipDir -Filter "EXEMPLO*.zip" | Select-Object -Index 0).FullName
Expand-Archive -Path $ZipFile -DestinationPath $TempDir
$BakFile = (Get-ChildItem $TempDir -Filter "*.bak" | Select-Object -Index 0).FullName
try {
$RelocateData = Join-Path "C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\" ("{0}.mdf" -f $Name)
$RelocateLog = Join-Path "C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\" ("{0}.ldf" -f $Name)
$Query = "RESTORE DATABASE [{0}] FROM DISK = N'{1}' WITH FILE = 1, MOVE N'Exemplo_Data' TO N'{2}', MOVE N'Exemplo_Log' TO N'{3}', NOUNLOAD, STATS = 5" -f $Name, $BakFile, $RelocateData, $RelocateLog
sqlcmd -Q $Query
}
finally {
Remove-Item -Recurse -Force -Path $TempDir
}
Invoke-Command -ScriptBlock {sqlcmd -i (Join-Path $ZipDir "Usuarios SQL 2012.sql") -d $Name } -ErrorAction SilentlyContinue | Out-Null
}