1

我正在整理一个 powershell 脚本,它将使用 RunSpacePools 输出一个包含 1)ServerName、2)SCCM 维护窗口、3)PingCheck、4)LastRebootTimestamp 的 CSV 文件。

通过使用这个惊人的答案,我得到了一些工作,但我的 CSV 文件有空行,我坚持将 SCCM 维护窗口放入 CSV。

我不确定如何完成 SCCM 维护窗口查找然后将其添加$Job.Result$ScriptBlock

空白 CSV 行是,,,有些行没有额外的空白行。

-edit,我现在的想法是执行 SCCM 窗口查找,然后简单地将其作为另一个参数/参数传递到 runspacepool。

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

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

$AlertData = get-SCOMAlert -Criteria "Severity = 1 AND ResolutionState < 254 AND Name = 'Pending Reboot detected on the ConfigMgr 2012 Client'" | Select NetbiosComputerName

    "Get Pend reboot servers from test"
#For test information
New-SCOMManagementGroupConnection -ComputerName TestSCOMServer

$AlertData += Get-SCOMAlert -Criteria "Severity = 1 AND ResolutionState < 254 AND Name = 'Pending Reboot detected on the ConfigMgr 2012 Client'" | Select NetbiosComputerName

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

$Global:table = @{}
    "Populate hash table"

$MaintenanceWindow = Import-Csv D:\Scripts\MaintenanceWindow2.csv

$MaintenanceWindow | ForEach-Object {$Global:table[$_.Computername] = $_.CollectionName}

$scriptblock = {
 Param([string]$server)

#Try getting SCCM Maintenance Window
$SCCMWindow = IF($Global:table.ContainsKey($server)){
                $SCCMWindow = $table[$server]
                } Else { $SCCMWindow = "Not Found!"}

 $PingCheck = Test-Connection -Count 1 $server -Quiet -ErrorAction SilentlyContinue
        IF($PingCheck){$PingResults = "Alive"}
        ELSE{$PingResults = "Dead"}

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

 [PSCustomObject]@{

 Server=$server
 Ping=$PingResults
 LastReboot=$LastReboot
 }#end custom object
}#script block end

$RunspacePool = [RunspaceFactory]::CreateRunspacePool(100,100)
$RunspacePool.Open()
$Jobs = 
 foreach ( $item in $AlertDataNoDupe )
 {
 $Job = [powershell]::Create().
        AddScript($ScriptBlock).
        AddArgument($item.NetbiosComputerName)
 $Job.RunspacePool = $RunspacePool

 [PSCustomObject]@{
  Pipe = $Job
  Result = $Job.BeginInvoke()
 }
}

Write-Host 'Working..' -NoNewline

 Do {
  Write-Host '.' -NoNewline
  Start-Sleep -Seconds 1
} While ( $Jobs.Result.IsCompleted -contains $false)

Write-Host ' Done! Writing output file.'
Write-host "Output file is d:\scripts\runspacetest5.csv"

$(ForEach ($Job in $Jobs)
{ $Job.Pipe.EndInvoke($Job.Result) }) |
 Export-Csv d:\scripts\runspacetest5.csv -NoTypeInformation

$RunspacePool.Close()
$RunspacePool.Dispose()
4

1 回答 1

0

不确定这是否是最好的方法,但我最终使用了以下方法,如果在 MaintenanceWindow2.csv 中有两个条目,则会出现问题,因为它返回System.Object[]

$scriptblock = {
 Param([string]$server)

 $csv = Import-Csv D:\Scripts\MaintenanceWindow2.csv
 $window = $csv | where {$_.Computername -eq "$server"} | % CollectionName
 $SCCMWindow = IF ($window){$window}ELSE{"NoDeadline"}
}
于 2016-03-19T20:59:40.143 回答