HostConnector.psm1

using module Microsoft.AVS.Management

$ZvmDriverInstallScriptFileName = "zloadmod.sh"
$ZvmDriverHelperFuncsScriptFileName = "zincl.sh"
$ZvmDriverUninstallSctiptFileName = "zunloadmod.sh"


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

        foreach ($Command in $Commands) {
            Write-Host $Command
            $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
    }
}

<#
.DESCRIPTION
 
This Cmdlet displays information about the available disk space
 
    .PARAMETER HostName
    Host Name to connect with ssh
 
.EXAMPLE
 
Get-HostTempFolderInfo -HostName xxx.xxx.xxx.xxx
#>

Function Get-HostTempFolderInfo {
    [CmdletBinding()]
    [AVSAttribute(5, UpdatesSDDC = $false)]
    param(
        [Parameter(Mandatory = $true, 
            HelpMessage = "Host Name to connect with SSH")]
        [string]$Hostname 
    )
    
    process {
        $Command = "vdf"
        return runSSHCommands -Hostname $Hostname -Commands $Command
    }
}

<#
.DESCRIPTION
 
This Cmdlet checks if the host is up and running
 
    .PARAMETER HostName
    Host Name to connect with ssh
 
.EXAMPLE
 
EnsureConnectivity -HostName xxx.xxx.xxx.xxx
#>

Function EnsureConnectivity {    
    [CmdletBinding()]
    [AVSAttribute(5, UpdatesSDDC = $false)]
    param(
        [Parameter(Mandatory = $true, 
            HelpMessage = "Host Name to connect with SSH")]
        [string]$Hostname 
    )
    
    process {
        $Command = "echo testing123"
        return runSSHCommands -Hostname $Hostname -Commands $Command
    }
}

<#
.DESCRIPTION
 
This Cmdlet retrieves the ESXi version
 
    .PARAMETER HostName
    Host Name to connect with ssh
 
.EXAMPLE
 
Get-HostEsxiVersion -HostName xxx.xxx.xxx.xxx
#>

Function Get-HostEsxiVersion {
    [CmdletBinding()]
    [AVSAttribute(5, UpdatesSDDC = $false)]
    param(
        [Parameter(Mandatory = $true, HelpMessage = "Host Name to connect with SSH")][string]$Hostname 
    )
    
    process {
        $Command = "vmware -l"
        return runSSHCommands -Hostname $Hostname -Commands $Command
    }
}

<#
.DESCRIPTION
 
This Cmdlet is responsible for loading the driver when the host is booting.
Rc.local file is executed after all the normal system services are started
 
    .PARAMETER HostName
    Host Name to connect with ssh
 
    .PARAMETER DatastoreUuid
    Datastore Uuid
 
    .PARAMETER BiosUuid
    "Host Bios Uuid || mob-> Property Path: host.hardware.systemInfo.uuid"
 
.EXAMPLE
 
ChangeRcLocal -HostName xxx.xxx.xxx.xxx -DatastoreUuid xxx -BiosUuid xxx
#>

