1

I have a problem with creating multiple VMWARE VM's from template in single VIServer. In following code when put anything bigger than 1 in throttle script only executes one vm and don't create any other (in parallel), it seems to have problem with keeping the session most likely just reset it or modify it in $global:DefaultVIServer.

Code below is based on question in this thread: Thread does not create multiple VMs (session issue) The answer is to keep the same session all the time but I couldn't get this to work

I also assume that this post was about the same issue I have: https://communities.vmware.com/thread/463060?start=0&tstart=0

I wonder if anyone had working solution in using runspace in powershell to create multiple vms in vmware ?

cls
Add-PSSnapin VMware.VimAutomation.Core | Out-Null 
#import csv
$VMs = Import-CSV 'filename.csv' -UseCulture
#connections array
$server = @()
#threads
$throttle = 2
# Create session state
$sessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
# Create runspace pool consisting of runspaces
$RunspacePool = [RunspaceFactory]::CreateRunspacePool(1, $throttle, $sessionState, $host)
$RunspacePool.Open()
#array for jobs
$Jobs = @()
#this contains all connected vSphere endpoints
$global:DefaultVIServer

$User = "domain\username"
$PWord = ConvertTo-SecureString -String "password" -AsPlainText -Force
$Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $User, $PWord   



# SctiptBlock to execute for each job
$ScriptBlock = {
    Param ($VM, $ses) 

    Add-PSSnapin VMware.VimAutomation.Core | Out-Null
    Write-Host $VM.Name "Father session:    "  $ses.SessionSecret    

    Connect-VIServer $VM.VIServer -Session $ses.SessionSecret -wa 0
    Write-Host $VM.Name "Child session:    "   $sessionf.SessionId

    New-VM -Name $VM.Name -ResourcePool "..." -Location "Windows Servers" -Datastore "..." -DiskStorageFormat "..." -Notes "..." -Template "..."
}


# Starting script
Write-Host ("Starting script: {0}" -f (Get-Date))
$startTime = Get-Date

$count = 0
ForEach($VM in $VMs)
{

    $count  = $count + 1

    Write-Host "Current connections:"
    $Global:DefaultVIServers.count

    $ses = Connect-VIServer $VM.VIServer -Credential $Credential -wa 0   

    $Job = [powershell]::create()
    $Job.RunspacePool = $RunspacePool
    $Job.AddScript($ScriptBlock).AddParameter("VM", $VM).AddParameter("ses", $ses)

    Write-Host "Adding job to jobs list"    
    $Jobs += New-Object PSObject -Property @{
        RunNum = $count
        Job = $Job
        Result = $Job.BeginInvoke() | Out-Null
        }    
}

Write-Host "Waiting.." -NoNewline
Do {
   Write-Host "." -NoNewline
   Start-Sleep -Seconds 1
} While ( $Jobs.Result.IsCompleted -contains $false)

$endTime = Get-Date
$totalSeconds = "{0:N4}" -f ($endTime-$startTime).TotalSeconds
Write-Host "All jobs finished in $totalSeconds seconds"
Community
  • 1
  • 1
Rafal
  • 217
  • 1
  • 3
  • 9
  • Is there any reason you aren't using the native -AsJob switch on New-VM? – Jim Moyle Feb 02 '17 at 20:57
  • I don't think this vmware cmdlet have such switch https://www.vmware.com/support/developer/PowerCLI/PowerCLI51/html/New-VM.html – Rafal Feb 02 '17 at 21:32
  • My apologies, that's the Hyper-V syntax, the VMware parameter syntax for the same is -runasync. Same question... – Jim Moyle Feb 03 '17 at 09:02
  • Hi I managed to do it using jobs in powershell by creating connection to vcenter (reusing session) in each job. The thing is that I want to use runspaces and compare performance with jobs. It should work in exactly same way but for some reason session is disconnecting for all despite first job. – Rafal Feb 05 '17 at 20:11

0 Answers0