Search

PowerCLI Function to install VIBs


In today's blog post, we will be looking at a PowerCLI/PowerShell function, that allows us to install VIBs to ESXi Hosts.

Typically, we install VIBs to host by transferring the VIB file to the host using file transfer tools likes WINSCP and then running ESXCLI commands via SSH or console.

With this function, we would just need a single tool i.e. PowerCLI, in order to install a VIB to a host. When using this function, you will have to provide the following parameters:

- VibName: Name of the VIB file that you want to install.

- LocalVibPath: File path to where the VIB file is stored on your system.

- VIBDatastore: The host datastore where you would want to store the VIB file.

- Server: ESXi host where you want to install the VIB.

- RootUser: ESXi host root user.

- RootPassword: ESXi host root user password.


The Function:

#
#Function to install ESXi VIBs using PowerCLI
#Written by sourav.mitra1990@gmail.com
#www.virtualmystery.info
#

#Function to upload vib to datastore from local path and install to the selected server
function Install-HostVib{
    #Define all parameters required
    Param (
            [Parameter(Mandatory=$true)][string][ValidateNotNullorEmpty()]$VibName,
            [Parameter(Mandatory=$true)][string][ValidateNotNullorEmpty()]$LocalVibPath,
            [Parameter(Mandatory=$true)][string][ValidateNotNullorEmpty()]$VIBDatastore,
            [Parameter(Mandatory=$true)][string][ValidateNotNullorEmpty()]$Server,
            [Parameter(Mandatory=$true)][string][ValidateNotNullorEmpty()]$RootUser,
            [Parameter(Mandatory=$true)][string][ValidateNotNullorEmpty()]$RootPassword
            )
        #Connect to the server
        Connect-VIServer -Server $Server -User $RootUser -Password $RootPassword

        #Declare VIB paths
        $vibdspath = "DS:\VIB\$VibName"
        $VibUrl= "[$VIBDatastore]VIB/$VibName"

        #upload the VIB to target datastore
        $ds= Get-Datastore $VIBDatastore
        New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root "\" > $null
        Copy-DatastoreItem -Item $LocalVibPath -Destination $vibdspath -ErrorAction SilentlyContinue -Force -Recurse
        Remove-PSDrive -Name DS 

        #Install the VIB to the server
        $vmhost= Get-VMHost -Name $Server
        $esxcli= Get-EsxCli -VMHost $vmhost -V2
                $vibParm = @{
                                viburl = $VibUrl
                                dryrun = $false
                                nosigcheck = $true
                                maintenancemode= $false
                                force = $false
                            }
        $vibinstall= $ESXCLI.software.vib.install.Invoke($vibParm)

        #Return message to user if vib installation was successful         
        Write-Host "$vibinstall.Message" -BackgroundColor DarkYellow
        
Disconnect-VIServer * -Confirm:$false
}

Screenshots of the function in action! :





Happy testing!!

50 views0 comments

Recent Posts

See All