Common/Common.psm1
using module './Result' Import-Module -Name @(Join-Path $PSScriptRoot Wrappers | Join-Path -ChildPath Wrappers) Import-Module -Name @(Join-Path $PSScriptRoot .. | Join-Path -ChildPath Util | Join-Path -ChildPath Util) Import-Module -Name @(Join-Path $PSScriptRoot .. | Join-Path -ChildPath Util | Join-Path -ChildPath MigrationUtil) Import-Module -Name @(Join-Path $PSScriptRoot .. | Join-Path -ChildPath RiverMeadow.Source | Join-Path -ChildPath SourceUtil | Join-Path -ChildPath SourceUtil) function Confirm-RMCommonParameter { param ( [hashtable] $UserParameter ) $Errors = @() $Warnings = @() $IsSourceAndTargetCloudPresent = $true if (!$UserParameter.ContainsKey("TargetCloud") -or [string]::IsNullOrEmpty($UserParameter["TargetCloud"])) { $Errors += "TargetCloud is required" $IsSourceAndTargetCloudPresent = $false } if (!$UserParameter.ContainsKey("SourceIP") -or [string]::IsNullOrEmpty($UserParameter["SourceIP"])) { $Errors += "SourceIP is required" $IsSourceAndTargetCloudPresent = $false } else { $IsValidIPAddress = Confirm-RMIPAddress -IPAddress $UserParameter.SourceIP if (!$IsValidIPAddress) { $Errors += "Invalid Source IP address." $IsSourceAndTargetCloudPresent = $false } } if (!$UserParameter.ContainsKey("TargetVMName") -or [string]::IsNullOrEmpty($UserParameter["TargetVMName"])) { $Warnings += "TargetVMName has not been provided, source hostname will be used as the target VM name" } $Errors += Confirm-RMSchedule -UserParameter $UserParameter return $Errors, $Warnings, $IsSourceAndTargetCloudPresent } function Confirm-RMVMBasedCommonParameter { param ( [hashtable] $UserParameter ) $Errors = @() $Warnings = @() $IsSourcePresent = $true if (!$UserParameter.ContainsKey("SourceVMName") -or [string]::IsNullOrEmpty($UserParameter["SourceVMName"])) { $Errors += "SourceVMName is required" $IsSourcePresent = $false } elseif (!$UserParameter.ContainsKey("TargetVMName") -or [string]::IsNullOrEmpty($UserParameter["TargetVMName"])) { $Warnings += "TargetVMName has not been provided, source hostname will be used as the target VM name" } if (!$UserParameter.ContainsKey("TargetCloud") -or [string]::IsNullOrEmpty($UserParameter["TargetCloud"])) { $Errors += "TargetCloud is required" $IsSourcePresent = $false } $Errors += Confirm-RMSchedule -UserParameter $UserParameter return $Errors, $Warnings, $IsSourcePresent } function Confirm-RMVMBasedDMCommonParameter { param( [hashtable] $UserParameter ) $Errors = @() $IsValidMigrationId = $true if (!$UserParameter.ContainsKey("MigrationId") -or [string]::IsNullOrWhiteSpace($UserParameter["MigrationId"])) { $Errors += "MigrationId is required" $IsValidMigrationId = $false } if ($UserParameter.ContainsKey("RunContinuous") ` -and $UserParameter.ContainsKey("ScheduledAt") ` -and ![string]::IsNullOrWhiteSpace($UserParameter["ScheduledAt"])) { $Errors += "Parameters 'RunContinuous' and 'ScheduledAt' cannot be used at the same time." } $Errors += Confirm-RMSchedule -UserParameter $UserParameter return $Errors, $IsValidMigrationId } function Confirm-RMSchedule { param( [hashtable] $UserParameter ) $Errors = @() if ($UserParameter.ContainsKey("ScheduledAt") -or ![string]::IsNullOrEmpty($UserParameter["ScheduledAt"])) { $IsValidDateTime = Confirm-RMDateFormat -InputDate $UserParameter["ScheduledAt"] -DateFormat "MM/dd/yyyy HH:mm" if ($IsValidDateTime) { if (!(Confirm-RMAfterCurrentDateTime -InputDateTime $UserParameter["ScheduledAt"])) { $Errors += "ScheduledAt date and time should be greater than the current date and time" } } else { $Errors += "ScheduledAt should have the date and time in the format 'MM/dd/yyyy HH:mm' with valid values for date and time" } } return $Errors } function Confirm-RMCommonParameterWithSource { param ( [hashtable] $UserParameter, [System.Object] $Source, [System.Object] $SourceSQLServerMapping, [System.Array] $MigrationExtension, [System.Array] $MigrationExtensionOSM, [string] $CloudType, [hashtable] $WindowsLicense ) $Errors = @() if ($UserParameter.ContainsKey("MountPoint") -and $UserParameter["MountPoint"].Count -gt 0) { $SourceMountPoints = Get-MountPoint -Source $Source $Errors += Compare-RMMountPoint -Source $Source -SourceMountPoints $SourceMountPoints.Values ` -UserInputMountPoints $UserParameter["MountPoint"] -ParameterName "MountPoint" $NonExistsMountPoints = Test-RMNonExistentMountPoints -SourceMountPoints $SourceMountPoints.Values -UserInputMountPoints $UserParameter["MountPoint"] if ($NonExistsMountPoints.Count -gt 0) { $NonExistsMountPointsAsString = $NonExistsMountPoints -join ", " $Errors += "'$NonExistsMountPointsAsString' does not exist on the source." } } if ($UserParameter.ContainsKey("UpgradeOSVersion") -and ![string]::IsNullOrEmpty($UserParameter["UpgradeOSVersion"])) { $UpgradeOSVersion = $UserParameter["UpgradeOSVersion"] $SourceOSMMapping = Get-RMOSMMappingBySource -Source $Source if ($SourceOSMMapping.Keys.Count -eq 0) { $Errors += "No OS upgrade options are available for the given source OS" } else { $OSMLabels = $SourceOSMMapping.keys -join ", " $UpgradeOSVersion = $UpgradeOSVersion.Trim('"') $UpgradeOSVersion = $UpgradeOSVersion.Trim("'") if ($SourceOSMMapping.keys -notcontains $UpgradeOSVersion) { $Errors += "UpgradeOSVersion should be one of '$OSMLabels'" } } } if ((!$UserParameter.ContainsKey("UpgradeOSVersion") -and [string]::IsNullOrEmpty($UserParameter["UpgradeOSVersion"]))) { if ($UserParameter.ContainsKey("ConvertFileSystem") -and ![string]::IsNullOrEmpty($UserParameter["ConvertFileSystem"])) { $Errors += "'UpgradeOSVersion' is required, when parameter 'ConvertFileSystem' is not null." } } elseif ($UserParameter.ContainsKey("ConvertFileSystem") -and ![string]::IsNullOrEmpty($UserParameter["ConvertFileSystem"])) { $ConvertFileSystem = $UserParameter["ConvertFileSystem"] if ("xfs" -ne $ConvertFileSystem) { $Errors += "ConvertFileSystem '$ConvertFileSystem' does not exist." } } if ("windows" -ieq $Source.os_type) { if ($UserParameter.ContainsKey("ReplaceWindowsLicense") -and ![string]::IsNullOrWhiteSpace($UserParameter["ReplaceWindowsLicense"])) { $ReplaceWindowsLicense = $UserParameter["ReplaceWindowsLicense"] if ($UserParameter.ContainsKey("UpgradeOSVersion") -and ![string]::IsNullOrEmpty($UserParameter["UpgradeOSVersion"])) { if ($UserParameter.ContainsKey("UpgradeOSVersion") -and ![string]::IsNullOrEmpty($UserParameter["UpgradeOSVersion"]) -and ` $UserParameter["UpgradeOSVersion"] -ne $UserParameter["ReplaceWindowsLicense"]) { $Errors += "The selected licence key version ($ReplaceWindowsLicense) does not match the 'UpgradeOSVersion' version." } $License = $WindowsLicense[$ReplaceWindowsLicense] if ($null -ieq $License) { $Errors += "ReplaceWindowsLicense '$ReplaceWindowsLicense' does not exist." } } else { $Errors += "'UpgradeOSVersion' is required, when 'ReplaceWindowsLicense' is not null." } } } elseif ($UserParameter.ContainsKey("ReplaceWindowsLicense") -and ![string]::IsNullOrWhiteSpace($UserParameter["ReplaceWindowsLicense"])) { $Errors += "'ReplaceWindowsLicense' can be not null only for a Windows source" } if ($UserParameter.ContainsKey("UpgradeSQLServer") -and ![string]::IsNullOrWhiteSpace($UserParameter["UpgradeSQLServer"])) { if ($SourceSQLServerMapping.Count -eq 0) { $Errors += "No SQL upgrade options are available for the given source OS" } $UpgradeSQLServer = $UserParameter["UpgradeSQLServer"] $UpgradeSQLServer.InstanceName | Group-Object | ForEach-Object { if ($_.Count -gt 1) { $Errors += "In the 'UpgradeSQLServer' object, 'InstanceName' should be unique." }} if ($UpgradeSQLServer.InstanceName.Count -gt $UpgradeSQLServer.UpgradeVersion.Count) { $Errors += "In the 'UpgradeSQLServer' object 'UpgradeVersion' is required for each SQL instance." } elseif ($UpgradeSQLServer.InstanceName.Count -ne $UpgradeSQLServer.UpgradeVersion.Count) { $Errors += "In the 'UpgradeSQLServer' object 'InstanceName' is required for each 'UpgradeVersion'." } foreach($Upgrade in $UpgradeSQLServer) { if ($SourceSQLServerMapping.instance_name -notcontains $Upgrade.InstanceName) { $InstanceName = $Upgrade.InstanceName $Errors += "Instance '$InstanceName' does not exist." } else { foreach($SQLServerObject in $SourceSQLServerMapping) { if ($Upgrade.InstanceName -ieq $SQLServerObject.instance_name -and $SQLServerObject.upgrade_options -notcontains $Upgrade.UpgradeVersion) { $UpgradeVersion = $Upgrade.UpgradeVersion $Errors += "Upgrade Version '$UpgradeVersion' does not exist." } } } } } if ($UserParameter.ContainsKey("MigrationExtension") -and ![string]::IsNullOrWhiteSpace($UserParameter["MigrationExtension"])) { $MigrationExtensionValue = $UserParameter["MigrationExtension"] if ($MigrationExtension -notcontains $MigrationExtensionValue) { $Errors += "MigrationExtension '$MigrationExtensionValue' does not exist." } } if ($UserParameter.ContainsKey("MigrationExtensionOSM") -and ![string]::IsNullOrWhiteSpace($UserParameter["MigrationExtensionOSM"])) { $MigrationExtensionOSMValue = $UserParameter["MigrationExtensionOSM"] if ((!$UserParameter.ContainsKey("UpgradeOSVersion") -or [string]::IsNullOrWhiteSpace($UserParameter["UpgradeOSVersion"]))) { $Errors += "'UpgradeOSVersion' is required, when parameter 'MigrationExtensionOSM' is not null." } elseif ($MigrationExtensionOSM -notcontains $MigrationExtensionOSMValue) { $Errors += "MigrationExtensionOSM '$MigrationExtensionOSMValue' does not exist." } } if ("linux" -ieq $Source.os_type -and $UserParameter.ContainsKey("Sysprep") ` -and $UserParameter["Sysprep"]) { $Errors += "'Sysprep' can be 'true' only for a Windows source." } if ("rivermeadow_standalone" -ieq $CloudType -or "gcp" -ieq $CloudType) { if ($UserParameter.ContainsKey("KMSItopia") -and $UserParameter["KMSItopia"]) { if ("windows" -ne $Source.os_type) { $Errors += "'KMSItopia' can be 'true' only for a Windows source" } if ($UserParameter.ContainsKey("KMSActivation") -and $UserParameter["KMSActivation"]) { $Errors += "'KMSItopia' and 'KMSActivation' cannot be 'true' simultaneously." } } } return $Errors } function Out-RMUserParameterResult { param ( [string[]] $ErrorMessage, [string[]] $WarningMessage ) foreach ($Warning in $WarningMessage) { Write-Warning -Message $Warning } if ($ErrorMessage.Count -eq 0) { return } foreach ($Error in $ErrorMessage) { Write-RMError -Message $Error } } function Update-RMMigrationReturnAsSuccess { param( [System.Object] $MigrationResponse, [bool] $IsScheduledMigration, [string] $ReturnMessage, [RMMigrationReturn] $RMMigrationReturn ) if ($IsScheduledMigration) { Write-Output "Migration scheduled successfully" | Out-Host $RMMigrationReturn.SetReturnCode([RMReturn]::SUCCESS) return $RMMigrationReturn } $MigrationId = Get-RMMigrationIdFromResponse -Response $MigrationResponse Write-Output "$ReturnMessage : $MigrationId" | Out-Host $RMMigrationReturn.SetMigrationId($MigrationId) $RMMigrationReturn.SetReturnCode([RMReturn]::SUCCESS) return $RMMigrationReturn } function Update-RMMigrationReturnAsError { param( [string] $Message, [RMMigrationReturn] $RMMigrationReturn ) Write-RMError -Message $Message $RMReturn = [RMReturn]::new([RMReturn]::ERROR, [RMError]::new($Message), $null, $null) $RMMigrationReturn.SetRMReturn($RMReturn) return $RMMigrationReturn } |