Anything as Code: Upload File to ESXi Datasore Introduction. Function Upload-FileToDatastore. Get-CalculateWorkingTime. Brief Summary.

Introduction


Hello to all readers of the SDDC and Architecture, Solution, Implementation and Operations as Code blog.

Today we will talk about such a conceptual approach as Operations as Code, which is an integral part of the Anything as Code: strategy. I, as an Architect and Engineer in the field of cloud technologies, have been trying to adhere to this strategy as much as possible for the last ten years.

Today I want to share with you my small developments, which fit nicely into the Operation as Code approach.

Probably, we all constantly face the situation when we need to upload some file to the VMware Virtual Infrastructure Datastore, for example, the installation image of the operating system in order to mount it to the virtual machine.

I wrote a small Powershell function that performs the task of uploading a file to the ESXi datastore. Functions are passed to the input the necessary parameters in the form of a variable, changing which you can also use a code reuse strategy.

Upload-FileToDatastore


So let’s look at the code for the Upload-FileToDatastore function:

Function Upload-FileToDatastore {

<#
.SYNOPSIS
    Upload file to ESXi datastore.

.DESCRIPTION
    Silent deploy the specified file to ESXi datastore.

.PARAMETER Target
    Name or IP Address of the VMware ESXi.

.PARAMETER Path
    Path to source file.

.PARAMETER Source
    Name of source file.

.PARAMETER Datastore
    Name of destination ESXi Datastore.

.PARAMETER Folder
    Name of destination ESXi Datastore Folder.

.NOTES
    Version:        0.1
    Author:         Andrii Romanenko
    Website:        blogs.airra.net
    Creation Date:  12.05.2022
    Purpose/Change: Initial script development

    Version:        0.2
    Author:         Andrii Romanenko
    Change Date:    28.09.2022
    Purpose/Change: Add Work Time Measuring.

    Version:        0.3
    Author:         Andrii Romanenko
    Change Date:    30.12.2023
    Purpose/Change: Reorganize Function. Add Workflow Time Measuring External Function: Get-CalculateWorkingTime.

.EXAMPLE

    $Parameters = @{
        Target    = $ESXiHost
        Path      = "c:\!\"
        Source    = "en_windows_server_2019_x64_dvd_4cb967d8.iso"
        Datastore = $ISODatastore
        Folder    = "\ISO"
    }
    Upload-FileToDatastore @Parameters
#>

param ( 
    [Parameter(Mandatory)][string]$Target,
    [Parameter(Mandatory)][string]$Path,
    [Parameter(Mandatory)][string]$Source,
    [Parameter(Mandatory)][string]$Datastore,
    [Parameter(Mandatory)][string]$Folder 
)

# Get start time of the Upload ISO operation
$StartTime = Get-Date           
$Message = "Begin Upload: "  + $Source + " to Datastore: " + $Datastore + " on Target:" + $Target + "."
Write-Host -ForegroundColor Green $Message

# Upload ISO
$ds = Get-VMHost -Name $Target | Get-Datastore $Datastore
New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root "\" > $null
Copy-DatastoreItem -Item $Path$Source -Destination "DS:/$($Folder)"
Remove-PSDrive -Name DS -Confirm:$false

Write-Host -ForegroundColor Green "Upload Finished!"

# Get end time of the Upload ISO operation
$EndTime = Get-Date

# Calculate Elapsed Time of the operation
Get-CalculateWorkingTime -StartTime $StartTime -EndTime $EndTime
}

The function accepts the following mandatory parameters as input in the form of the @Parameters variable:

  • Target – Name or IP Address of the VMware ESXi;
  • Path – Path to source file;
  • Source – Name of source file;
  • Datastore – Name of destination ESXi Datastore;
  • Folder – Name of destination ESXi Datastore Folder.

At the beginning of work, the start time is fixed in the form of the $StartTime variable.

Next, the Datastore parameters of the ESXi Host are entered into the $ds variable.

To access the Datastore for the copy operation, the standard New-PSDrive Powershell commandlet is used. The copy operation is performed by the Copy-DatastoreItem command from VMware PowerCLI.

At the end of the operation, the PSDdrive object is deleted and the copying $EndTime is fixed.

The calculation of the duration of the copying operation is carried out by calling the external function Get-CalculateWorkingTime, which receives the parameters of the start and end of the operation.

The result of the function is shown in the following figure:

Figure 1. The result of the execution of the Upload-FileToDatastore Powershell function.

Get-CalculateWorkingTime


The code for the Get-CalculateWorkingTime function is below.

Function Get-CalculateWorkingTime {

<#
.SYNOPSIS
    Calculate working time of workflow.

.DESCRIPTION
    Calculate working time of workflow.

.PARAMETER StartTime
    Begin Time of Workflow.

.PARAMETER EndTime
    End Time of Workflow.

.NOTES
    Version:        0.1
    Author:         Andrii Romanenko
    Website:        blogs.airra.net
    Creation Date:  22.03.2023
    Purpose/Change: Initial script development

    Version:        0.2
    Author:         Andrii Romanenko
    Website:        blogs.airra.net
    Creation Date:  14.08.2023
    Purpose/Change: Add time measuring format in hours.

.EXAMPLE
    
    Get-CalculateWorkingTime -StartTime $StartTime -EndTime $EndTime 

#>

param ( 
    [Parameter(Mandatory)][datetime]$StartTime,
    [Parameter(Mandatory)][datetime]$EndTime 
)

# Calculate Elapsed Time of the workflow operation
$ElapsedTime = $EndTime-$StartTime
$ElapsedTime = '{0:hh} h. {0:mm} min. {0:ss} sec.' -f $ElapsedTime 
Write-Host 'Total Operation Duration:'  $ElapsedTime -ForegroundColor Green 
}

Brief Summary


So, in today’s post, I’ve shared with you some code snippets that demonstrate the strategy and practice of Operations as Code.

I note that you can use this code at your discretion, adding there, for example, verification codes and handling of exceptional situations, such as whether there is a connection to the ESXi host or a file with the same name is present, etc.

Also, don’t forget that the function requires the presence of the VMware PowerCLI.

That’s all for today. But in the next publication, I will tell about the function that will help check the already copied file on the datastore for the checksum.

Follow the news until the meeting is on air in a few days.
Sincerely, AIRRA.

This entry was posted in Code, Programming, Technology, Virtualization, VMware and tagged , , , , , , , , , , . Bookmark the permalink.