-1

I am using this amazing answer and got RunSpacePools to output a CSV file but my CSV file has blank rows and I just cannot figure out where the blank rows are coming from.

The blank lines are shown in Notepad as ,,,

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

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

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

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

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

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

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

$csv = Import-Csv D:\Scripts\MaintenanceWindow2.csv
$window = $csv | where {$_.Computername -eq "$server"} | % CollectionName
$SCCMWindow = IF ($window){$window}ELSE{"NoDeadline"}

 $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!"}

 #create custom object as output for CSV.
 [PSCustomObject]@{    
 Server=$server
 MaintenanceWindow=$SCCMWindow
 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 -Milliseconds 500
} While ( $Jobs.Result.IsCompleted -contains $false)

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

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

$RunspacePool.Close()
$RunspacePool.Dispose()
Community
  • 1
  • 1
user4317867
  • 2,397
  • 4
  • 31
  • 57
  • if that foreach loop at the bottom of your code is what is writing the file, i would debug that. refactor the code to not use the pipeline. loop over the jobs and log information about each job. gather what you think you will write into another data structure. when you are satisfied with that, export to csv. – Kory Gill Mar 20 '16 at 00:16
  • Thank you for the comment, I'm afraid this is over my head. I'm looking for more code examples to get something working but things are simply not working as expected. – user4317867 Apr 02 '16 at 20:01
  • I'm working with [this method of run space pools](http://learn-powershell.net/2012/05/10/speedy-network-information-query-using-powershell/) and get output that is formatted as desired but the data is incorrect. – user4317867 Apr 02 '16 at 23:08

1 Answers1

0

After trial and error, I ended up working with this method of run space pools to get close. Looking closer, I found the output was polluted by WMI's extra whitespaces.

To solve this, I ended up using the following within the ScriptBlock's Try statement.

$LastReboot = [Management.ManagementDateTimeConverter]::ToDateTime `
($operatingSystem.LastBootUpTime).ToString().Trim()

Now the data returned is all single line as desired.

-Edit to comment on WMI's extra whitespaces in output. See this question for more details.

Consider the following method to return a computer's last reboot timestamp. Note you can format the string as needed, see this library page for more info.

$os = (gwmi -Class win32_operatingsystem).LastBootUpTime
[Management.ManagementDateTimeConverter]::ToDateTime($os)

Whitespaces in WMI output in PowerShell

Observe the whitespaces, which can be removed by converting the output to a string then using Trim() to remove the whitespaces.

WMI output without extra WhiteSpaces

Community
  • 1
  • 1
user4317867
  • 2,397
  • 4
  • 31
  • 57