HostConnectorModule.psm1

# Todo: delete CreateSession

Function CreateSession {
    Write-Host "Create Session was called"
    $hostName="192.168.132.1"
    $userName="root"
    $password="Zertodata1!"

    $secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
    $credentials = New-Object System.Management.Automation.PSCredential($userName, $secpasswd)

    $SSH = New-SSHSession -ComputerName $hostName -Credential $credentials #-AcceptKey 1
    return $SSH
} 

Function Test{
    Write-Host "Test New Module !!!! "
}

Function GetHostTempFolderInfo {
    $SSH=CreateSession
    $Command="vdf"
    $SSHOut = $(Invoke-SSHCommand -SSHSession $SSH -Command $Command).Output
    return $SSHOut
}

Function EnsurePrivateKeyConnectivity {
    $SSH=CreateSession
    $Command="echo testing123"
    $SSHOut = $(
    Invoke-SSHCommand -SSHSession $SSH -Command $Command).Output
    return $SSHOut
}

Function GetEsxiVersionWithSsh {
    $SSH=CreateSession
    $Command="vmware -l"
    $SSHOut = $(Invoke-SSHCommand -SSHSession $SSH -Command $Command).Output
    return $SSHhOut    
}

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

    Process {    
        $cmd = 'echo sh /vmfs/volumes/' + $DatastoreUuid + '/zagentid/' + $BiosUuid + '/zloadmod.sh load ' + $DatastoreUuid +' '+ $BiosUuid + ' > /etc/vmware/zloadmod.txt 2>&1 #ZeRTO\ >> /tmp/rc.local'
        $Commands =    'grep -v ZeRTO /etc/rc.local > /tmp/rc.local',
                    'echo #ZeRTO\ >> /tmp/rc.local',
                    $cmd,
                    'echo #ZeRTO\ >> /tmp/rc.local',
                    'cp -f /tmp/rc.local /etc/rc.local'
        Write-Host "Command: $Commands"
        #$SSHOut = $(Invoke-SSHCommand -SSHSession $SSH -Command ($Commands -join ';')).Output
        #Todo: output needed?
    }
}

Function InstallDriverCommandsForEsxi67AndAbove {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true, HelpMessage = "Data Store Uuid")]    [string]$DatastoreUuid,
        [Parameter(Mandatory=$true, HelpMessage = "Bios Uuid")]    [string]$BiosUuid,
        [Parameter(Mandatory=$true, HelpMessage = "destination directory")]    [string]$DestDir,
        [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]$InitSpec,
        [Parameter(Mandatory=$true,    HelpMessage = "Esxi version")]    [string]$EsxiVersion
    )

    Process {
        $FullRemoteFileLocation = '/vmfs/volumes/' + $DatastoreUuid + '/' + $DestDir
        $RemoteLoadmodFileName = $FullRemoteFileLocation + '/' + $ZvmDriverInstallScriptFileName
        $RemoteLoadmodFileHelperName = $FullRemoteFileLocation + '/' + $ZvmDriverHelperFuncsScriptFileName
        $InstallScriptTempPath = '/tmp/' + $ZvmDriverInstallScriptFileName
        $InstallScriptHelperTempPath = '/tmp/' + $ZvmDriverHelperFuncsScriptFileName
        
        $Commands = 'cp ' + $RemoteLoadmodFileName + ' ' + $InstallScriptTempPath,
                    'cp ' + $RemoteLoadmodFileHelperName + ' ' + $InstallScriptHelperTempPath,
                    'chmod a+x ' + $InstallScriptTempPath,
                    $InstallScriptTempPath + ' ' + $InitSpec + ' ' + $DatastoreUuid + ' ' + $BiosUuid + ' 1 ' + $EsxiVersion + ' > /etc/vmware/zloadmod.txt',
                    'rm ' + $InstallScriptTempPath,
                    'rm ' +$InstallScriptHelperTempPath
                    
        Write-Host "Command: $Commands"
        #$SSHOut = $(Invoke-SSHCommand -SSHSession $SSH -Command ($Commands -join ';')).Output
      
    }
}

