2014-04-01 26 views
0

我需要使報告更易讀。有第三方工具可以檢索,如果一個特定的設置是真或假,它會檢查每個用戶一次在不同的域,然後將結果一個接一個地放在我的csv的單個列中。 我需要能夠根據該用戶的域將輸出行分成不同的csv列。Windows Powershell根據字符串內容將單個柱子格式化爲幾個

$var1 = "C:\location\filewithallsettings.csv" 
. C:\mytool.exe\ show setting > $var1 

然後只檢索「TRUE」的:

Select-String -Path C:\filewithallsettings.csv -Pattern "enabled:true" |` 
select-object -property Line| 
Export-Csv -Path C:\filewithTRUEsettings.csv -Encoding UTF8 -Delimiter ";" ` 
-NoTypeInformation 

輸出看起來像這樣一行:

[email protected] has this setting enabled:true 
[email protected] has this setting enabled:true 
[email protected] has this setting enabled:true 

現在我不能讓過去的這一點,但我需要以某種方式分析第二個文件字符串,並將其中的每一行放入另一個文件中的特定列中,具體取決於一個用戶的域。我再次假設選擇字符串或$ varname.contains(),但我只是不知道如何。 我的目標是像這樣的文件:

DOMAIN ANOTHERDOMAIN THIRDDOMAIN andsoon 
    User1 User2   User3 

記得......我的工具只給一列! 預先感謝您!

編輯 我將filewithallsettings.csv的內容複製到這篇文章中。雙重用戶不相關,因爲在一個域上不能有兩個相同的地址。一個真正的用戶在不同的領域也是不相關的,我知道有一些和哪些。

[email protected] has this setting enabled:true 
[email protected] has this setting enabled:false 
[email protected] has this setting enabled:true 
[email protected] has this setting enabled:false 
[email protected] has this setting enabled:true 
[email protected] has this setting enabled:false 
[email protected] has this setting enabled:true 
[email protected] has this setting enabled:true 
[email protected] has this setting enabled:true 
[email protected] has this setting enabled:true 
[email protected] has this setting enabled:true 
[email protected] has this setting enabled:true 

沒有標題只是一個在另一個下面的行。預期的產出就是這樣,因爲我無法改變工具的工作方式。但是我認爲我可以創建另一個只有TRUE的csv,這是唯一需要檢查的。好了,這我被做上述選擇串做,我得到的文件相同的風格,這次沒有FALSEs和頭「行」

