Well....now that my brain is nearly wrecked i claim for help :smileycry:
I'm doing a script to find an entity in a set of Vcenters. I have to bring the name of the Entity, Type(i give to bring this variable...) and the Vcenter...Only two types of information of various vcenters.
Example:
Name | Type |
---|---|
CL002-01 | Cluster |
Vm001-POC | VirtualMachine |
The chalenge for this task is to reduce the time of response... The old script brings the information back but a lot of time is wasted.
I got here and my brain don't want to work anymore... Any help??
Add: a Doubt.. is that possible to connect to multiple Vcenters using "job-commands" and use the connection information of them in some way?
#Add-PSSnapin VMware.VimAutomation.Core
#Add-PSSnapin VMware.VumAutomation
#. "%programfiles%\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1"
# Adds the base cmdlets
Add-PSSnapin VMware.VimAutomation.Core -EA SilentlyContinue# Set to multiple VC Mode
if(((Get-PowerCLIConfiguration -Scope Session).DefaultVIServerMode) -ne "Multiple") {
Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope AllUsers -Confirm:$false | Out-Null
Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope User -Confirm:$false | Out-Null
Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope Session -Confirm:$false | Out-Null
}$count = 0
Function Get-INFOS{
param($dcInfo)
Get-Inventory -Location $dcInfo
}Function Find{
foreach ($info in $finalOUTPUT){if($info.Name -match "$typed*"){
$infoFound = $info.Name
$vcFound = ((($info.uid.split("@"))[1]).split(":"))[0]
Write-host "
Result Found($count):
Nome: $infoFound
VCenter: $VCFound
" -ForegroundColor Green
$count = $count +1
}
}
}Function Load{
$vCenters= import-csv lista_vcenters.csv -Header("Vcenter")
$finalOUTPUT = @()
#$finalOUTPUT = "" | Select-Object VC, EntityType, Entity
Write-Host ("Getting the Inventory info from {0} vCenters." -f $vCenters.Length)
foreach ($vCenter in $vCenters) {Start-job -scriptblock{
$vc = Connect-VIServer $vCenter.Vcenter -WarningAction SilentlyContinue
if (!$vc) {
Write-Host "Failure connecting to the vCenter $vCenter."
exit
}$dcs = Get-Datacenter -Server $vc
foreach ($dc in $dcs) {$vcname = $vCenter.vcenter
Write-host " "
Write-Host "Getting info from $vcname" -ForegroundColor Green
$finalOUTPUT += Get-INFOS $dc
}
Disconnect-VIServer $vc -Confirm:$false
} | Wait-Job
}
}Load
$typed = Read-Host "Type the name of the Entity you want to find:"
Find
while(((Read-Host "Do you want to start a new Search?") -eq "y")){
$count = 0
Clear
$typed = Read-Host "Type the name of the Entity you want to find:"
if($count -lt 1){
Write-Host "No Results Found!" -ForegroundColor Red
}
Find
}
When you run too many jobs in parallel, and thus place stress on memory and CPU, the total runtime of the sessions, then if you run them in batches.
With the Invoke-Command cmdlet you use the ThrottleLimit parameter, which allows a better control over how much parallelism you want.
See also Example 2 on the Start-Job cmdlet.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, that is possible.
Have a look at Clinton's post called Multithreading PowerCLI
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
So Luc, after burning some brain here i am:
I'm trying, first, to make a set of instructions to be runned at each of my 27 vCenters. Initially, i'm trying to get only the Clusters Names.:smileysilly: Until here OKAY
$vcenters = Import-Csv lista_vcenters.csv -Header("vcenter")
#Dentro do job, executar e exibir somente os resultados
$job = {add-pssnapin VMware.VimAutomation.Core -ea 0;
connect-viserver $args[0] -wa 0|Out-Null;
write-host "Vcenter: "$args[0];
write-host "--------------";
get-cluster|%{Write-Host "`t"$_.Name};
write-host "--------------";
Disconnect-VIServer $args[0] -Confirm:$false -wa 0 | Out-Null;
}
$jobsRunning = @()
foreach($vc in $vcenters){
#start-job -scriptblock $job -Name "meujobteste" | Wait-Job
$randJOB = "JOB_"+$vc.Vcenter
$jobsRunning += start-job -scriptblock $job -ArgumentList $vc.Vcenter -Name $randJOB
}
$jobsRunning | %{
$_ |Select State;
while(((Get-Job -Name $_.Name) | Select State).State -eq "Running"){
Get-Job -Name $_.Name| Wait-Job;
}
}
But when Running, i realize that for some reason, a few jobs were Stuck with "Blocked" Status... This make impossible to do what i want... Run the instructions at the same time at all vCenters.
PS C:\Windows\system32> get-job
Id Name State HasMoreData Location Command
-- ---- ----- ----------- -------- -------
355 JOB_SC... Completed True localhost add-pssnapin VMware.Vi...
353 JOB_SC... Blocked True localhost add-pssnapin VMware.Vi...
351 JOB_S003... Completed True localhost add-pssnapin VMware.Vi...
349 JOB_S02... Completed True localhost add-pssnapin VMware.Vi...
347 JOB... Completed True localhost add-pssnapin VMware.Vi...
345 JOB_S03... Completed True localhost add-pssnapin VMware.Vi...
343 JOB_SC01... Blocked True localhost add-pssnapin VMware.Vi...
341 JOB_... Completed True localhost add-pssnapin VMware.Vi...
339 JOB_SP1... Blocked True localhost add-pssnapin VMware.Vi...
337 JOB_S000... Completed True localhost add-pssnapin VMware.Vi...
335 JOB_5O Completed True localhost add-pssnapin VMware.Vi...
333 JOB_S001... Running True localhost add-pssnapin VMware.Vi...
331 JOB_S000... Completed True localhost add-pssnapin VMware.Vi...
329 JOB_S00... Completed True localhost add-pssnapin VMware.Vi...
327 JOB_SC00... Completed True localhost add-pssnapin VMware.Vi...
325 JOB_S000... Completed True localhost add-pssnapin VMware.Vi...
323 JOB_SO Completed True localhost add-pssnapin VMware.Vi...
321 JOB_SU Completed True localhost add-pssnapin VMware.Vi...
319 JOB_AS Completed True localhost add-pssnapin VMware.Vi...
371 JOB_S00... Completed True localhost add-pssnapin VMware.Vi...
369 JOB_S000... Blocked True localhost add-pssnapin VMware.Vi...
367 JOB_S006... Completed True localhost add-pssnapin VMware.Vi...
365 JOB_005... Completed True localhost add-pssnapin VMware.Vi...
363 JOB_S006... Completed True localhost add-pssnapin VMware.Vi...
361 JOB_S6... Completed True localhost add-pssnapin VMware.Vi...
359 JOB_00... Completed True localhost add-pssnapin VMware.Vi...
357 JOB_S03... Completed True localhost add-pssnapin VMware.Vi...
After that i try to receive the job data of the Jobs that were Completed...At that time, the Blocked Jobs became Running and Completed too...
Well... my doubt. There is a limitation about creating Jobs??
I just wanted to run the 27 processes at the same time but, probably i can't, right?
After All, i'm happy. Evolving so much!! :smileylaugh:
When you run too many jobs in parallel, and thus place stress on memory and CPU, the total runtime of the sessions, then if you run them in batches.
With the Invoke-Command cmdlet you use the ThrottleLimit parameter, which allows a better control over how much parallelism you want.
See also Example 2 on the Start-Job cmdlet.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
OK, so... instead doing like this:
$jobsRunning += start-job -scriptblock $job -ArgumentList $vc.Vcenter -Name $randJOB
i have to do like this?:
$jobsRunning += invoke-command -scriptblock $job -ArgumentList $vc.Vcenter -throttlelimit 27 -jobname $randJOB
I'm worried about the functionality. With the Start-Job command i can do almost everything i need (That is: Start the script block at all vCenters at the same time)
I can't test right now...i'm at home. Tomorrow i'll place things better in the script and let you know if it works.
Doubt 1: What happens with the 11 jobs remaining if i set the throttlelimit to 16? I need to run 27 jobs at the same time( I think...).
Doubt 2: I don't want to run these commands on other computers... The invoke-command needs to specify a remote computer?
I'm moving to another way around this script...it was taking too much time and effort, that, unfortunately i didn't have. As soon as possible, i'll return 😃