1+ function Clear-ComputerWorkloadHVVanilla {
2+ # Requires -Version 3.0
3+ # Requires -Modules ResourceLocker
4+
5+ [CmdletBinding ()]
6+ [OutputType ([System.Boolean ])]
7+ Param (
8+ [Parameter (Mandatory )]
9+ [string ]$ComputerName ,
10+ [Parameter (Mandatory )]
11+ [string ]$DestinationVMHostName ,
12+ [Parameter (Mandatory )]
13+ [string ]$DestinationVMHostPath ,
14+ [scriptblock ]$SourceFilter ,
15+ [scriptblock ]$DestinationFilter ,
16+ [int ]$MaxParallelMigrations ,
17+ [switch ]$PutInASubfolder = $ModuleWideHVVanillaPutInASubfolder ,
18+ [ref ]$DestinationVMHostLock
19+ )
20+
21+ $ErrorActionPreference = ' Stop'
22+
23+ Write-Debug - Message (' ENTER {0}' -f $MyInvocation.MyCommand.Name )
24+
25+ try {
26+ Write-Debug - Message (' ENTER TRY {0}' -f $MyInvocation.MyCommand.Name )
27+
28+ Write-Debug - Message (' $ComputerName = '' {0}'' ' -f $ComputerName )
29+ Write-Debug - Message (' $DestinationVMHostName = '' {0}'' ' -f $DestinationVMHostName )
30+ Write-Debug - Message (' $DestinationVMHostPath = '' {0}'' ' -f $DestinationVMHostPath )
31+ Write-Debug - Message (' $SourceFilter = {{{0}}}' -f $SourceFilter )
32+ Write-Debug - Message (' $DestinationFilter = {{{0}}}' -f $DestinationFilter )
33+ Write-Debug - Message (' $MaxParallelMigrations = {0}' -f $MaxParallelMigrations )
34+ Write-Debug - Message (' $PutInASubfolder = ${0}' -f $PutInASubfolder )
35+ Write-Debug - Message (' $DestinationVMHostLock: '' {0}'' ' -f $DestinationVMHostLock )
36+ Write-Debug - Message (' $DestinationVMHostLock.Value: '' {0}'' ' -f $DestinationVMHostLock.Value )
37+
38+ Write-Debug - Message ' $CallerName = Get-LockCallerName'
39+ $CallerName = Get-LockCallerName
40+ Write-Debug - Message (' $CallerName = '' {0}'' ' -f $CallerName )
41+
42+ Write-Debug - Message (' $AllSourceVMs = Get-VM -ComputerName '' {0}'' ' -f $ComputerName )
43+ $AllSourceVMs = Get-VM - ComputerName $ComputerName
44+ Write-Debug - Message (' $AllSourceVMs: '' {0}'' ' -f [string ]$AllSourceVMs.Name )
45+
46+ Write-Debug - Message ' if ($SourceFilter)'
47+ if ($SourceFilter ) {
48+ Write-Debug - Message (' $SourceVMs = $AllSourceVMs | Where-Object -FilterScript {{{0}}}' -f $SourceFilter )
49+ $SourceVMs = $AllSourceVMs | Where-Object - FilterScript $SourceFilter
50+ }
51+ else {
52+ Write-Debug - Message ' $SourceVMs = $AllSourceVMs'
53+ $SourceVMs = $AllSourceVMs
54+ }
55+ Write-Debug - Message (' $SourceVMs: '' {0}'' ' -f [string ]$SourceVMs.Name )
56+
57+ Write-Debug - Message ' if ($SourceVMs)'
58+ if ($SourceVMs ) {
59+ Write-Debug - Message (' $AllDestinationVMs = Get-VM -ComputerName '' {0}'' ' -f $DestinationVMHostName )
60+ $AllDestinationVMs = Get-VM - ComputerName $DestinationVMHostName
61+ Write-Debug - Message (' $AllDestinationVMs: '' {0}'' ' -f [string ]$AllDestinationVMs.Name )
62+
63+ Write-Debug - Message (' $DestinationFilter = {{{0}}}' -f $DestinationFilter )
64+ Write-Debug - Message ' if ($DestinationFilter)'
65+ if ($DestinationFilter ) {
66+ Write-Debug - Message (' $DestinationVMs = $AllDestinationVMs | Where-Object -FilterScript {{{0}}}' -f $DestinationFilter )
67+ $DestinationVMs = $AllDestinationVMs | Where-Object - FilterScript $DestinationFilter
68+ }
69+ else {
70+ Write-Debug - Message ' $DestinationVMs = $AllDestinationVMs'
71+ $DestinationVMs = $AllDestinationVMs
72+ }
73+ Write-Debug - Message (' $DestinationVMs: '' {0}'' ' -f [string ]$DestinationVMs.Name )
74+
75+ Write-Debug - Message ' if ($DestinationVMs)'
76+ if ($DestinationVMs ) {
77+ $Message = ' Unable to proceed: the destination server {0} should not contain any VMs, but it does ({1})' -f $DestinationVMHostName , [string ]$DestinationVMs.Name
78+ $PSCmdlet.ThrowTerminatingError ((New-Object - TypeName ' System.Management.Automation.ErrorRecord' - ArgumentList ((New-Object - TypeName ' System.InvalidOperationException' - ArgumentList $Message ), ' InvalidOperationException' , [System.Management.Automation.ErrorCategory ]::LimitsExceeded, $null )))
79+ }
80+
81+ Write-Debug - Message (' $DestinationVMHostLock.Value: '' {0}'' ' -f [string ]$DestinationVMHostLock.Value )
82+ Write-Debug - Message ' if (-not ($DestinationVMHostLock.Value))'
83+ if (-not ($DestinationVMHostLock.Value )) {
84+ Write-Debug - Message (' $DestinationVMHostLock.Value = Lock-HostResource -ComputerName {0} -CallerName {1}' -f $DestinationVMHostName , $CallerName )
85+ $DestinationVMHostLock.Value = Lock-HostResource - ComputerName $DestinationVMHostName - CallerName $CallerName
86+ Write-Debug - Message (' $DestinationHostLock: '' {0}'' ' -f $DestinationHostLock )
87+ Write-Debug - Message (' $DestinationHostLock.Value: '' {0}'' ' -f $DestinationHostLock.Value )
88+ }
89+ Write-Debug - Message (' $SourceVMs: '' {0}'' ' -f [string ]$SourceVMs.Name )
90+
91+ Write-Debug - Message (' $DestinationVMHost = Get-VMHost -ComputerName '' {0}'' ' -f $DestinationVMHostName )
92+ $DestinationVMHost = Get-VMHost - ComputerName $DestinationVMHostName
93+ Write-Debug - Message (' $DestinationVMHost: '' {0}'' ' -f $DestinationVMHost )
94+ Write-Debug - Message (' $UnmigratableVMs = Move-VMReliably -DestinationVMHost $DestinationVMHost -Path '' {0}'' -VM $SourceVMs -MaxParallelMigrations {1} -PutInASubfolder:${2}' -f $DestinationVMHostPath , $MaxParallelMigrations , $PutInASubfolder )
95+ $UnmigratableVMs = Move-VMReliably - DestinationVMHost $DestinationVMHost - Path $DestinationVMHostPath - VM $SourceVMs - MaxParallelMigrations $MaxParallelMigrations - PutInASubfolder:$PutInASubfolder
96+ Write-Debug - Message (' $UnmigratableVMs: '' {0}'' ' -f [string ]$UnmigratableVMs.Name )
97+ Write-Debug - Message ' if ($UnmigratableVMs)'
98+ if ($UnmigratableVMs ) {
99+ $Message = ' Unable to proceed: unable to migrate some VMs ({0}) from host {1} to host {2}' -f [string ]$UnmigratableVMs.Name , $ComputerName , $DestinationVMHostName
100+ $PSCmdlet.ThrowTerminatingError ((New-Object - TypeName ' System.Management.Automation.ErrorRecord' - ArgumentList ((New-Object - TypeName ' System.InvalidOperationException' - ArgumentList $Message ), ' InvalidOperationException' , [System.Management.Automation.ErrorCategory ]::ResourceBusy, $null )))
101+ }
102+
103+ Write-Debug - Message ' $true'
104+ $true
105+ }
106+ else {
107+ Write-Debug - Message ' $false'
108+ $false
109+ }
110+
111+ Write-Debug - Message (' EXIT TRY {0}' -f $MyInvocation.MyCommand.Name )
112+ }
113+ catch {
114+ Write-Debug - Message (' ENTER CATCH {0}' -f $MyInvocation.MyCommand.Name )
115+
116+ Write-Debug - Message (' {0}: $PSCmdlet.ThrowTerminatingError($_)' -f $MyInvocation.MyCommand.Name )
117+ $PSCmdlet.ThrowTerminatingError ($_ )
118+
119+ Write-Debug - Message (' EXIT CATCH {0}' -f $MyInvocation.MyCommand.Name )
120+ }
121+
122+ Write-Debug - Message (' EXIT {0}' -f $MyInvocation.MyCommand.Name )
123+ }
0 commit comments