Powershell将表转换为数组

时间:2016-05-13 21:22:46

标签: powershell splunk

我正在尝试通过Powershell在WSUS API中运行查询,输出计算机名称,所需的补丁等,然后我需要将其注入“日志”文件,该文件被摄入Splunk,以便我们可以制作仪表板等。

我目前的代码是

    $computerscope = New-Object Microsoft.UpdateServices.Administration.ComputerTargetScope
    $LogTime = Get-Date -Format "MM-dd-yyyy_hh-mm-ss"
    $updatescope = New-Object Microsoft.UpdateServices.Administration.UpdateScope
    $wsus.GetSummariesPerComputerTarget($updatescope,$computerscope) |
    Select-Object $logtime,@{L=’ComputerTarget';E={($wsus.GetComputerTarget([guid]$_.ComputerTargetId)).FullDomainName}},
    @{L=’NeededCount';E={($_.DownloadedCount + $_.NotInstalledCount)}},DownloadedCount,NotApplicableCount,NotInstalledCount,InstalledCount,FailedCount | Select-String Computer

输出如下:

@{05-13-2016_05-12-25=; ComputerTarget=########; NeededCount=12; DownloadedCount=0; NotApplicableCount=82245; NotInstalledCount=12; InstalledCount=23; FailedCount=0}

我需要它看起来像这样:

05-13-2016_05-12-25=; ComputerTarget=#######; NeededCount=12; DownloadedCount=0; NotApplicableCount=82245; NotInstalledCount=12; InstalledCount=23; FailedCount=0

如果您想尝试问题的根源,我正在尝试将表转换为数组,以便splunk可以逐行读取它,但这会给出一个我想要转换的表:

$computerscope = New-Object Microsoft.UpdateServices.Administration.ComputerTargetScope
$LogTime = Get-Date -Format "MM-dd-yyyy_hh-mm-ss"
$updatescope = New-Object Microsoft.UpdateServices.Administration.UpdateScope
$wsus.GetSummariesPerComputerTarget($updatescope,$computerscope) |
Select-Object $logtime,@{L=’ComputerTarget';E={($wsus.GetComputerTarget([guid]$_.ComputerTargetId)).FullDomainName}},
@{L=’NeededCount';E={($_.DownloadedCount + $_.NotInstalledCount)}},DownloadedCount,NotApplicableCount,NotInstalledCount,InstalledCount,FailedCount `

给出输出:

05-13-2016_05-16-04 : 
ComputerTarget      : ########
NeededCount         : 12
DownloadedCount     : 0
NotApplicableCount  : 82245
NotInstalledCount   : 12
InstalledCount      : 23
FailedCount         : 0

1 个答案:

答案 0 :(得分:0)

您似乎只想删除使用正则表达式执行此操作的前导@{和尾随}

...allyourcode... | Select-String Computer | ForEach-Object { $_.Line -replace '^\@\{(.*?)\}$', '$1' }

但是,使Select-String将对象转换为字符串表示形式是导出数据的不良方法。 Splunk可以读取CSV文件,因此我建议使用它(并且还使用不动产的logtime)。例如:

$computerscope = New-Object Microsoft.UpdateServices.Administration.ComputerTargetScope
$LogTime = Get-Date -Format "MM-dd-yyyy_hh-mm-ss"
$updatescope = New-Object Microsoft.UpdateServices.Administration.UpdateScope
$wsus.GetSummariesPerComputerTarget($updatescope,$computerscope) |
Select-Object @{L="LogTime";e={ $logtime }},@{L=’ComputerTarget';E={($wsus.GetComputerTarget([guid]$_.ComputerTargetId)).FullDomainName}},
@{L=’NeededCount';E={($_.DownloadedCount + $_.NotInstalledCount)}},DownloadedCount,NotApplicableCount,NotInstalledCount,InstalledCount,FailedCount |
Export-Csv -Path mydata.csv -NoTypeInformation