2016-05-13 50 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