ZertoAVSModule.psm1

using module Microsoft.AVS.Management


Function TestConnection{
    return "TestConnection"
}

Function runSSHCommands
{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true, HelpMessage = "Host Name")][string]$Hostname,
        [Parameter(Mandatory=$true, HelpMessage = "command to run")][String[]]$Commands
    )
    
    process {
        $Result = @()

        foreach ($Command in $Commands)
        {
            $SSH = Invoke-SSHCommand -SSHSession $SSH_Sessions[$Hostname].Value -Command $Command 
            $Result += New-Object PSObject -Property @{
                Cmd = $Command
                ExitStatus = $SSH.ExitStatus
                Output = $SSH.Output
            }
        }
        
        return $Result
    }
}

Function Get-HostTempFolderInfo {
    param(
        [Parameter(Mandatory=$true, HelpMessage = "Host Name")][string]$Hostname 
    )
    
    process {
        $Command="vdf"
        return runSSHCommands -Hostname $Hostname -Commands $Command
    }
}

Function EnsureConnectivity {    
    param(
        [Parameter(Mandatory=$true, HelpMessage = "Host Name")][string]$Hostname 
    )
    
    process {
        $Command="echo testing123"
        return runSSHCommands -Hostname $Hostname -Commands $Command
    }
}

Function Get-HostEsxiVersion {
    param(
        [Parameter(Mandatory=$true, HelpMessage = "Host Name")][string]$Hostname 
    )
    
    process {
        $Command="vmware -l"
        return runSSHCommands -Hostname $Hostname -Commands $Command
    }
}

Function ChangeRcLocal {
    param(
        [Parameter(Mandatory=$true, HelpMessage = "Host Name")][string]$Hostname,
        [Parameter(Mandatory=$true, HelpMessage = "Data Store Uuid")][string]$DatastoreUuid,
        [Parameter(Mandatory=$true, HelpMessage = "Bios Uuid")]    [string]$BiosUuid
    )

    Process {
        $Commands =    'grep -v ZeRTO /etc/rc.local > /tmp/rc.local',
                    'echo \#ZeRTO\ >> /tmp/rc.local',
                    ('echo sh /vmfs/volumes/{0}/zagentid/{1}/zloadmod.sh load {0} {1} \> /etc/vmware/zloadmod.txt \2\>\&\1 \#ZeRTO\ >> /tmp/rc.local' -f $DatastoreUuid, $BiosUuid),
                    'echo \#ZeRTO\ >> /tmp/rc.local',
                    'cp -f /tmp/rc.local /etc/rc.local'
                    
        return runSSHCommands -Hostname $Hostname -Commands $Commands
    }
}

Function InstallDriverForEsxi67AndAbove {
    param(
        [Parameter(Mandatory=$true, HelpMessage = "Host Name")][string]$Hostname,
        [Parameter(Mandatory=$true, HelpMessage = "Data Store Uuid")]    [string]$DatastoreUuid,
        [Parameter(Mandatory=$true, HelpMessage = "Bios Uuid")]    [string]$BiosUuid,
        [Parameter(Mandatory=$true, HelpMessage = "Name of ZVM driver install script")]    [string]$ZvmDriverInstallScriptFileName,
        [Parameter(Mandatory=$true, HelpMessage = "Name of ZVM driver helper funcs script")]    [string]$ZvmDriverHelperFuncsScriptFileName,
        [Parameter(Mandatory=$true,    HelpMessage = "init or load")]    [string]$isInit,
        [Parameter(Mandatory=$true,    HelpMessage = "Esxi version")]    [string]$EsxiVersion
    )

    Process {
        $FullRemoteFileLocation = ('/vmfs/volumes/{0}/zagentid/{1}' -f $DatastoreUuid, $BiosUuid)
        $RemoteLoadmodFileName = ('{0}/{1}' -f $FullRemoteFileLocation, $ZvmDriverInstallScriptFileName)
        $RemoteLoadmodFileHelperName = ('{0}/{1}' -f $FullRemoteFileLocation, $ZvmDriverHelperFuncsScriptFileName)
        $InstallScriptTempPath = ('/tmp/{0}' -f $ZvmDriverInstallScriptFileName)
        $InstallScriptHelperTempPath = ('/tmp/{0}' -f $ZvmDriverHelperFuncsScriptFileName)
        
        $Commands = ('cp {0} {1}' -f $RemoteLoadmodFileName, $InstallScriptTempPath),
                    ('cp {0} {1}' -f $RemoteLoadmodFileHelperName, $InstallScriptHelperTempPath),
                    ('chmod a+x {0}' -f $InstallScriptTempPath),
                    ('{0} {1} {2} {3} 1 {4} > /etc/vmware/zloadmod.txt' -f $InstallScriptTempPath, $isInit, $DatastoreUuid, $BiosUuid, $EsxiVersion),
                    ('rm {0}' -f $InstallScriptTempPath),
                    ('rm {0}' -f $InstallScriptHelperTempPath)

        return runSSHCommands -Hostname $Hostname -Commands $Commands
        
    }
}

