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

Hey guys, another little update:

I’ve been working on utilizing the vSphere SDK as of late, but I’ve had little success in finding any useful guides. The VMWare site http://pubs.vmware.com/vsphere-60/index.jsp has some information that’s somewhat useful when attempting to learn how their SDK works, but I still didn’t get much out of it all. I googled around a bit to find bits and pieces and a lot of people expressed the same concern that I have – so I can at least relish the fact that I’m not alone in this thought. However, I want to try and figure it out to make my job a little easier and help develop some tools for the other individuals at our office. This will be a little mini-series of progressing through the vSphere SDK. I also wanted to provide some samples of a more recent build of the vSphere SDK, as a lot of other examples I’ve seen are relatively old and potentially out of date.

using System;
using System.Collections.Generic;
using VMware.Vim;

namespace VMTest
{
    class Program
    {
        // strings holding the basic data we'll need to connect
        private const string vURL = "https://iphere/sdk";
        private const string uName = "administrator@vsphere.local";
        private const string uPass = "password";
        
        static void Main()
        {
            Program p = new Program(); 
            Console.WriteLine("Starting the vSphere connection...");
            p.vSphere();
            Console.WriteLine("Program stopped!");
            Console.ReadKey();
        }
        
        public void vSphere()
        {
            //New VIServer connection
            VimClientImpl vClient = new VimClientImpl();
            //New connection to the vSphere Web Client (Over 443)
            ServiceContent sContent = vClient.Connect(vURL);
            //User credentials to utilize
            UserSession uSession = vClient.Login(uName, uPass);
            //Get the vms
            IList<EntityViewBase> vmList = vClient.FindEntityViews(typeof(VirtualMachine), null, null, null);
            //Power on the VMs
        
            foreach (VirtualMachine vm in vmList)
            {
                //VirtualMachine virt = (VirtualMachine)vm;
                Console.WriteLine("Powering on VM: " + vm.Name);
                ManagedObjectReference vmMor = vm.MoRef;
                vm.PowerOnVM(vm.MoRef);
            }
        
            //Log out of the vServer
            vClient.Logout();
        }
    }
}