Function DefaultInstallDriverCommands{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true, HelpMessage = "Data Store Uuid")]    [string]$DatastoreUuid,
        [Parameter(Mandatory=$true, HelpMessage = "Bios Uuid")]    [string]$BiosUuid,
        [Parameter(Mandatory=$true, HelpMessage = "Destination directory")]    [string]$DestDir,
        [Parameter(Mandatory=$true, HelpMessage = "Name of ZVM driver install script")]    [string]$ZvmDriverInstallScriptFileName,
        [Parameter(Mandatory=$true, HelpMessage = "init or load")]    [string]$InitSpec,
        [Parameter(Mandatory=$true,    HelpMessage = "Esxi version")]    [string]$EsxiVersion
    )
    
    process {
        $FullRemoteFileLocation = '/vmfs/volumes/' + $DatastoreUuid + '/' + $DestDir
        $RemoteLoadmodFileName = $FullRemoteFileLocation + '/' + $ZvmDriverInstallScriptFileName
        $Commands = 'chmod a+x ' + $RemoteLoadmodFileName,
                    $RemoteLoadmodFileName + ' ' + $InitSpec + ' ' + $DatastoreUuid + ' ' + $BiosUuid + ' 1' + $EsxiVersion + ' > /etc/vmware/zloadmod.txt'

        Write-Host "Command: $Commands"
        #$SSHOut = $(Invoke-SSHCommand -SSHSession $SSH -Command ($Commands -join ';')).Output
    }
}

Function UninstallDriverCommandsForEsxi67AndAbove{
    
    param(
        [Parameter(Mandatory=$true, HelpMessage = "Data Store Name")]    [string]$DatastoreName,
        [Parameter(Mandatory=$true, HelpMessage = "Destination directory")]    [string]$DestDir,
        [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/' + $DatastoreName + '/' + $DestDir
        $RemoteUnloadmodFileName = $FullRemoteFileLocation + '/' + $ZvmDriverUninstallSctiptFileName
        $RemoteLoadmodFileHelperName = $FullRemoteFileLocation + '/' +  $ZvmDriverHelperFuncsScriptFileName
            
        $UninstallScriptTempPath = '/tmp/' + $ZvmDriverUninstallSctiptFileName
        $ScriptHelperTempPath = '/tmp/' + $ZvmDriverHelperFuncsScriptFileName
        
        $Commands = 'cp '+ $RemoteUnloadmodFileName + ' ' +$UninstallScriptTempPath,
                    'cp '+ $RemoteLoadmodFileHelperName + ' ' + $criptHelperTempPath,
                    'chmod a+x ' + $UninstallScriptTempPath,
                    $UninstallScriptTempPath+ ' cleanup > /etc/vmware/zunloadmod.txt',
                    'rm ' + $UninstallScriptTempPath,
                    'rm ' + $ScriptHelperTempPath
                    
        Write-Host "Command: $Commands"
        #$SSHOut = $(Invoke-SSHCommand -SSHSession $SSH -Command ($Commands -join ';')).Output
    }
}

Function DefaultUninstallDriverCommands{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true, HelpMessage = "Data Store Name")]    [string]$DatastoreName,
        [Parameter(Mandatory=$true,    HelpMessage = "Destination directory")]    [string]$DestDir,
        [Parameter(Mandatory=$true, HelpMessage = "ZVM Uninstall script file name")] [string]$ZvmDriverUninstallSctiptFileName
    )
    
    process {
        $FullRemoteFileLocation = '/vmfs/volumes/' + $DatastoreName + '/' + $DestDir
        $RemoteUnloadmodFileName = $FullRemoteFileLocation + '/' + $ZvmDriverUninstallSctiptFileName
        
        $Commands = 'chmod a+x ' + $RemoteUnloadmodFileName,
                    $RemoteUnloadmodFileName + ' cleanup > /etc/vmware/zunloadmod.txt'
                    
        Write-Host "Command: $Commands"
        #$SSHOut = $(Invoke-SSHCommand -SSHSession $SSH -Command ($Commands -join ';')).Output
    }
}