Function DefaultInstallDriver {
    param(
        [Parameter(Mandatory=$true, HelpMessage = "Host Name")][string]$Hostname,
        [Parameter(Mandatory=$true, HelpMessage = "Data Store Uuid")]    [string]$DatastoreUuid,
        [Parameter(Mandatory=$true, HelpMessage = "Bios Uuid")]    [string]$BiosUuid,
        [Parameter(Mandatory=$true, HelpMessage = "Name of ZVM driver install script")]    [string]$ZvmDriverInstallScriptFileName,
        [Parameter(Mandatory=$true, HelpMessage = "init or load")]    [string]$isInit,
        [Parameter(Mandatory=$true,    HelpMessage = "Esxi version")]    [string]$EsxiVersion
    )
    
    process {
        $FullRemoteFileLocation = ('/vmfs/volumes/{0}/zagentid/{1}' -f $DatastoreUuid, $BiosUuid)
        $RemoteLoadmodFileName = ('{0}/{1}' -f $FullRemoteFileLocation, $ZvmDriverInstallScriptFileName)
        $Commands = ('chmod a+x {0}' -f $RemoteLoadmodFileName),
                    ('{0} {1} {2} {3} 1 {4} > /etc/vmware/zloadmod.txt' -f $RemoteLoadmodFileName, $isInit, $DatastoreUuid, $BiosUuid, $EsxiVersion)

        return runSSHCommands -Hostname $Hostname -Commands $Commands
    }
}

Function UninstallDriverForEsxi67AndAbove {
    param(
        [Parameter(Mandatory=$true, HelpMessage = "Host Name")][string]$Hostname,
        [Parameter(Mandatory=$true, HelpMessage = "Data Store Name")]    [string]$DatastoreName,
        [Parameter(Mandatory=$true, HelpMessage = "Bios Uuid")]    [string]$BiosUuid,
        [Parameter(Mandatory=$true, HelpMessage = "ZVM Uninstall script file name")]    [string]$ZvmDriverUninstallSctiptFileName,        
        [Parameter(Mandatory=$true, HelpMessage = "Name of ZVM driver helper funcs script")]    [string]$ZvmDriverHelperFuncsScriptFileName
    )
    
    process {
        $FullRemoteFileLocation = ('/vmfs/volumes/{0}/zagentid/{1}' -f $DatastoreName, $BiosUuid)
        $RemoteUnloadmodFileName = ('{0}/{1}' -f $FullRemoteFileLocation, $ZvmDriverUninstallSctiptFileName)
        $RemoteLoadmodFileHelperName = ('{0}/{1}' -f $FullRemoteFileLocation, $ZvmDriverHelperFuncsScriptFileName)
        $UninstallScriptTempPath = ('/tmp/{0}' -f $ZvmDriverUninstallSctiptFileName)
        $ScriptHelperTempPath = ('/tmp/{0}' -f $ZvmDriverHelperFuncsScriptFileName)
        
        $Commands = ('cp {0} {1}' -f $RemoteUnloadmodFileName, $UninstallScriptTempPath),
                    ('cp {0} {1}' -f $RemoteLoadmodFileHelperName, $scriptHelperTempPath),
                    ('chmod a+x {0}' -f $UninstallScriptTempPath),
                    ('{0} cleanup > /etc/vmware/zunloadmod.txt' -f $UninstallScriptTempPath),
                    ('rm {0}' -f $UninstallScriptTempPath),
                    ('rm {0}' -f $ScriptHelperTempPath)

        return runSSHCommands -Hostname $Hostname -Commands $Commands
    }
}

Function DefaultUninstallDriver {
    param(
        [Parameter(Mandatory=$true, HelpMessage = "Host Name")][string]$Hostname,
        [Parameter(Mandatory=$true, HelpMessage = "Data Store Name")]    [string]$DatastoreName,
        [Parameter(Mandatory=$true, HelpMessage = "Bios Uuid")]    [string]$BiosUuid,
        [Parameter(Mandatory=$true, HelpMessage = "ZVM Uninstall script file name")] [string]$ZvmDriverUninstallSctiptFileName
    )
    
    process {
        $FullRemoteFileLocation = ('/vmfs/volumes/{0}/zagentid/{1}' -f $DatastoreName, $BiosUuid)
        $RemoteUnloadmodFileName = ('{0}/{1}' -f $FullRemoteFileLocation, $ZvmDriverUninstallSctiptFileName)
        $Commands = ('chmod a+x {0}' -f $RemoteUnloadmodFileName),
                    ('{0} cleanup > /etc/vmware/zunloadmod.txt' -f $RemoteUnloadmodFileName)
                    
        return runSSHCommands -Hostname $Hostname -Commands $Commands
    }
}