2011-11-03 18 views
0

私は2種類のCSVを持っています。 1つは、グローバルグループ、ローカルグループ、およびシステムのレポートです。もう1つは、ユーザー、ログオンID、グローバルグループの一覧です。Powershellで2つの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