我正在運行以下代碼以從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!"}
} }
}
初步猜測,我假設它是'Get-WmiObject',因爲WMI非常慢。我建議改爲查看CIM會話。 – Eris
我在考慮使用[這個偉大的腳本](https://mjolinor.wordpress.com/2014/06/03/invoke-scritptasync-v2/),它使用Runspace Pools進行多線程。 – user4317867