线程不会创建多个VM(会话问题) [英] Thread does not create multiple VMs (session issue)

查看:101
本文介绍了线程不会创建多个VM(会话问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据取自* .CSV文件.脚本应在我的测试用例2 VM中在相同vcenter上但在不同线程中创建.需要指导如何解决这个问题.

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"

结果:

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
=================
..............

结果显示,因为virtual38是第一个队列,所以它具有会话信息并可以执行操作.

As result shows because virtual38 was first in line it has session information and can perform actions.

也许对此我错了,我认为会话问题阻止了创建第二个VM(virtual39)

maybe i am wrong about this I think session isssue is preventing to create second VM (virtual39 )

更新/通知:我意识到,当线程$ Throttle = 1时,将创建两个VM;当我将$ Throttle = 2或更大时,将仅创建其中一个(第一个).

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.

有什么建议吗?

推荐答案

信不信由你,但我发现问题出在哪里.脚本正在运行,但是当连接到新会话或现有会话(甚至是不同的vCenter,需要在多个vCenter环境中创建虚拟机)时,将修改DefaultVIServers变量,并删除会话.

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.

解决方案: *在执行所有其他操作之前,请创建与vCenter的所有连接,以使连接稳定.例如:

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
}

现在脚本可以完美运行,并且可以在不同或相同的vCenter中创建VM.如果有人会遇到任何问题,请告诉我.

Now script works perfectly and creates VM in different or the same vCenter. if anyone will experience any problems with it let me know.

这篇关于线程不会创建多个VM(会话问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