2014-02-27 3 views
0

나는이 질문이 전에 물어 봤고 가능한 해결책 몇 가지를 보았지만 그 중 하나를 내 스크립트에서 사용할 수없는 것 같아. 내 스크립트는 완제품이 아니며 정리가 필요하지만 지금은 분명히 작동합니다.동시 powershell 작업의 수 제한

스크립트의 기본 기능은 가상 컴퓨터의 스냅 숏을 만든 다음 백업을 위해 복제하는 것입니다. 모든 것이 예상대로 작동하지만 문제는 내 vcenter가 너무 많은 작업을 한 번에 넘치기 때문에 무릎에 내 vcenter를 가져 오는 것입니다. 한 번에 한 번에 3 ~ 4 개의 작업을 동시에 실행하는 것을 제한하고 싶었습니다. 누구든지 내게 손을 줄 수 있습니까? **

$backupinfo | % {Start-Job -Scriptblock $scriptblock -ArgumentList $_ | Out-Null} 

While((Get-Job -State 'Running').Count -ge 3) 
    { 
     Start-Sleep -Milliseconds 10 
    } 

업데이트 : 행운으로 스크립트의 끝에 다음 코드를 던지는 시도했습니다

Add-PSSnapin VMware.VimAutomation.Core 

#Connect to vCenter 
Connect-VIServer vcenter.mydomain.com 

#Define variables 
$vmhost = get-vmhost -name 10.1.1.10 

#Export list of VM's 
get-vmhost -name $vmhost | get-vm | select Name, BackupDS | Export-Csv -NoTypeInformation test.csv 

#Edit CSV to populate BackupDS Column 
(Import-Csv test.csv) | % { $_.BackupDS = '[NAS1]' +$_.BackupDS; $_ } | Export-Csv test.csv -NoTypeInformation 

#Edit CSV file to format required by script 
(gc test.csv) | foreach-object{$_ -replace 'Name','MasterVM'} | set-content test.csv -force 

#Import Backup CSV 
$backupinfo = Import-Csv test.csv 



$scriptblock = { 
Param($customer) 

    Add-PSSnapin VMware.VimAutomation.Core 

    #Connect to vCenter 
    Connect-VIServer vcenter.mydomain.com 

    #Define variables 
    $vmhost = get-vmhost -name 10.1.1.10 

    #Set Date format for clone names 
    $date = Get-Date -Format "MMddyyyy" 

    #Set Date format for emails 
    $time = (Get-Date -f "HH:MM") 

    $vm = Get-VM $customer.MasterVM 

    #Send Start Email 
    #C:\scripts\backupstartedemail.ps1 

    # Create new snapshot for clone 
    $cloneSnap = $vm | New-Snapshot -Name "Clone Snapshot" 

    # Get managed object view 
    $vmView = $vm | Get-View 

    # Get folder managed object reference 
    $cloneFolder = $vmView.parent 

    # Build clone specification 
    $cloneSpec = new-object Vmware.Vim.VirtualMachineCloneSpec 
    $cloneSpec.Snapshot = $vmView.Snapshot.CurrentSnapshot 

    # Make linked disk specification 
    $cloneSpec.Location = new-object Vmware.Vim.VirtualMachineRelocateSpec 
    $cloneSpec.Location.Datastore = (Get-Datastore -Name $customer.BackupDS | Get- View).MoRef 
    $cloneSpec.Location.Transform =  [Vmware.Vim.VirtualMachineRelocateTransformation]::sparse 

    $cloneName = "$vm-$date" 

    # Create clone 
    $vmView.CloneVM($cloneFolder, $cloneName, $cloneSpec) 

    # Write newly created VM to stdout as confirmation 
    Get-VM $cloneName 

    # Remove Snapshot created for clone 
    Get-Snapshot -VM (Get-VM -Name $customer.MasterVM) -Name $cloneSnap | Remove- Snapshot -confirm:$False 

    # Remove clones machine from inventory 
    remove-vm -vm $cloneName -confirm:$false 

    #Send Complete Email 
    #C:\scripts\backupcompletedemail.ps1 
} 

$backupinfo | % {Start-Job -Scriptblock $scriptblock -ArgumentList $_ | Out-Null} 

Get-Job | Wait-Job | Receive-Job 

: 미리 감사드립니다 ... 여기 내 코드입니다

여기 제공된 답변을 사용하여 시도했습니다 (Run N parallel jobs in powershell). 난 내 스크립트를 실행하면

Add-PSSnapin VMware.VimAutomation.Core 

#Connect to vCenter 
Connect-VIServer vcenter.mydomain.com 

