问题
Data is taken from *.CSV file. Script should create in my test case 2 VM on the same vcenter but in different thread. Need guidance how to solve this.
cls
Add-PSSnapin VMware.VimAutomation.Core | Out-Null
#import VM settings
$VMs = Import-CSV '...\Data.csv' -UseCulture
#array for connections
$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 = @()
$global:DefaultVIServer
$ScriptBlocky = {
Param ($VM, $ses)
Add-PSSnapin VMware.VimAutomation.Core | Out-Null
Write-Host $VM.vm_name "Father session: " $ses.SessionSecret
$sessionf = Connect-VIServer $VM.vs_xstream__vc_host -Session $ses.SessionSecret -wa 0
Write-Host $VM.vm_name "Child session: " $sessionf.SessionId
$domain_name = $VM.FQDN.split('.')
Write-Host $domain_name
#Random for testing purposes
$OSspec = ("BasicLinuxSpec_rand{0}" -f (Get-Random))
# Create new OS Customization Specification template for further usage
New-OSCustomizationSpec –Name $OSspec –Domain ("{0}.{1}" -f $domain_name[1],$domain_name[2]) –DnsServer $VM.DNS,$VM.DNS2 –NamingScheme VM –OSType Linux
Get-OSCustomizationSpec $OSspec -Server $VM.vs_xstream__vc_host | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $VM.Ipaddress -SubnetMask $VM.NetMask -DefaultGateway $VM.Gateway
Write-Host $VM.vm_name "OSspec: "$OSspec
Write-Host $VM.vm_name "Cluster: "$VM.Cluster
Write-Host $VM.vm_name "Host: "$VM.vs_xstream__vc_host
# Selecting random Cluster for VMs deployment
$ClusterHost = Get-Cluster $VM.Cluster -server $VM.vs_xstream__vc_host | Get-VMHost | Get-Random
Write-Host "================="
Write-Host $VM.vm_name "ClusterHost " $ClusterHost
Write-Host "================="
New-Vm -VMhost $ClusterHost -Name $VM.vm_name -Server $VM.vs_xstream__vc_host -Datastore $VM.Datastore -Template $VM.Template -Description $VM.Description -DiskStorageFormat "Thin" -OScustomizationSpec $OSspec
Remove-OSCustomizationSpec $OSspec -Server $VM.vs_xstream__vc_host -confirm:$false
}
Write-Host ("Starting script: {0}" -f (Get-Date))
$startTime = Get-Date
$count = 0
ForEach($VM in $VMs)
{
$count = $count + 1
Write-Host $VM.vs_xstream__vc_host
Write-Host "Current connections:"
$Global:DefaultVIServers.count
$ses = Connect-VIServer $VM.vs_xstream__vc_host -User $VM.vs_xstream__vc_admin -Password $VM.vs_xstream__vc_password -wa 0
$Job = [powershell]::create()
$Job.RunspacePool = $RunspacePool
$Job.AddScript($ScriptBlocky).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()
}
#Disconnect-VIServer $VM.vs_xstream__vc_host -Confirm:$false -Force
}
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"
Results:
Name Port User
---- ---- ----
10.xxx.xx.10 443 VSPHERE.LOCAL\do1xstream
Starting script: 2/29/2016 2:48:20 PM
10.xxx.xx.10
Current connections:
1
Adding job to jobs list
10.xxx.xx.10
Current connections:
1
The specified mount name 'vmstores' is already in use.
The specified mount name 'vis' is already in use.
virtual38 Father session: f8bafd42e6b7c85aa313e2896eba11c0f9f876cf
Adding job to jobs list
virtual38 Child session: f8bafd42e6b7c85aa313e2896eba11c0f9f876cf
virtual38 domain com
Waiting...virtual39 Father session: f8bafd42e6b7c85aa313e2896eba11c0f9f876cf
virtual39 Child session:
virtual39 domain com
virtual39 OSspec: BasicLinuxSpec_rand618798101
virtual39 Cluster: do1mgmt
virtual39 Host: 10.xxx.xx.10
=================
virtual39 ClusterHost
=================
virtual38 OSspec: BasicLinuxSpec_rand581276505
virtual38 Cluster: do1mgmt
virtual38 Host: 10.xxx.xx.10
.=================
virtual38 ClusterHost 10.xxx.xx.2
=================
..............
As result shows because virtual38 was first in line it has session information and can perform actions.
maybe i am wrong about this I think session isssue is preventing to create second VM (virtual39 )
Update/notice: I realized that when threads $Throttle = 1 then both VM is created, when I set $Throttle = 2 or more then only one of them (first one) is created.
Any suggestions?
回答1:
Believe me or not but I found what was wrong. Script is working, but when connecting to new session or existing one (or even different vCenter, when is need to create VM in more then one vCenter environment) DefaultVIServers variable is modified and session drops.
Solution: * Create all connection to vCenter's before all other actions so connection would be stable. for example:
# Iterate and connect to all vCenters so connections to them will be stable and read only
ForEach($VM in $VMs)
{
If ($VM.Use -eq 1)
{
If($server.Contains($VM.vs_xstream__vc_host) -eq $FALSE)
{
$server += $VM.vs_xstream__vc_host
$AllSessions += Connect-VIServer $VM.vs_xstream__vc_host -User $VM.vs_xstream__vc_admin -Password $VM.vs_xstream__vc_password -NotDefault
Write-Host "Connected to: " $VM.vs_xstream__vc_host
}
}
}
After all work is done i simply disconnect from all sessions
# Disconnect all created sessions
ForEach($item in $server)
{
Write-Host ("Disconnecting from {0}...." -f ($item))
Disconnect-VIServer $item -Confirm:$false
}
Now script works perfectly and creates VM in different or the same vCenter. if anyone will experience any problems with it let me know.
来源:https://stackoverflow.com/questions/35711543/thread-does-not-create-multiple-vms-session-issue