|
21 | 21 | Windows Authentication will be used if SqlCredential is not specified. SQL Server does not accept Windows credentials being passed as credentials. |
22 | 22 | To connect as a different Windows user, run PowerShell as that user. |
23 | 23 |
|
| 24 | + .PARAMETER PSDCSqlCredential |
| 25 | + Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted. |
| 26 | + This works similar as SqlCredential but is only meant for authentication to the the host |
| 27 | +
|
24 | 28 | .PARAMETER PSDCSqlCredential |
25 | 29 | Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted. |
26 | 30 | This works similar as SqlCredential but is only meant for authentication to the PSDatabaseClone database server and database. |
|
61 | 65 | [System.Management.Automation.PSCredential] |
62 | 66 | $SqlCredential, |
63 | 67 | [System.Management.Automation.PSCredential] |
64 | | - $PSDCSqlCredential, |
| 68 | + $Credential, |
| 69 | + [System.Management.Automation.PSCredential] |
| 70 | + $pdcCredential, |
65 | 71 | [switch]$EnableException |
66 | 72 | ) |
67 | 73 |
|
68 | 74 | begin { |
| 75 | + # Get the module configurations |
| 76 | + $pdcSqlInstance = Get-PSFConfigValue -FullName psdatabaseclone.database.Server |
| 77 | + $pdcDatabase = Get-PSFConfigValue -FullName psdatabaseclone.database.name |
| 78 | + if (-not $pdcCredential) { |
| 79 | + $pdcCredential = Get-PSFConfigValue -FullName psdatabaseclone.database.credential -Fallback $null |
| 80 | + } |
| 81 | + else { |
| 82 | + $pdcCredential = $pdcCredential |
| 83 | + } |
| 84 | + |
69 | 85 | # Test the module database setup |
70 | 86 | try { |
71 | | - Test-PSDCConfiguration -SqlCredential $PSDCSqlCredential -EnableException |
| 87 | + Test-PSDCConfiguration -SqlCredential $pdcCredential -EnableException |
72 | 88 | } |
73 | 89 | catch { |
74 | 90 | Stop-PSFFunction -Message "Something is wrong in the module configuration" -ErrorRecord $_ -Continue |
75 | 91 | } |
76 | 92 |
|
77 | | - $pdcSqlInstance = Get-PSFConfigValue -FullName psdatabaseclone.database.server |
78 | | - $pdcDatabase = Get-PSFConfigValue -FullName psdatabaseclone.database.name |
79 | | - |
80 | 93 | } |
81 | 94 |
|
82 | 95 | process { |
|
86 | 99 | # Loop through each of the hosts |
87 | 100 | foreach ($hst in $HostName) { |
88 | 101 |
|
| 102 | + # Setup the computer object |
| 103 | + $computer = [PsfComputer]$hst |
| 104 | + |
| 105 | + if (-not $computer.IsLocalhost) { |
| 106 | + $command = [scriptblock]::Create("Import-Module PSDatabaseClone") |
| 107 | + |
| 108 | + try { |
| 109 | + Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
| 110 | + } |
| 111 | + catch { |
| 112 | + Stop-PSFFunction -Message "Couldn't import module remotely" -Target $command |
| 113 | + return |
| 114 | + } |
| 115 | + } |
| 116 | + |
89 | 117 | $query = " |
90 | 118 | SELECT i.ImageLocation, |
91 | 119 | c.CloneLocation, |
|
105 | 133 | # Get the clones registered for the host |
106 | 134 | try { |
107 | 135 | Write-PSFMessage -Message "Get the clones for host $hst" -Level Verbose |
108 | | - $results = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $PSDCSqlCredential -Database $pdcDatabase -Query $query |
| 136 | + $results = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $pdcCredential -Database $pdcDatabase -Query $query |
109 | 137 | } |
110 | 138 | catch { |
111 | 139 | Stop-PSFFunction -Message "Couldn't get the clones for $hst" -Target $pdcSqlInstance -ErrorRecord $_ -Continue |
|
125 | 153 | try { |
126 | 154 | Write-PSFMessage -Message "Mounting vhd $($result.CloneLocation)" -Level Verbose |
127 | 155 |
|
128 | | - Mount-VHD -Path $result.CloneLocation -NoDriveLetter -ErrorAction SilentlyContinue |
| 156 | + # Check if computer is local |
| 157 | + if ($computer.IsLocalhost) { |
| 158 | + $null = Mount-VHD -Path $result.CloneLocation -NoDriveLetter -ErrorAction SilentlyContinue |
| 159 | + } |
| 160 | + else { |
| 161 | + $command = [ScriptBlock]::Create("Mount-VHD -Path $($result.CloneLocation) -NoDriveLetter -ErrorAction SilentlyContinue") |
| 162 | + $null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
| 163 | + } |
129 | 164 | } |
130 | 165 | catch { |
131 | 166 | Stop-PSFFunction -Message "Couldn't mount vhd" -Target $clone -Continue |
|
139 | 174 | if ($result.DatabaseName -notin $databases.Name) { |
140 | 175 |
|
141 | 176 | # Get all the files of the database |
142 | | - $databaseFiles = Get-ChildItem -Path $result.AccessPath -Recurse | Where-Object {-not $_.PSIsContainer} |
| 177 | + # Check if computer is local |
| 178 | + if ($computer.IsLocalhost) { |
| 179 | + $databaseFiles = Get-ChildItem -Path $result.AccessPath -Recurse | Where-Object {-not $_.PSIsContainer} |
| 180 | + } |
| 181 | + else { |
| 182 | + $commandText = "Get-ChildItem -Path $($result.AccessPath) -Recurse | " + 'Where-Object {-not $_.PSIsContainer}' |
| 183 | + $command = [ScriptBlock]::Create($commandText) |
| 184 | + $databaseFiles = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
| 185 | + } |
143 | 186 |
|
144 | 187 | # Setup the database filestructure |
145 | 188 | $dbFileStructure = New-Object System.Collections.Specialized.StringCollection |
|
0 commit comments