#Define variables 
$vmhost = get-vmhost -name 10.1.1.10 

#Export list of VM's 
get-vmhost -name $vmhost | get-vm | select Name, BackupDS | Export-Csv -NoTypeInformation  test.csv 

#Edit CSV to populate BackupDS Column 
(Import-Csv test.csv) | % { $_.BackupDS = '[NAS1]' +$_.BackupDS; $_ } | Export-Csv test.csv  -NoTypeInformation 

#Edit CSV file to format required by script 
(gc test.csv) | foreach-object{$_ -replace 'Name','MasterVM'} | set-content test.csv -force 

#Import Backup CSV 
$backupinfo = Import-Csv test.csv 

#Set Date format for clone names 
$date = Get-Date -Format "MMddyyyy" 

#Set Date format for emails 
$time = (Get-Date -f "HH:MM") 

foreach ($customer in $backupinfo) { 
    $running = @(Get-Job | Where-Object { $_.State -eq 'Running' }) 
    if ($running.Count -le 8) { 
     Start-Job { 
      $vm = Get-VM $customer.MasterVM 

    #Send Start Email 
    #C:\scripts\backupstartedemail.ps1 

    # Create new snapshot for clone 
    $cloneSnap = $vm | New-Snapshot -Name "Clone Snapshot" 

    # Get managed object view 
    $vmView = $vm | Get-View 

    # Get folder managed object reference 
    $cloneFolder = $vmView.parent 

    # Build clone specification 
    $cloneSpec = new-object Vmware.Vim.VirtualMachineCloneSpec 
    $cloneSpec.Snapshot = $vmView.Snapshot.CurrentSnapshot 

    # Make linked disk specification 
    $cloneSpec.Location = new-object Vmware.Vim.VirtualMachineRelocateSpec 
    $cloneSpec.Location.Datastore = (Get-Datastore -Name $customer.BackupDS | Get- View).MoRef 
    $cloneSpec.Location.Transform =  [Vmware.Vim.VirtualMachineRelocateTransformation]::sparse 

    $cloneName = "$vm-$date" 

    # Create clone 
    #$vmView.CloneVM($cloneFolder, $cloneName, $cloneSpec) 

    # Write newly created VM to stdout as confirmation 
    #Get-VM $cloneName 

    # Remove Snapshot created for clone 
    Get-Snapshot -VM (Get-VM -Name $customer.MasterVM) -Name $cloneSnap | Remove-Snapshot -  confirm:$False 

    # Remove clones machine from inventory 
    #remove-vm -vm $cloneName -confirm:$false 

    #Send Complete Email 
    #C:\scripts\backupcompletedemail.ps1 
     } 
    } else { 
     $running | Wait-Job 
    } 
    Get-Job | Receive-Job 
} 

는 않습니다 모두가 각 가상 머신이 돌려 주어, 결코 (등 스냅 샷)

HasMoreData : True 
StatusMessage : 
Location  : localhost 
Command  : 
        $vm = Get-VM $customer.MasterVM 
        .................. 
        rest of my code 
        .................. 
JobStateInfo : Running 
Finished  : System.Threading.ManualResetEvent 
InstanceId : 81ac8e67-0267-4d11-998b-0e8cfa95292b 
Id   : 40 
Name   : Job40 
ChildJobs  : {Job41} 
PSBeginTime : 2/27/2014 12:41:19 PM 
PSEndTime  : 
PSJobTypeName : BackgroundJob 
Output  : {} 
Error   : {} 
Progress  : {} 
Verbose  : {} 
Debug   : {} 

당신이 제공 할 수있는 모든 지침 작업 중 하나를하지 않는다?

+0

실제로 시도한 것을 보여주고 "작동하지 않는 방법"을 설명해야합니다. 여기서 제시하는 코드는 동시 작업 수를 제한하지 않습니다. – alroc

+0

[PowerShell에서 N 개의 병렬 작업 실행] 가능한 복제본 (http://stackoverflow.com/questions/8781666/run-n-parallel-jobs-in-powershell) – alroc

+0

게시물에 해당 내용을 편집하십시오. 논평. – alroc

답변

1

워크 플로우 사용을 고려하십시오. 지능적 동시 실행 예컨대 :

workflow New-Clone([string[]]$Customer) { 
    InlineScript { Import-Module modules } 
    foreach -parallel($cust in $Customer) { 
     InlineScript { 
      ... 
      $vm = Get-VM $using:cust.MasterVM 
      ... 
     } 
    } 
} 

이 V3 PowerShell을 필요하거나 플로 지원 않는 높은 수를 스로틀 링한다 병렬의 foreach있다.

관련 문제