2016-05-13 93 views
0

我想通过Powershell在WSUS API中运行一个查询,输出计算机名称,需要的补丁等,然后我需要将其注入到一个“日志”文件中,然后将它读入Splunk中我们可以使仪表盘等Powershell将表转换为数组

我当前的代码是

$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 
+0

因此,您所需要做的只是删除第一个示例的'@ {'和'}'? –

回答

0

看起来你只是想删除前导@{和拖尾},你可以用这样的正则表达式做:

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

但是,使Select-String将您的对象转换为字符串表示是一种导出数据的不好方法。 Splunk可以读取CSV文件,因此我建议使用该文件(并且还使用实时属性来记录日志)。例如:

$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