Skip to content

Commit ad0ef9b

Browse files
committed
Initial commit
1 parent 1813cfe commit ad0ef9b

36 files changed

Lines changed: 2911 additions & 0 deletions

PSDatabasClone.psd1

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
@{
2+
# Script module or binary module file associated with this manifest
3+
ModuleToProcess = 'PSDatabasClone.psm1'
4+
5+
# Version number of this module.
6+
ModuleVersion = '1.0.0.0'
7+
8+
# ID used to uniquely identify this module
9+
GUID = '747980ca-0d3d-4529-b4fd-e6cdd059c62a'
10+
11+
# Author of this module
12+
Author = 'sander'
13+
14+
# Company or vendor of this module
15+
CompanyName = 'MyCompany'
16+
17+
# Copyright statement for this module
18+
Copyright = 'Copyright (c) 2018 sander'
19+
20+
# Description of the functionality provided by this module
21+
Description = 'Cloning module for SQL Server databases'
22+
23+
# Minimum version of the Windows PowerShell engine required by this module
24+
PowerShellVersion = '5.0'
25+
26+
# Name of the Windows PowerShell host required by this module
27+
# PowerShellHostName = ''
28+
29+
# Minimum version of the Windows PowerShell host required by this module
30+
# PowerShellHostVersion = ''
31+
32+
# Minimum version of the .NET Framework required by this module
33+
# DotNetFrameworkVersion = '2.0'
34+
35+
# Minimum version of the common language runtime (CLR) required by this module
36+
# CLRVersion = '2.0.50727'
37+
38+
# Processor architecture (None, X86, Amd64, IA64) required by this module
39+
# ProcessorArchitecture = 'None'
40+
41+
# Modules that must be imported into the global environment prior to importing
42+
# this module
43+
RequiredModules = @(
44+
@{ ModuleName = 'PSFramework'; ModuleVersion = '0.9.10.23' },
45+
@{ ModuleName = 'dbatools'; ModuleVersion = '0.9.337' }
46+
)
47+
48+
# Assemblies that must be loaded prior to importing this module
49+
# RequiredAssemblies = @()
50+
51+
# Script files (.ps1) that are run in the caller's environment prior to
52+
# importing this module
53+
# ScriptsToProcess = @()
54+
55+
# Type files (.ps1xml) to be loaded when importing this module
56+
# TypesToProcess = @('xml\PSDatabasClone.Types.ps1xml')
57+
58+
# Format files (.ps1xml) to be loaded when importing this module
59+
# FormatsToProcess = @('xml\PSDatabasClone.Format.ps1xml')
60+
61+
# Modules to import as nested modules of the module specified in
62+
# ModuleToProcess
63+
# NestedModules = @()
64+
65+
# Functions to export from this module
66+
FunctionsToExport = 'Initialize-PdcVhdDisk',
67+
'Invoke-PdcRepairClone',
68+
'New-PdcDatabaseClone',
69+
'New-PdcDatabaseImage',
70+
'New-PdcVhdDisk',
71+
'Remove-PdcDatabaseClone',
72+
'Remove-PdcDatabaseImage',
73+
'Set-PdcConfiguration'
74+
75+
# Cmdlets to export from this module
76+
CmdletsToExport = ''
77+
78+
# Variables to export from this module
79+
VariablesToExport = ''
80+
81+
# Aliases to export from this module
82+
AliasesToExport = ''
83+
84+
# List of all modules packaged with this module
85+
ModuleList = @()
86+
87+
# List of all files packaged with this module
88+
FileList = @()
89+
90+
# Private data to pass to the module specified in ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
91+
PrivateData = @{
92+
93+
#Support for PowerShellGet galleries.
94+
PSData = @{
95+
96+
# Tags applied to this module. These help with module discovery in online galleries.
97+
# Tags = @()
98+
99+
# A URL to the license for this module.
100+
# LicenseUri = ''
101+
102+
# A URL to the main website for this project.
103+
# ProjectUri = ''
104+
105+
# A URL to an icon representing this module.
106+
# IconUri = ''
107+
108+
# ReleaseNotes of this module
109+
# ReleaseNotes = ''
110+
111+
} # End of PSData hashtable
112+
113+
} # End of PrivateData hashtable
114+
}

