header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
foreach very slow
Last Post 11 May 2011 07:29 PM by Daniel Petcher. 3 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
jbizUser is Offline
New Member
New Member
Posts:2
Avatar

--
12 Feb 2010 01:04 PM
    Here is the script (SCOM 2007 maintenance mode):

    param($rootMS,$minutesMM,$comment,$textFile) 
    $servername = Get-Content $textFile 
    Add-PSSnapin "Microsoft.EnterpriseManagement.OperationsManager.Client" -ErrorVariable errSnapin
    Set-Location "OperationsManagerMonitoring::" -ErrorVariable errSnapin
    new-managementGroupConnection -ConnectionString:$rootMS -ErrorVariable errSnapin
    set-location $rootMS -ErrorVariable errSnapin
    foreach ($srv in $servername) { 
      $computerPrincipalName = $agent.displayname 
      $computerPrincipalName 
      $computerClass = get-monitoringclass -name:Microsoft.Windows.Computer 
      $computerCriteria = "PrincipalName='" + $computerPrincipalName + "'" 
      $Computer = Get-MonitoringObject -monitoringclass:$ComputerClass | where {$_.PathName -match $srv} 
      $startTime = [System.DateTime]::Now 
      $endTime = $startTime.AddMinutes($minutesMM) 
      "Putting " + $computer + " into maintenance mode" 
      New-MaintenanceWindow -startTime:$startTime -endTime:$endTime -monitoringObject:$computer -comment:$comment 
    }



    It reads a text file and puts each server from that text file into maintenance mode. It does each server one at a time and only goes to the next one after the previous one has finished. You can imagine this can take a while with a lot of servers. Does anyone know a way to speed it up?
    Steven MurawskiUser is Offline
    Community Co-Director
    Basic Member
    Basic Member
    Posts:137

    --
    12 Feb 2010 01:26 PM
    I'll have to look a bit closer for some perfomance suggestions (should have some time this weekend), but you might have some unintended consequences with your "-ErrorVariable" assignments.
    By default, the ErrorVariable will replace the existing value with the new error.. that means if you have an error in Add-PsSnapin and one in Set-Location, the Set-Location error will overwrite the Add-PSSnapin error.
    If you add a "+" in front of the variable name, it will add the new error records, rather than replacing them.
    E.g. -ErrorVariable +errSnapin
    Steven Murawski
    Blog ( blog.usepowershell.com )
    Co-Host - Mind of Root ( www.mindofroot.com )
    Marco Shaw (MVP)User is Offline
    Veteran Member
    Veteran Member
    Posts:1647
    Avatar

    --
    12 Feb 2010 03:18 PM
    Everything that's somewhat static, move it before the foreach.

    Example:
    $Computer = Get-MonitoringObject -monitoringclass:$ComputerClass

    Move that to be eval'd before the foreach, then use $Computer within your foreach.

    Move all your date calculations before the foreach also since you don't need to calculate them each time.

    If you're still stuck, let us know. I might be able to provide more feedback on Monday or Tuesday...
    Marco

    *Microsoft MVP - Windows PowerShell
    https://mvp.support.microsoft.com/profile/Marco.Shaw
    *Co-Author - Sams Windows PowerShell Unleashed 2nd Edition
    *Blog - http://marcoshaw.blogspot.com
    Daniel PetcherUser is Offline
    New Member
    New Member
    Posts:7
    Avatar

    --
    11 May 2011 07:29 PM
    If you have PowerShell v2.0 you can use a Start-Job command. If you put it inside the foreach loop, the servers will process in parallel, rather than in series.
    My opinions are mine. My employers have other people to speak for them.
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer