2016-02-29 111 views
1

我想創建具有作業的虛擬機。作業不會創建虛擬機

cls 
if (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) { 
    Add-PSSnapin VMware.VimAutomation.Core | Out-Null 
} 

#import VM settings 
$VMs = Import-CSV '...\Install_AIO_automated\Data.csv' -UseCulture 

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

$ScriptBlocky = { 
    Param($VM) 

    Write-Host "Begin" 
    $server += $VM.vs_xstream__vc_host 
    $connection = Connect-VIServer $VM.vs_xstream__vc_host -User $VM.vs_xstream__vc_admin -Password $VM.vs_xstream__vc_password 
    $connection.SessionId 

    Write-Host $connection.SessionId 
    Write-Host $OSspec 

    $OSspec = 'BasicLinuxSpec' 

    # Selecting random Cluster for VMs deployment 
    Write-Host $VM.Cluster 
    Write-Host $VM.vs_xstream__vc_host 
    $ClusterHoste = Get-Cluster $VM.Cluster -server $VM.vs_xstream__vc_host | Get-VMHost | Get-Random 
    #$ClusterHost = Get-Cluster $VM.Cluster -server $VM.vs_xstream__vc_host | Get-VMHost | Where{$_.ConnectionState -eq "Connected"} | Get-Random 
    Write-Host $connection.SessionId 
    Write-Host $ClusterHoste 

    Write-Host "" 
    $domain_name = $VM.FQDN.split('.') 
    Write-Host $domain_name 
    # 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 -Server $VM.vs_xstream__vc_host 
    # Starting VMs customization with previously created specification 
    $OSspec 

    Write-Host "TRY" 
    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 create:" 
    Write-Host $OSspec 
    Write-Host $ClusterHost 

    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 Customization Specification template 
    Remove-OSCustomizationSpec $OSspec -Server $VM.vs_xstream__vc_host -confirm:$false 

    Write-Host "Done" 
} 

foreach ($VM in $VMs) { 
    if ($VM.Use -eq 1) { 
     $session = $global:DefaultVIServer | %{ $_.sessionsecret } 
     $vcserver = $global:DefaultVIServer.ServiceUri.Host 

     $session 
     $vcserver 
     Write-Host "start script" 

     Start-Job -ScriptBlock $ScriptBlocky -ArgumentList $VM 

     Write-Host "end script" 

     Get-Job | Wait-Job 
    } 
} 

結果:

Current connections: 
1 

22f81178669d314003bc75206e3ffad384ee2568 

10.xx.xx.xx (IP address) 

start script 


Id  Name   PSJobTypeName State   HasMoreData  Location  
--  ----   ------------- -----   -----------  -------- 
46  Job46   BackgroundJob Running  True   localhost 

end script 

2  Job2   BackgroundJob Completed  False   localhost 
.. 

46  Job46   BackgroundJob Completed  True   localhost

什麼也沒有發生,沒有虛擬機被創建,但如果我運行此命令:

& $ScriptBlocky -VM $VM 

VM將被創建。

有沒有人看到我的錯誤或我做錯了什麼?

+0

好的,以前我試過用線程做同樣的事情,但是失敗了,現在正在做一些工作。 輸出: 啓動腳本 20 Job20 BackgroundJob運行真正的本地主機 端腳本 但Get_job給了我這個。 Job20 BackgroundJob Running True localhost 看起來像腳本不能做一些事情。看到帶有Receive-Job的輸出顯示以下行: New-OSCustomizationSpec -Name $ OSspec -Domain「xxxx.local」-DnsServer $ VM.DNS,$ VM.DNS2 -NamingScheme VM -OSType Linux -Server $ VM.vs_xstream__vc_host 沒有錯誤。不知道爲什麼。 – vencrena

+0

我覺得問題可能與會話... – vencrena

回答

0

這是用來創建虛擬機的代碼IM將我的代碼

$create = New-VM -Name $VM01Name -Template $VM01Template -Location $VM01Folder -VMHost $VM01Host -ResourcePool $VM01Resource -Datastore $VM01DiskDatastore01 -OSCustomizationSpec $VM01OSCustom -RunAsync -Confirm:$false 

看看,看看這是否會幫助

+0

這是正確的調用,但問題不存在。 – vencrena

0

同解決方案(線程沒有創建多個虛擬機(會話問題)) 題。

解決方案:*在所有其他操作之前創建與vCenter的所有連接,以便連接穩定。例如:所有的工作已經完成

# 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 
     } 
    } 
} 

後,我簡單地從所有會話

# Disconnect all created sessions 
ForEach($item in $server) 
{ 
    Write-Host ("Disconnecting from {0}...." -f ($item)) 
    Disconnect-VIServer $item -Confirm:$false 
} 

斷開現在腳本完美的作品,並在不同或相同的vCenter創建VM。如果有人會遇到任何問題,請告訴我。