Function ChangeRcLocal {
    [CmdletBinding()]
    [AVSAttribute(10, UpdatesSDDC = $true)]
    param(
        [Parameter(Mandatory = $true, 
            HelpMessage = "Host Name to connect with SSH")]
        [string]$Hostname,
        [Parameter(Mandatory = $true, 
            HelpMessage = "Datastore Uuid")]
        [string]$DatastoreUuid,
        [Parameter(Mandatory = $true, 
            HelpMessage = "Host Bios Uuid || mob-> Property Path: host.hardware.systemInfo.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
    }
}

<#
.DESCRIPTION
 
This Cmdlet installs the driver for ESXi 6.7 and above
 
    .PARAMETER HostName
    Host Name to connect with SSH
     
    .PARAMETER DatastoreUuid
    Datastore Uuid
 
    .PARAMETER BiosUuid
    Host Bios Uuid || mob-> Property Path: host.hardware.systemInfo.uuid
 
    .PARAMETER ZvmDriverInstallScriptFileName
    Install driver script (zloadmod.sh - tweakable)
 
    .PARAMETER ZvmDriverHelperFuncsScriptFileName
    Helper functions script - zincl.sh
 
    .PARAMETER IsInit
    Init or load the driver
 
    .PARAMETER EsxiVersion
    Esxi version
 
.EXAMPLE
InstallDriverForEsxi67AndAbove -HostName xxx.xxx.xxx.xxx -DatastoreUuid <UUID> -BiosUuid <UUID> -ZvmDriverInstallScriptFileName <default is zloadmod.sh> -ZvmDriverHelperFuncsScriptFileName <default is zincl.sh> -IsInit <init/load> -EsxiVersion xx
#>

Function InstallDriverForEsxi67AndAbove {
    [CmdletBinding()]
    [AVSAttribute(10, UpdatesSDDC = $true)]
    param(
        [Parameter(Mandatory = $true, 
            HelpMessage = "Host Name to connect with SSH")]
        [string]$Hostname,
        [Parameter(Mandatory = $true, 
            HelpMessage = "Datastore Uuid")]
        [string]$DatastoreUuid,
        [Parameter(Mandatory = $true, 
            HelpMessage = "Host Bios Uuid || mob-> Property Path: host.hardware.systemInfo.uuid")]    
        [string]$BiosUuid,
        [Parameter(Mandatory = $true, 
            HelpMessage = "Init or load the driver")]    
        [string]$IsInit,
        [Parameter(Mandatory = $true, 
            HelpMessage = "Esxi version")]
        [string]$EsxiVersion
    )

    Process {

        $RemoteLoadmodFileName = Join-Path $psScriptRoot $ZvmDriverInstallScriptFileName
        $RemoteLoadmodFileHelperName = Join-Path $psScriptRoot $ZvmDriverHelperFuncsScriptFileName
        
        $InstallScriptTempPath = ('/tmp/{0}' -f $ZvmDriverInstallScriptFileName)
        $InstallScriptHelperTempPath = ('/tmp/{0}' -f $ZvmDriverHelperFuncsScriptFileName)


        $hostName="192.168.132.2"
        $userName="root"
        $password="Zertodata1!"
        $secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
        $credentials = New-Object System.Management.Automation.PSCredential($userName, $secpasswd)
        $sftp= New-SFTPSession -ComputerName $hostName -Credential $credentials


        Set-SFTPItem -SessionId ($STFP_Sessions[$Hostname]).Value -Destination $DestFolder -Path $RemoteLoadmodFileName -Force
        Set-SFTPItem -SessionId ($sftp).SessionId -Destination $DestFolder -Path $RemoteLoadmodFileHelperName -Force


        $Commands = ('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
        
    }
}

<#
.DESCRIPTION
 
This Cmdlet installs the driver for ESXi 6.5 and lower
 
    .PARAMETER HostName
    Host Name to connect with SSH
     
    .PARAMETER DatastoreUuid
    Datastore Uuid
 
    .PARAMETER BiosUuid
    Host Bios Uuid || mob-> Property Path: host.hardware.systemInfo.uuid
 
    .PARAMETER ZvmDriverInstallScriptFileName
    Install driver script (zloadmod.sh - tweakable)
 
    .PARAMETER IsInit
    Init or load the driver
 
    .PARAMETER EsxiVersion
    Esxi version
 
.EXAMPLE
DefaultInstallDriver -HostName xxx.xxx.xxx.xxx -DatastoreUuid <UUID> -BiosUuid <UUID> -ZvmDriverInstallScriptFileName <default is zloadmod.sh> -IsInit <init/load> -EsxiVersion xx
#>

Function DefaultInstallDriver {
    [CmdletBinding()]
    [AVSAttribute(10, UpdatesSDDC = $true)]
    param(
        [Parameter(Mandatory = $true, 
            HelpMessage = "Host Name to connect with SSH")]
        [string]$Hostname,
        [Parameter(Mandatory = $true, 
            HelpMessage = "Datastore Uuid")]    
        [string]$DatastoreUuid,
        [Parameter(Mandatory = $true,
         HelpMessage = "Host Bios Uuid || mob-> Property Path: host.hardware.systemInfo.uuid")]    
        [string]$BiosUuid,
        [Parameter(Mandatory = $true,
         HelpMessage = "Install driver script (zloadmod.sh - tweakable)")]
        [string]$ZvmDriverInstallScriptFileName,
        [Parameter(Mandatory = $true,
         HelpMessage = "Init or load the driver")]    
        [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
    }
}

<#
.DESCRIPTION
 
This Cmdlet used to uninstall the driver for ESXi 6.7 and above
 
    .PARAMETER HostName
    Host Name to connect with SSH
     
    .PARAMETER DatastoreUuid
    Datastore Uuid
 
    .PARAMETER BiosUuid
    Host Bios Uuid || mob-> Property Path: host.hardware.systemInfo.uuid
 
    .PARAMETER ZvmDriverUninstallSctiptFileName
    Uninstall driver script (zunloadmod.sh - tweakable)
 
    .PARAMETER ZvmDriverHelperFuncsScriptFileName
    /***/
 
    .PARAMETER IsInit
    Init or load the driver
 
    .PARAMETER EsxiVersion
    Esxi version
 
.EXAMPLE
UninstallDriverForEsxi67AndAbove -HostName xxx.xxx.xxx.xxx -DatastoreUuid <UUID> -BiosUuid <UUID> ZvmDriverUninstallSctiptFileName <default is zunloadmod.sh> -IsInit <init/load> -EsxiVersion xx
#>

Function UninstallDriverForEsxi67AndAbove {
    [CmdletBinding()]
    [AVSAttribute(10, UpdatesSDDC = $true)]
    param(
        [Parameter(Mandatory = $true,
         HelpMessage = "Host Name to connect with SSH")]
        [string]$Hostname,
        [Parameter(Mandatory = $true,
         HelpMessage = "Datastore Name")]    
        [string]$DatastoreName,
        [Parameter(Mandatory = $true,
         HelpMessage = "Host Bios Uuid || mob-> Property Path: host.hardware.systemInfo.uuid")]    
        [string]$BiosUuid,
        [Parameter(Mandatory = $true, 
            HelpMessage = "Uninstall driver script (zunloadmod.sh - tweakable)")]
        [string]$ZvmDriverUninstallSctiptFileName,        
        [Parameter(Mandatory = $true, 
            HelpMessage = "Helper functions script - zincl.sh")]
        [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
    }
}

<#
.DESCRIPTION
 
This Cmdlet used to uninstall the driver for ESXi 6.5 and lower
 
    .PARAMETER HostName
    Host Name to connect with SSH
     
    .PARAMETER DatastoreUuid
    Datastore Uuid
 
    .PARAMETER BiosUuid
    Host Bios Uuid || mob-> Property Path: host.hardware.systemInfo.uuid
 
    .PARAMETER ZvmDriverUninstallSctiptFileName
    Uninstall driver script (zunloadmod.sh - tweakable)
 
    .PARAMETER IsInit
    Init or load the driver
 
    .PARAMETER EsxiVersion
    Esxi version
 
.EXAMPLE
DefaultUninstallDriver -HostName xxx.xxx.xxx.xxx -DatastoreUuid <UUID> -BiosUuid <UUID> ZvmDriverUninstallSctiptFileName <default is zunloadmod.sh> -IsInit <init/load> -EsxiVersion xx
#>

Function DefaultUninstallDriver {
    [CmdletBinding()]
    [AVSAttribute(10, UpdatesSDDC = $true)]
    param(
        [Parameter(Mandatory = $true, 
            HelpMessage = "Host Name to connect with SSH")]
        [string]$Hostname,
        [Parameter(Mandatory = $true, 
            HelpMessage = "Datastore Name")]    
        [string]$DatastoreName,
        [Parameter(Mandatory = $true,
         HelpMessage = "Host Bios Uuid || mob-> Property Path: host.hardware.systemInfo.uuid")]
        [string]$BiosUuid,
        [Parameter(Mandatory = $true, 
            HelpMessage = "Uninstall driver script ($ZvmDriverUninstallSctiptFileName - tweakable)")]
        [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
    }
}