common_utils.ps1

$ZVM_VM_NAME = "ZVML"

Function StartZVM {

    process{
        
        try {
            Write-Host "Starting $($MyInvocation.MyCommand)..."

            $ZVM = Get-VM -Name $ZVM_VM_NAME 

            if ($ZVM.PowerState -eq 'PoweredOff') {
                Write-Host "$ZVM_VM_NAME is powered off, going to power it on"
                Start-VM $ZVM -ErrorAction Stop
            }
            else {
                Write-Host "$ZVM_VM_NAME is up and running"
            }
        }
        catch {
            Write-Error "Failed to start $ZVM_VM_NAME, exception = $_" -ErrorAction Stop
        }
    }
}

Function GenerateRandomPassword {
     #Generate a password with at least 2 uppercase, 4 lowercase, 4 digits & 2 special character (!@#$%^&*())
     
     Write-Host "Starting $($MyInvocation.MyCommand)..."
     
    $upperChars =(65..90)
    $lowerChars    = (97..122)
    $numerics =  (48..57)
    $specialChars = @(33, 35, 36, 37, 38, 40, 41, 42, 45, 64, 94)    
    
    $seedArray = ($upperChars | Get-Random -Count 2)
    $seedArray += ($lowerChars | Get-Random -Count 4)
    $seedArray += ($numerics | Get-Random -Count 4)
    $seedArray += ($specialChars | Get-Random -Count 2)
    
    Foreach ($a in $seedArray){
        $passwordAscii += , [char][byte]$a 
    }
    
    $password = $passwordAscii -join ""
    
    return $password
}

Function CreateZertoFolderOnHost {
    param(
        [Parameter(Mandatory = $true, 
            HelpMessage = "Host Name to connect with SSH")]
        [string]$HostName 
    )
    
    process {
        Write-Host "Starting $($MyInvocation.MyCommand)..."
        
        $Command = "mkdir -p $ZERTO_FOLDER_ON_HOST"
        $Res = RunSSHCommands -HostName $HostName -Commands $Command
        $ExitStatus = $Res["0_exitStatus"];
        
        if ( $ExitStatus -ne '0' ) {
            throw "failed to create $ZERTO_FOLDER_ON_HOST on host $HostName. Exit status for ""$Command"" is $ExitStatus"
        } 
        else {
            Write-Host "Zerto folder ($ZERTO_FOLDER_ON_HOST) was created on $HostName."
        }
    }
}