validations_utils.ps1
$PUBLIC_KEY = ('{0}/ZertoPublicKey.pem' -f $psScriptRoot) Function Validate-FileBySignature { param ( [Parameter(Mandatory=$true, HelpMessage = "File to verify")] [string]$FilePath, [Parameter(Mandatory=$true, HelpMessage = "Signature file to verify")] [string]$SignatureFilePath ) Process { Write-Host "Verifying signature for $FilePath" $isVerified = (openssl dgst -sha256 -verify $PUBLIC_KEY -signature $SignatureFilePath $FilePath 2>&1) -join ";" if ($isVerified -eq "Verified OK") { Write-Host "File signature was verified successfully for $FilePath by $SignatureFilePath" return $true } else { Write-Error "Could not verify $FilePath signature by $SignatureFilePath" return $false } } } Function Validate-NetworkSettings { param ( [Parameter(Mandatory=$true, HelpMessage = "ZVML ip address")] [string] $ZvmlIp, [Parameter(Mandatory = $true, HelpMessage = "SubnetMask address.")] [string] $SubnetMask, [Parameter(Mandatory = $true, HelpMessage = "Default gateway.")] [string] $DefaultGateway, [Parameter(Mandatory = $true, HelpMessage = "DNS server address.")] [string] $DNS ) Process { Write-Host "Validating ZVML IP $ZvmlIp" $IpSettingsValidated = Validate-IpAddress $ZvmlIp Write-Host "Validating SubnetMask IP $SubnetMask" $IpSettingsValidated = (Validate-IpAddress $SubnetMask) -and $IpSettingsValidated Write-Host "Validating DNS IP $DNS" $IpSettingsValidated = (Validate-IpAddress $DNS) -and $IpSettingsValidated Write-Host "Validating DefaultGateway IP $DefaultGateway" $IpSettingsValidated = (Validate-IpAddress $DefaultGateway) -and $IpSettingsValidated if ($IpSettingsValidated -eq $false) { $Message = "Ip settings validations failed" Write-Error $Message throw $Message } } } Function Validate-IpAddress { param ( [Parameter(Mandatory=$true, HelpMessage = "ip address")] [string]$ip ) Process { Write-Host "Starting $($MyInvocation.MyCommand)..." try { if (([ipaddress]$ip).IPAddressToString -eq ("" + $ip)){ Write-Host "IP address $ip is valid" return $true } else { $Message = "Zerto Virtual Manager (ZVM) deployment failed. Reasons for failure include: ZVM could not be configured with the specified static IP address $ip, due to wrong format." Write-Error $Message return $false } } catch { $Message = "Zerto Virtual Manager (ZVM) deployment failed. Reasons for failure include: ZVM could not be configured with the specified static IP address $ip, due to wrong format." Write-Error $Message return $false } } } Function Validate-VcEnvParams { param( [Parameter(Mandatory = $true, HelpMessage = "Datastore Name")] [string]$DatastoreName, [Parameter(Mandatory = $true, HelpMessage = "ZVM IP")] [string]$ZVMLIp, [Parameter(Mandatory = $true, HelpMessage = "Subnet Mask")] [string]$SubnetMask, [Parameter(Mandatory = $true, HelpMessage = "Default Gateway")] [string]$DefaultGateway, [Parameter(Mandatory = $true, HelpMessage = "DNS Address")] [string]$DNS ) process { Write-Host "Starting $($MyInvocation.MyCommand)..." if((Test-VmExists -VmName $ZVM_VM_NAME) -eq $true){ Write-Host "$ZVM_VM_NAME already exists, trying to power it on" Start-ZVM Write-Error "$ZVM_VM_NAME already exists and powered on, exit" -ErrorAction Stop } if((Validate-DatastoreName -DatastoreName $DatastoreName) -ne $true) { throw "Datastore=$DatastoreName does not exists. Validation failed" } Validate-NetworkSettings -ZvmlIp $ZVMLIp -SubnetMask $SubnetMask -DefaultGateway $DefaultGateway -DNS $DNS Write-Host "Validations Finished" } } Function Get-ValidatedHostName { param( [Parameter(Mandatory = $false, HelpMessage = "Host Name")] [string]$HostName ) process{ Write-Host "Starting $($MyInvocation.MyCommand)..." $AllValidHostNames = (Get-VMHost -ErrorAction SilentlyContinue | where {$_.ConnectionState -eq "Connected" -and $_.PowerState -eq "PoweredOn"}).Name if ($HostName){ Write-Host "Host provided by user: $HostName" $ValidHostName = $AllValidHostNames | where {$_ -eq $HostName} | select -First 1 } else { Write-Host "Host was not provided by user, an alternate valid host will be selected" $ValidHostName = $AllValidHostNames | select -First 1 } if($ValidHostName){ Write-Host "Host=$ValidHostName exists. Validation passed succesfully" return $ValidHostName } else{ $Message = "Zerto Virtual Manager (ZVM) deployment failed. Reasons for failure include: Could not find a valid host for ZVMA deployment." Write-Host $Message Write-Error "No valid host was found. Validation failed" -ErrorAction Stop } } } Function Test-VmExists { param( [Parameter(Mandatory = $true, HelpMessage = "VM Name")] [string]$VmName ) process{ Write-Host "Starting $($MyInvocation.MyCommand)..." $VM = Get-VM -Name $VmName -ErrorAction SilentlyContinue | Select-Object -first 1 if($VM -eq $null) { Write-Host "VM=$VmName does not exist" return $false } Write-Host "VM=$VmName exists" return $true } } Function Validate-BiosUUID { param( [Parameter(Mandatory = $true, HelpMessage = "Datastore Name")] [string]$DatastoreName, [Parameter(Mandatory = $true, HelpMessage = "Host Bios Uuid || mob-> Property Path: host.hardware.systemInfo.uuid")] [string]$BiosUuid ) process{ Write-Host "Starting $($MyInvocation.MyCommand)..." $Datastore = Get-Datastore -Name $DatastoreName | Select-Object -first 1 New-PSDrive -Name TgtDS -Location $Datastore -PSProvider VimDatastore -Root '\' | Out-Null $DatastoreFolders = Get-ChildItem -Path TgtDS: -Recurse Remove-PSDrive -Name TgtDS foreach($DatastoreFolder in $DatastoreFolders) { if($DatastoreFolder.Name -eq $BiosUuid) { Write-Host "BiosUuid=$BiosUuid exists. Validation passed succesfully" return $true } } Write-Error "BiosUuid=$BiosUuid does not exists. Validation failed" return $false } } Function Validate-DigitsOnly { param( [Parameter(Mandatory = $true, HelpMessage = "Input string to validate all the characters are numeric")] [string]$InputString ) process{ Write-Host "Starting $($MyInvocation.MyCommand)..." if($InputString -match "^\d+$") { Write-Host "InputString = $InputString contains only digits" return $true } Write-Error "Validation failed. InputString = $InputString doesn't contain only digits" return $false } } Function Validate-DatastoreName { param( [Parameter(Mandatory = $true, HelpMessage = "Datastore Name")] [string]$DatastoreName ) process{ Write-Host "Starting $($MyInvocation.MyCommand)..." $Datastore = Get-Datastore -Name $DatastoreName | Select-Object -first 1 if($Datastore -eq $null) { Write-Error "Datastore=$DatastoreName does not exists. Validation failed" return $false } Write-Host "Datastore=$DatastoreName exists. Validation pass" return $true } } |