PSDatabasClone.psm1

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
$script:ModuleRoot = $PSScriptRoot
2+
$script:ModuleVersion = "1.0.0.0"
3+
4+
function Import-ModuleFile
5+
{
6+
<#
7+
.SYNOPSIS
8+
Loads files into the module on module import.
9+
10+
.DESCRIPTION
11+
This helper function is used during module initialization.
12+
It should always be dotsourced itself, in order to proper function.
13+
14+
This provides a central location to react to files being imported, if later desired
15+
16+
.PARAMETER Path
17+
The path to the file to load
18+
19+
.EXAMPLE
20+
PS C:\> . Import-ModuleFile -File $function.FullName
21+
22+
Imports the file stored in $function according to import policy
23+
#>
24+
[CmdletBinding()]
25+
Param (
26+
[string]
27+
$Path
28+
)
29+
30+
if ($doDotSource) { . $Path }
31+
else { $ExecutionContext.InvokeCommand.InvokeScript($false, ([scriptblock]::Create([io.file]::ReadAllText($Path))), $null, $null) }
32+
}
33+
34+
# Detect whether at some level dotsourcing was enforced
35+
$script:doDotSource = Get-PSFConfigValue -FullName PSDatabasClone.Import.DoDotSource -Fallback $false
36+
if ($PSDatabasClone_dotsourcemodule) { $script:doDotSource = $true }
37+
38+
# Execute Preimport actions
39+
. Import-ModuleFile -Path "$ModuleRoot\internal\scripts\preimport.ps1"
40+
41+
# Import all internal functions
42+
foreach ($function in (Get-ChildItem "$ModuleRoot\internal\functions" -Filter "*.ps1" -Recurse -ErrorAction Ignore))
43+
{
44+
. Import-ModuleFile -Path $function.FullName
45+
}
46+
47+
# Import all public functions
48+
foreach ($function in (Get-ChildItem "$ModuleRoot\functions" -Filter "*.ps1" -Recurse -ErrorAction Ignore))
49+
{
50+
. Import-ModuleFile -Path $function.FullName
51+
}
52+
53+
# Execute Postimport actions
54+
. Import-ModuleFile -Path "$ModuleRoot\internal\scripts\postimport.ps1"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
TOPIC
2+
about_PSDatabasClone
3+
4+
SHORT DESCRIPTION
5+
Explains how to use the PSDatabasClone powershell module
6+
7+
LONG DESCRIPTION
8+
<Insert Content here>
9+
10+
KEYWORDS
11+
PSDatabasClone
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
function Initialize-PdcVhdDisk {
2+
<#
3+
.SYNOPSIS
4+
Initialize-PdcVhdDisk initialized the VHD
5+
6+
.DESCRIPTION
7+
Initialize-PdcVhdDisk will initialize the VHD.
8+
It mounts the disk, creates a volume, creates the partition and sets it to active
9+
10+
.PARAMETER Path
11+
The path to the VHD
12+
13+
.PARAMETER Credential
14+
Allows you to use credentials for creating items in other locations To use:
15+
16+
$scred = Get-Credential, then pass $scred object to the -Credential parameter.
17+
18+
.NOTES
19+
Author: Sander Stad (@sqlstad, sqlstad.nl)
20+
21+
Website: https://easyclone.io
22+
Copyright: (C) Sander Stad, sander@sqlstad.nl
23+
License: MIT https://opensource.org/licenses/MIT
24+
25+
.LINK
26+
https://easyclone.io/
27+
28+
.EXAMPLE
29+
Initialize-PdcVhdDisk -Path $path
30+
31+
Initialize the disk pointing to the path with all default settings
32+
33+
.EXAMPLE
34+
Initialize-PdcVhdDisk -Path $path -AllocationUnitSize 4KB
35+
36+
Initialize the disk and format the partition with a 4Kb allocation unit size
37+
38+
#>
39+
40+
[CmdLetBinding()]
41+
42+
Param(
43+
[Parameter(Mandatory = $true)]
44+
[string[]]$Path,
45+
[System.Management.Automation.PSCredential]
46+
$Credential,
47+
[ValidateSet('GPT', 'MBR')]
48+
[string]$PartitionStyle = 'GPT',
49+
[int]$AllocationUnitSize = 64KB
50+
)
51+
52+
begin {
53+
# Check the path to the vhd
54+
if (-not (Test-Path -Path $Path -Credential $Credential)) {
55+
Stop-PSFFunction -Message "Vhd path cannot be found" -Target $Path -Continue
56+
}
57+
}
58+
59+
process {
60+
# Test if there are any errors
61+
if (Test-PSFFunctionInterrupt) { return }
62+
63+
# Get all the disks
64+
$disks = Get-Disk | Select-Object Number, Location, OperationalStatus
65+
66+
# Check if disk is already mounted
67+
if ($disks.Location -contains $Path) {
68+
Write-PSFMessage -Message "Vhd is already mounted" -Level Warning
69+
70+
# retrieve the specific disk
71+
$disk = $disks | Where-Object {$_.Location -eq $Path}
72+
}
73+
else {
74+
# Mount the vhd
75+
try {
76+
Write-PSFMessage -Message "Mounting disk $disk" -Level Verbose
77+
78+
$disk = Mount-VHD -Path $Path -PassThru | Get-Disk
79+
}
80+
catch {
81+
Stop-PSFFunction -Message "Couldn't mount vhd" -Target $Path -ErrorRecord $_ -Continue
82+
}
83+
}
84+
85+
# Check if the disk is already initialized
86+
if ($disk.PartitionStyle -eq 'RAW') {
87+
try {
88+
Write-PSFMessage -Message "Initializing disk $disk" -Level Verbose
89+
$disk | Initialize-Disk -PartitionStyle $PartitionStyle -Confirm:$false
90+
}
91+
catch {
92+
Stop-PSFFunction -Message "Couldn't initialize disk" -Target $disk -ErrorRecord $_ -Continue
93+
}
94+
}
95+
96+
# Create the partition, set the drive letter and format the volume
97+
try {
98+
$volume = Get-Disk -Number $disk.DiskNumber | New-Partition -UseMaximumSize | Format-Volume -FileSystem NTFS -NewFileSystemLabel "EasyClone" -AllocationUnitSize $AllocationUnitSize -Confirm:$false
99+
}
100+
catch {
101+
# Dismount the drive
102+
Dismount-VHD -Path $Path
103+
104+
Stop-PSFFunction -Message "Couldn't create the partition" -Target $disk -ErrorRecord $_ -Continue
105+
}
106+
107+
# Add the results to the custom object
108+
[PSCustomObject]@{
109+
DiskNumber = $disk.DiskNumber
110+
Disk = $disk
111+
Partition = (Get-Partition -Disk $disk)
112+
Volume = $volume
113+
}
114+
115+
}
116+
117+
end {
118+
# Test if there are any errors
119+
if (Test-PSFFunctionInterrupt) { return }
120+
121+
Write-PSFMessage -Message "Finished initializing disk(s)" -Level Verbose
122+
}
123+
124+
}

0 commit comments

Comments
 (0)