Line ` 
[email protected] has this setting enabled:true 
[email protected] has this setting enabled:true 
[email protected] has this setting enabled:true 
[email protected] has this setting enabled:true 
[email protected] has this setting enabled:true 
[email protected] has this setting enabled:true 
[email protected] has this setting enabled:true 
[email protected] has this setting enabled:true 
[email protected] has this setting enabled:true 
[email protected] has this setting enabled:true 

我想在每一行進行搜索,並根據所域有它應該把整行放在一個csv在所述域的列標題下,就像我的第一篇文章。

+0

你能否給出更多樣本輸入和輸出場景?例如,如果您在同一個域中擁有多個用戶,您想要什麼?有兩個用戶的兩個域和一個用戶的域?現在回答這個問題需要對這些問題作出假設,所以如果你能舉出更多的例子,它可能會爲每個人節省時間。也許還有一個示例輸入文件(您稱爲'filewithallsettings.csv'的文件)可能是有益的。 –

+0

當然我嘗試: 我的工具向每個單一用戶發送一個呼叫給每個單個域,並檢索單個設置「enabled:false」或「enabled:true」(我不知道哪個設置,我很抱歉)在這樣的CSV一條線: 用戶[email protected]已啓用XXXX:真 用戶[email protected]已啓用XXXX:假 用戶[email protected]已啓用XXXX:假 用戶YYY @ firstdomain .org已啓用xxxx:真 用戶[email protected]啓用了xxxx:true – Obre1

+0

是的我確定嘗試: 我的工具向每個單個用戶發送一個呼叫給每個單個域,並檢索單個設置「enabled:false」或「啓用:真」(我不知道哪個設置,我很抱歉),並把它作爲一行中的一行csv是這樣的: 用戶[email protected]已啓用xxxx:true 用戶[email protected]已啓用xxxx:false 用戶yyz @ firstdomain。org已啓用xxxx:false 用戶[email protected]啓用了xxxx:true 用戶[email protected]啓用了xxxx:true 並且這種情況一直持續到每個域有100到2000個用戶的第11個域 我需要fomrat它就像上面 – Obre1

回答

0

首先,我會說,感覺好像csv對於這個數據來說是一種奇怪的格式,因爲單行上每列的數據與同一行上的其他數據沒有任何關係。要讓PowerShell cmdlet Export-Csv創建這樣一個CSV文件,我們必須創建對象,其中每個對象的不同屬性與同一對象上的其他屬性沒有任何關係。

這就是說,這是一個非常快速的解決方案(第一個來到我的頭上,這意味着很可能有很多更好的方法來解決這個問題),您可以使用和更改自己的喜好:

$content = @" 
[email protected] has this setting enabled:true 
[email protected] has this setting enabled:false 
[email protected] has this setting enabled:true 
[email protected] has this setting enabled:false 
[email protected] has this setting enabled:true 
[email protected] has this setting enabled:false 
[email protected] has this setting enabled:true 
[email protected] has this setting enabled:true 
[email protected] has this setting enabled:true 
[email protected] has this setting enabled:true 
[email protected] has this setting enabled:true 
[email protected] has this setting enabled:true 
"@ 

$fileContent = $content -split [Environment]::Newline 
#In your scenario, the above line should be replaced with the next line 
#$fileContent = Get-Content C:\filewithallsettings.csv -Encoding UTF8 

$usersByDomain = 
    $content -split [Environment]::Newline | 
    #Start by selecting only the lines you want, using a regex which captures the user info 
    Select-String -Pattern "(.*)@([^ ]*) .*enabled:true" | 
    #Then convert these regex matches into object, making them easier to work with 
    Foreach { 
     New-Object PSObject -Property @{ 
      Username = $_.Matches.Groups[1].Value 
      Domain = $_.Matches.Groups[2].Value 
     } 
    } | 
    #Then group them by the domain name 
    Group Domain 

$longestUserListCount = ($usersByDomain | Measure -Property Count -Maximum).Maximum 

#Since we want to create a very strange format where the properties on each object does not have 
#anything in common with the other properties on the same object, we want to turn the groups 
#sideways (to get domains as properties and each object ("line") to hold usernames for the 
#aforementioned properties. 
$result = @() 
for($index = 0; $index -lt $longestUserListCount; $index++) 
{ 
    $properties = @{} 
    foreach($domain in $usersByDomain) 
    { 
     #If the domain has this many users, add the username as the property value, otherwise add 
     #an empty string to get all lines to have the same properties 
     if ($index -lt $domain.Count) 
     { 
      $properties[$domain.name] = $domain.Group[$index].UserName 
     } 
     else 
     { 
      $properties[$domain.name] = "" 
     } 
    } 
    $result += New-Object PSObject -Property $properties 
} 

$result | Format-Table * 
$result | convertto-csv -NoTypeInformation 
#If you want to export it to a csv file instead, just remove the above two lines and uncomment the next 
#Export-Csv c:\output.csv -NoTypeInformation -Encoding UTF8 

執行上面的腳本會產生以下的輸出:

對於Format-Table

domain.org     anotherdomain.org   thirddomain.org   
----------     -----------------   ---------------   
User1      User1      User1      
User3      User3      User1      
          User4      User2      
                 User3      

對於ConvertTo-Csv

"domain.org","anotherdomain.org","thirddomain.org" 
"User1","User1","User1" 
"User3","User3","User1" 
"","User4","User2" 
"","","User3" 
+0

非常感謝!我會嘗試一下並重新發布我的結果。注意:CSV格式不是唯一的,它只是我的第三方工具只能輸出文本字符,並且通常可以直接從命令提示符運行,也可以顯示結果。 – Obre1

0

對不起,但我非常非常新的節目一般。再次感謝您的迴應,我試了一下,但它產生了奇怪的結果,我想我沒有提供正確的信息。

我的預期輸出CSV中的列是在特定於域的電子郵件地址後自行命名的。

UK DE FR IT 

第三方工具給了我下面的信息,我可以保存到一個變量,然後做出variabel我的第一個CSV

[email protected] has this setting enabled:true 
[email protected] has this setting enabled:false 
[email protected] has this setting enabled:false 
[email protected] has this setting enabled:false 
[email protected] has this setting enabled:true 
[email protected] has this setting enabled:true 
this goes on until the last (11.) domain and approx. 5000 Users 

再次:首先CSV沒有列標題,它也可能是一個正常的.txt文件

現在我需要精確匹配每一個上述線路的最終預期輸出CSV這將是這樣的:

UK              DE 
--              -- 
[email protected] has this setting enabled:true [email protected] has this setting enabled:true 
                 [email protected] has this setting enabled:true 

像取決於@在我的第一個輸出的每一行後的所有內容,它將完整的行放在特定列下的預期輸出csv中。首先檢查「enabled:true」是否存在,如果是,那麼檢查域名是什麼,如果域名是「DomainNameUK」,那麼把你剛剛檢查過的完整行放到C:\ expectedOutput.csv下面的列中

相關問題