1

I'm putting together a powershell script that will use RunSpacePools to output a CSV file containing 1)ServerName, 2)SCCM Maintenance Window, 3)PingCheck, 4)LastRebootTimestamp.

I've got something working by using this amazing answer but my CSV file has blank lines and I'm stuck on getting the SCCM Maintenance Window into the CSV.

I'm unsure of how to complete the SCCM Maintenance Window lookup then add it to the output of the $Job.Result or could I just add it into the $ScriptBlock and let the RunSpacePool very quickly complete the lookup.

The blank CSV line is ,, and some lines don't have the extra blank line.

-edit, my thinking is now to perform the SCCM window lookup then simply pass that into the runspacepool as another param/argument.

    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()
Community
  • 1
  • 1
user4317867
  • 2,397
  • 4
  • 31
  • 57
  • With a RunspacePool it may be better to use s synchronized hash table `$Global:table = [hashtable]::Synchronized(@{})` this will prevent access conflicts when executing multiple jobs at once. – Jan Chrbolka Mar 15 '16 at 06:12
  • I'm trying to get this working but things are just not going my way. – user4317867 Mar 18 '16 at 15:21
  • Ended up using this approach by taking [the answer here](http://stackoverflow.com/questions/17730839/import-csv-and-search-the-results) and using an IF statement but now some results are `System.Object[]` which I believe is a result of being in the list/CSV twice. – user4317867 Mar 18 '16 at 16:36

1 Answers1

0

Not sure if this is the best way but I ended up using the following which presents a problem in the event there are two entries in MaintenanceWindow2.csv because it returns 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"}
}
user4317867
  • 2,397
  • 4
  • 31
  • 57