Script Created in PowerGui taking a long time to load

jahhan
jahhan used Ask the Experts™
on
I have several Powershell scripts, configured to retrieve information about MS Exchange 2003 services, taking a long time to display information through PowerGUI.  All of my scripts retrieve information for 25 servers.  Is there a reason as to why it take a long time to run some script and not others?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chris DentPowerShell Developer
Top Expert 2010

Commented:

Too many variables, it depends entirely on what each script does.

Chris

Author

Commented:
OK,
well this is one of the scripts that is slow to load.
------------------------------------------------------------------
$snServername='server01', 'server02' (all the way up to server25)
ForEach ($Srv in $snServerName)
{
$wmiServer = Get-Wmiobject -class Exchange_Server -computer $snServerName -namespace root\MicrosoftExchangeV2 #-ErrorAction SilentlyContinue
$cdoexmIExchangeServer = New-Object -com CDOEXM.ExchangeServer
$cdoexmIStorageGroup = New-Object -com CDOEXM.StorageGroup
$cdoexmIMailboxStoreDB = New-Object -com CDOEXM.MailboxStoreDB
#$cdoexmIExchangeServer.Datasource.Open(('LDAP://' + $wmiServer.DN))
$cdoexmIExchangeServer.Datasource.Open($Srv)
foreach ($sgStoragegroup in $cdoexmIExchangeServer.StorageGroups)
{
 if ($sgStoragegroup.Indexof("CN=Recovery Storage Group") -eq 0){"Recovery Storage Group"}
 else{
  $cdoexmIStorageGroup.DataSource.Open(('LDAP://' + $sgStoragegroup))
  foreach ($MailStore in $cdoexmIStorageGroup.MailboxStoreDBs)
  {
   $cdoexmIMailboxStoreDB.DataSource.Open(('LDAP://' + $MailStore))
   if($cdoexmIMailboxStoreDB.Status -eq 0){$cdoexmIMailboxStoreDB.Name + " Mounted"}
   if($cdoexmIMailboxStoreDB.Status -eq 1){$cdoexmIMailboxStoreDB.Name + " DisMounted"}
}
}
}
}
----------------------------------------------------------------------------------
I have to wait 10 minutes before the script completes.
Chris DentPowerShell Developer
Top Expert 2010

Commented:

Ouch, that's a long time.

You'd have to break it down further to figure out where it's slowing down. You could always insert a few Write-Host statements to mark progress.

Chris
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
How do I go about creating write-host statements?
PowerShell Developer
Top Expert 2010
Commented:

Like this... continual feedback so you can see how far your script has got, or what it's going to do next. From that you can figure out where it's getting stuck.

Chris
$snServername='server01', 'server02' (all the way up to server25)

ForEach ($Srv in $snServerName)
{
  # Show some progress indicators
  Write-Host "Starting Loop for $snServerName"

  Write-Host "Executing WMI Query"

  $wmiServer = Get-Wmiobject Exchange_Server -Computer $snServerName `
    -Namespace root\MicrosoftExchangeV2 #-ErrorAction SilentlyContinue

  Write-Host "Creating CDOEXM Objects"

  $cdoexmIExchangeServer = New-Object -com CDOEXM.ExchangeServer
  $cdoexmIStorageGroup = New-Object -com CDOEXM.StorageGroup
  $cdoexmIMailboxStoreDB = New-Object -com CDOEXM.MailboxStoreDB

  Write-Host "Opening Exchange Server Data Source"

  #$cdoexmIExchangeServer.Datasource.Open(('LDAP://' + $wmiServer.DN))
  $cdoexmIExchangeServer.Datasource.Open($Srv)

  Write-Host "Checking StorageGroups"

  foreach ($sgStoragegroup in $cdoexmIExchangeServer.StorageGroups)
  {
    if ($sgStoragegroup.Indexof("CN=Recovery Storage Group") -eq 0)
    {
      "Recovery Storage Group"
    }
    else
    {
      Write-Host "Opening Storage Group $sgStoragegroup"

      $cdoexmIStorageGroup.DataSource.Open(('LDAP://' + $sgStoragegroup))

      foreach ($MailStore in $cdoexmIStorageGroup.MailboxStoreDBs)
      {

        Write-Host "OPening Store $MailStore"

        $cdoexmIMailboxStoreDB.DataSource.Open(('LDAP://' + $MailStore))
        if ($cdoexmIMailboxStoreDB.Status -eq 0)
        {
          "$($cdoexmIMailboxStoreDB.Name) Mounted"
        }
        if ($cdoexmIMailboxStoreDB.Status -eq 1)
        {
          "$($cdoexmIMailboxStoreDB.Name) DisMounted"
        }
      }
    }
  }
}

Open in new window

Author

Commented:
thanks for this modified script.  I executed the script and it is stuck on "WMI Query".  In my testing environment it went through really quick (test environment only have one server).  In the production environment it did not even hit the first server yet and it has been 1 minute.
Chris DentPowerShell Developer
Top Expert 2010

Commented:

It could be trapped waiting for a timeout within WMI. It can be horribly slow if it does get stuck there. The namespace you're asking for isn't big, so I'd be surprised if it were more than that.

Have you tried executing just that command against the server in question separately?

Chris

Author

Commented:
I have not ran the command against the server in question; however, I am about to do that now, and get back to you with my findings.

Author

Commented:
Ok I was able to identify the server with the problem.  It seem as if my bridgehead server was the culprit.  After excluding the server from the script, it completed execution.

Author

Commented:
The script is still not instant; however, it completes in a reasonable amount of time.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial