Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 希望加速此PowerShell功能_Loops_Powershell - Fatal编程技术网

Loops 希望加速此PowerShell功能

Loops 希望加速此PowerShell功能,loops,powershell,Loops,Powershell,我正在运行以下代码从SCOM 2012中提取数据,并使用SCCM 2012中导出的电子表格,输出等待重新启动的服务器及其SCCM维护窗口,以便自动计划重新启动 代码运行大约需要5-8分钟,我想知道是否有任何方法可以加快这个过程。在Begin循环下运行的代码是瓶颈 Function Generate-RebootData{ IF(Get-Command Get-SCOMAlert -ErrorAction SilentlyContinue){}ELSE{Import-Module Operatio

我正在运行以下代码从SCOM 2012中提取数据,并使用SCCM 2012中导出的电子表格,输出等待重新启动的服务器及其SCCM维护窗口,以便自动计划重新启动

代码运行大约需要5-8分钟,我想知道是否有任何方法可以加快这个过程。在Begin循环下运行的代码是瓶颈

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!"}
 } }
}
您应该首先执行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
}

乍一看,我假设它是Get WmiObject,因为WMI是出了名的慢。我建议改为查看CIM会话。我正在考虑使用哪个使用运行空间池进行多线程处理。此更新的代码未运行,我尝试在WMI调用之前添加ping检查,但它也失败,无法索引到空数组。更新后的代码生成错误索引操作失败;数组索引的计算结果为空。在MaintenanceWindow=if$table[$\ux.NetbiosComputerName]{