vSphere 6.5 – Upload Folder to Datastore

I’ve been working with vSphere 6.5 lately and I noticed a shortcoming that it had which I’m sure others have seen too. There’s no current option to upload multiple files to a datastore, which subsequently leaves the only option to upload files one at a time. Suffice to say, that’s not an option that I’d like to do. I made a quick script to help faciliate uploading folders along with the structure they had.

#Dependency for pointing to a folder
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")

#region Template script for handling VIServer login

Write-Host "Welcome to the folder uploader script."
$VIServerConnection = $false
$datastoreValue = $false
$datastore = $null

While(!$VIServerConnection)
{
    #Gather vSphere username and password
    Write-Host "Please input the vCenter login credentials:"
    $vCenterCreds = Get-Credential

    #Request for the address
    Write-Host "Please input the vCenter IP address:"
    $vCenterAddress = GenerateForm -formName "vCenter Address" -formContent "Input the vCenter IP address"
    $vCenterAddress = GenerateWarningForm -priorFormContent $vCenterAddress -formName "vCenter Address" -formContent "Are you sure this is the correct information?"

    $VIServer = Connect-VIServer -Server $vCenterAddress -Credential $vCenterCreds -WarningAction Continue

    if($VIServer.IsConnected)
    {
        $ViServerConnection = $true
    }else{
        cls
        Write-Host "Unable to establish a connection to the vCenter: $vCenterAddress! Please try again."
    }
}

#endregion

while(!$datastoreValue)
{
    #Grab the datastore name
    Write-Host "Please input the datastore name"
    $datastoreName = Read-Host
    $datastore = Get-Datastore $datastoreName

    if($datastore -ne $null)
    {
        $datastoreValue = $true
    }else{
        Write-Host "Error finding the datastore with the name $datastoreName"
    }
}

$folderpath = $null
$folder = New-Object System.Windows.Forms.FolderBrowserDialog
$folder.rootfolder = "MyComputer"

if($folder.ShowDialog() -eq "OK")
{
    $folderpath += $folder.SelectedPath
}

#create the psdrive
New-PSDrive -PSProvider VimDatastore -Root "\testfolder" -Location $datastore -Name ds1
New-PSDrive -PSProvider FileSystem -Root $folderpath -Name base

Copy-DatastoreItem base: ds1: -Force -Recurse

#Optional to remove the folder that was created
#Code below will match folder name
#$path = [Regex]::Match($folderpath, '\\(?:.(?!\\))+$')
#$path = "ds1:" + $path.Value
#Remove-Item -Path $path -Recurse | Where {$_.PSIsContainer }
Remove-PSDrive -Name ds1
Remove-PSDrive -Name base

Leave a Reply

Your email address will not be published. Required fields are marked *