2016-01-23 55 views
0

我正在運行以下代碼以從SCOM 2012中提取數據並使用SCCM 2012導出的電子表格,輸出服務器正在等待重新啓動,以及SCCM維護時段用於自動化計劃重新啓動。加速此PowerShell功能

代碼需要大約5-8分鐘才能運行,我想知道是否有任何方法可以加速此過程。在Begin Loop下運行的代碼是瓶頸。

Function Generate-RebootData{ 
IF(Get-Command Get-SCOMAlert -ErrorAction SilentlyContinue){}ELSE{Import-Module OperationsManager} 

"Get Pend reboot servers from prod" 
New-SCOMManagementGroupConnection -ComputerName ProdSrv 

$AlertData = get-SCOMAlert -Criteria ` 
"Severity = 1 AND ResolutionState < 254 AND Name = 'Pending Reboot'" | 
Select NetbiosComputerName 

"Get Pend reboot servers from cert" 
#For cert information 
New-SCOMManagementGroupConnection -ComputerName CertSrv 

$AlertData += Get-SCOMAlert -Criteria ` 
"Severity = 1 AND ResolutionState < 254 AND Name = 'Pending Reboot'" | 
Select NetbiosComputerName 

"Remove duplicates" 
$AlertDataNoDupe = $AlertData | Sort NetbiosComputerName -Unique 

"Create hash table" 
$table = @{} 
"Populate hash table" 
Import-Csv D:\Scripts\servers2.csv | ForEach-Object { 
    $table[$_.Computername] = $_.'Collection Name'} 

"Create final object" 
$result = @{} 
"Begin Loop" 
$result = $AlertDataNoDupe | ForEach-Object { [PSCustomObject] @{ 

Server=$_.NetbiosComputerName 

MaintenanceWindow=IF($table[$_.NetbiosComputerName]){$table[$_.NetbiosComputerName]} 

       ELSE{"Not found!"} 

PingCheck=IF(Test-Connection -Count 1 $_.NetbiosComputerName -Quiet -EA SilentlyContinue) 
     {"Alive"} 
     ELSE{"Dead"} 

LastReboot=Try{ 
$operatingSystem = Get-WmiObject Win32_OperatingSystem -ComputerName ` 
$_.NetbiosComputerName -ErrorAction Stop 
     [Management.ManagementDateTimeConverter]::ToDateTime(` 
$operatingSystem.LastBootUpTime)} 
     Catch{"Access Denied!"} 
} } 
} 
+0

初步猜測,我假設它是'Get-WmiObject',因爲WMI非常慢。我建議改爲查看CIM會話。 – Eris

+0

我在考慮使用[這個偉大的腳本](https://mjolinor.wordpress.com/2014/06/03/invoke-scritptasync-v2/),它使用Runspace Pools進行多線程。 – user4317867

回答

0

您應該執行PingCheck第一,且僅當成功移動在與Get-WmiObject電話 - 有沒有必要,如果你剛剛決定了它是「死」聯繫的機器。

... 
$result = $AlertDataNoDupe | ForEach-Object { 
    # Create hashtable 
    $Properties = @{ 
     Server = $_.NetbiosComputerName 
     MaintenanceWindow = if($table[$_.NetbiosComputerName]){ 
      = $_.NetbiosComputerName 
     } else { 
      'Not found!' 
     } 
    } 

    # Perform ping check, keep as boolean 
    $Properties['PingCheck'] = Test-Connection -Count 1 $_.NetbiosComputerName -Quiet -EA SilentlyContinue 

    $Properties['LastReboot'] = if($Properties['PingCheck']) 
    { 
     try 
     { 
      # Server seems to be online 
      $operatingSystem = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $_.NetbiosComputerName -ErrorAction Stop 
      [Management.ManagementDateTimeConverter]::ToDateTime($operatingSystem.LastBootUpTime) 
     } 
     catch 
     { 
      'Access Denied!' 
     } 
    } 
    else 
    { 
     # If server doesn't respond, declare it offline 
     'Computer offline!' 
    } 

    # create the object 
    New-Object -TypeName psobject -Property $Properties 
} 
+0

此更新後的代碼無法運行,我嘗試在WMI調用之前添加ping檢查,但它也失敗,導致無法索引到空數組中。更新的代碼產生錯誤「索引操作失敗;在維護窗口= if($ table [$ _。NetbiosComputerName]){[' – user4317867