2011-11-03 58 views
0

我有兩個不同的CSV。一個是全球團體,地方團體和系統的報告。另一個是用戶,登錄ID和全局組的列表。如何使用Powershell進行多個到多個CSV文件的合併?

正如您所看到的,全局AD組是常用列,並且在這兩個文件中都標記爲「成員」。成員中的一些項目實際上是本地用戶或域用戶。需要將本地用戶過濾掉,並且需要將域用戶移植到結果中。

什麼使這項任務困難的是,聯接是多對多。另外,我需要能夠輕鬆地向腳本添加新列。我已經知道如何獲取您創建的任何腳本的輸出,並根據需要對列進行重新排序。

理想情況下,每個CSV文件中的列應該無關緊要,腳本應該基於公共列將兩個文件中的所有列連接在一起。雖然,如果在調用函數時可以指定公共列,那將會很好。

所以樣本數據會看起來像:

$CSV1 
"Members","LocalGroup","System" 
"System1\local-user","Guests","System1" 
"adminaccounts","Administrators",System2" 
"adminaccounts","Auditors","System2" 
"System3\User1","Administrators","System3" 
"System3\Temp","Guests","System3" 
"System4\Temp","Guests","System4" 
"System5\Temp","Guests","System5" 
"Domain1\ngd","Users","System6" 
"Domain1\Group1","Administrators","System7" 
"Domain1\Group1","Administrators","System8" 
"Domain1\Group1","Administrators","System9" 
"Domain1\Group1","Administrators","System10" 
"Domain1\Group2","Administrators","System1" 
"Domain1\Group2","Administrators","System4" 
"Domain1\Group2","Administrators","System7" 
"Domain1\Group3","Administrators","System3" 
"Domain1\Group3","Administrators","System1" 
"Domain1\Group4","Administrators","System4" 
"Domain2\Group1","Administrators","System5" 
"Domain2\Group1","Administrators","System6" 
"Domain2\Group1","Administrators","System2" 

$CSV2 
"Members","Username","Logon ID" 
"Domain1\Group1","Pauline S. Trotter","pst" 
"Domain1\Group1","Caroline T. Gartner","ctg" 
"Domain1\Group1","Albert F. Hollis","afh" 
"Domain1\Group1","Todd C. Cope","tcc" 
"Domain1\Group1","Rebecca D. Bergman","rdb" 
"Domain1\Group1","Robert A. Wagner","raw" 
"Domain1\Group2","Daniel R. Dalton","drd" 
"Domain1\Group2","Christopher V. Alderbrook","cva" 
"Domain1\Group2","Brittany L. Stone","bls" 
"Domain1\Group2","Susan K. Holman","skh" 
"Domain1\Group2","Mark P. Bett","mpb" 
"Domain1\Group2","Francine U. Zen","fuz" 
"Domain1\Group2","Holly M. Fulton","hmf" 
"Domain1\Group3","Edgar X. Indiana","exi" 
"Domain1\Group3","John Q. Pullit","jqp" 
"Domain1\Group3","Victor E. Emmerson","vee" 
"Domain1\Group3","Robert A. Wagner","raw" 
"Domain1\Group3","Pauline S. Trotter","pst" 
"Domain1\Group4","Noreen G. Dulles","ngd" 
"Domain1\Group4","Lois H. Crantz","lhc" 
"Domain1\Group4","Brad F. Quilt","bfq" 
"Domain1\Group4","Zoren T. Maranda","ztm" 
"Domain1\Group4","William K. Entz","wke" 
"Domain1\Group4","Quils U. Naruz","qun" 
"Domain2\Group1","Zoren T. Maranda","ztm" 
"Domain2\Group1","Robert A. Wagner","raw" 
"Domain2\Group1","Holly M. Fulton","hmf" 
"Domain2\Group1","Brad F. Quilt","bfq" 
"Domain2\Group1","Pauline S. Trotter","pst" 
"Domain2\Group1","Noreen G. Dulles","ngd" 

在此先感謝您的幫助。

問候,

亞歷

下面是用樣本輸出一些簡化的數據:

$CSV1 
"Members","LocalGroup","System" 
"System1\local-user","Guests","System1" 
"adminaccounts","Administrators",System2" 
"adminaccounts","Auditors","System2" 
"System3\User1","Administrators","System3" 
"System3\Temp","Guests","System3" 
"System4\Temp","Guests","System4" 
"System5\Temp","Guests","System5" 
"Domain1\ngd","Users","System6" 
"Domain1\Group1","Administrators","System7" 
"Domain1\Group1","Administrators","System8" 
"Domain1\Group2","Administrators","System1" 
"Domain1\Group3","Administrators","System3" 
"Domain1\Group3","Administrators","System1" 
"Domain1\Group4","Administrators","System4" 
"Domain2\Group1","Administrators","System5" 

$CSV2 
"Members","Username","Logon ID" 
"Domain1\Group1","Pauline S. Trotter","pst" 
"Domain1\Group1","Caroline T. Gartner","ctg" 
"Domain1\Group1","Albert F. Hollis","afh" 
"Domain1\Group2","Daniel R. Dalton","drd" 
"Domain1\Group2","Christopher V. Alderbrook","cva" 
"Domain1\Group3","Pauline S. Trotter","pst" 
"Domain1\Group4","Noreen G. Dulles","ngd" 
"Domain1\Group4","Quils U. Naruz","qun" 
"Domain2\Group1","Zoren T. Maranda","ztm" 
"Domain2\Group1","Noreen G. Dulles","ngd" 

Output 
"Username","Logon ID","Members","LocalGroup","System" 
,,"System1\local-user","Guests","System1" 
,,"adminaccounts","Administrators",System2" 
,,"adminaccounts","Auditors","System2" 
,,"System3\User1","Administrators","System3" 
,,"System3\Temp","Guests","System3" 
,,"System4\Temp","Guests","System4" 
,,"System5\Temp","Guests","System5" 
"Pauline S. Trotter","pst","Domain1\Group1","Administrators","System7" 
"Pauline S. Trotter","pst","Domain1\Group1","Administrators","System8" 
"Pauline S. Trotter","pst","Domain1\Group3","Administrators","System3" 
"Pauline S. Trotter","pst","Domain1\Group3","Administrators","System1" 
"Caroline T. Gartner","ctg","Domain1\Group1","Administrators","System7" 
"Caroline T. Gartner","ctg","Domain1\Group1","Administrators","System8" 
"Albert F. Hollis","afh","Domain1\Group1","Administrators","System7" 
"Albert F. Hollis","afh","Domain1\Group1","Administrators","System8" 
"Daniel R. Dalton","drd","Domain1\Group2","Administrators","System1" 
"Christopher V. Alderbrook","cva","Domain1\Group2","Administrators","System1" 
"Noreen G. Dulles","ngd","Domain1\Group4","Administrators","System4" 
"Noreen G. Dulles","ngd","Domain2\Group1","Administrators","System5" 
"Quils U. Naruz","qun","Domain1\Group4","Administrators","System4" 
"Zoren T. Maranda","ztm","Domain2\Group1","Administrators","System5" 

回答

0

你可以使用Join-Collections script

$csv1 = import-csv csv1.csv 
$csv2 = import-csv csv2.csv 
$collection = Join-Collections -FirstCollection $csv1 -FirstJoinColumn Members -SecondCollection $csv2 -SecondJoinColumn Members 
$collection | export-csv result.csv 
+0

該腳本的更高版本不起作用,但我會嘗試此版本。 – ITBlogger