【问题标题】:RunSpacePool output CSV contains blank rowsRunSpacePool 输出 CSV 包含空白行
【发布时间】:2016-03-19 23:53:23
【问题描述】:

我正在使用this amazing answer 并让 RunSpacePools 输出一个 CSV 文件,但我的 CSV 文件有空白行,我无法弄清楚这些空白行的来源。

空白行在记事本中显示为,,,

    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()

【问题讨论】:

  • 如果代码底部的 foreach 循环是写入文件的内容,我会对其进行调试。重构代码以不使用管道。遍历作业并记录有关每个作业的信息。收集您认为您将写入另一个数据结构的内容。当您对此感到满意时,导出为 csv。
  • 感谢您的评论,恐怕这超出了我的想象。我正在寻找更多代码示例以使某些东西正常工作,但事情根本没有按预期工作。
  • 我正在使用 this method of run space pools 并获得按需要格式化但数据不正确的输出。

标签: csv powershell runspace


【解决方案1】:

经过反复试验,我最终通过with this method of run space pools 工作来接近。仔细观察,我发现输出被 WMI 的额外空格污染了。

为了解决这个问题,我最终在 ScriptBlock 的 Try 语句中使用了以下内容。

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

现在返回的数据都是单行的。

-编辑以评论 WMI 在输出中的额外空格。 See this question for more details.

考虑使用以下方法返回计算机的上次重新启动时间戳。请注意,您可以根据需要格式化字符串,see this library page for more info

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

观察空格,可以通过将输出转换为字符串然后使用Trim() 删除空格来删除空格。

【讨论】: