3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PowerShellでJoin-Objectを使用せず配列同士を結合する

Last updated at Posted at 2021-12-23

TechCommit Advent Calendar 2021 24日目も担当しますRyotaです!
よろしくお願いいたします。

はじめに

O365系の処理をする際に、O365等からデータをダウンロードする関係上、PowerShellでプログラムを組むことがよくあります。
配列と配列を比較して結合する機会も多く発生します。Join-Objectを使用することで、配列と配列の結合ができることが分かったのですが、モジュールを追加でインストールする必要がある模様。あまりモジュールを追加でインストールしたくなかったので、PowerShellの標準機能で配列同士を結合することにしました。
PowerShellで配列同士を結合するスクリプトをご紹介します。

環境

Windows 10
PowerShell Version 5.1.19041.1320

入力CSV(sample)

UserLoginInfo.csv
"mailaddress","username"
"hoge1@example.co.jp","Hoge, Hoge1"
"fuga1@example.co.jp","Fuga, Fuga1"
"hoge2@example.co.jp","Hoge, Hoge2"
"fuga2@example.co.jp","Fuga, Fuga2"
"hoge3@example.co.jp","Hoge, Hoge3"
"fuga3@example.co.jp","Fuga, Fuga3"
"hoge4@example.co.jp","Hoge, Hoge4"
"fuga4@example.co.jp","Fuga, Fuga4"
UserChara.csv
"mailaddress","height","weight","性別"
"hoge1@example.co.jp","169cm","60kg","man"
"fuga1@example.co.jp","182cm","70kg","man"
"hoge2@example.co.jp","160cm","43kg","woman"
"fuga2@example.co.jp","158cm","40kg","woman"
"hoge3@example.co.jp","173cm","65kg","man"
"fuga3@example.co.jp","179cm","45kg","man"
"hoge4@example.co.jp","162cm","70kg","woman"
"fuga4@example.co.jp","190cm","100kg","man"
"hoge5@example.co.jp","175cm","30kg","man"
"fuga5@example.co.jp","178cm","70kg","man"
"hoge6@example.co.jp","168cm","45kg","woman"
"fuga6@example.co.jp","180cm","75kg","man"

出力CSV(sample)

UserInfo_Join_data_yyyyMMddhhmmss.csv
"mailaddress","username","height","weight","性別"
"hoge1@example.co.jp","Hoge, Hoge1","169cm","60kg","man"
"fuga1@example.co.jp","Fuga, Fuga1","182cm","70kg","man"
"hoge2@example.co.jp","Hoge, Hoge2","160cm","43kg","woman"
"fuga2@example.co.jp","Fuga, Fuga2","158cm","40kg","woman"
"hoge3@example.co.jp","Hoge, Hoge3","173cm","65kg","man"
"fuga3@example.co.jp","Fuga, Fuga3","179cm","45kg","man"
"hoge4@example.co.jp","Hoge, Hoge4","162cm","70kg","woman"
"fuga4@example.co.jp","Fuga, Fuga4","190cm","100kg","man"

配列同士を結合するPowerShellスクリプト

array_join.ps1

#結合する配列(CSV)の読み込み
$LoginInfos = Import-Csv ".\UserLoginInfo.csv"
$Charas = Import-Csv ".\UserChara.csv"

#出力する配列の定義
$OutPutDatas = New-Object System.Collections.ArrayList
$FileaddName = (Get-Date).ToString("yyyyMMddHHmmss")

#2つの配列を順に読み込む
foreach ($LoginInfo in $LoginInfos) {
    foreach ($Chara in $Charas){
        #2つの配列のキー(メールアドレス)を比較
        if(($Chara.mailaddress) -eq ($LoginInfo.mailaddress)){
            $OutPutData = New-Object PSObject | Select-Object mailaddress,username,height,weight,"性別"
            #UserLoginInfo.csvから取得したデータ(メールアドレスとユーザー名)を出力配列に追加
            $OutPutData.mailaddress += $LoginInfo.mailaddress
            $OutPutData.username += $LoginInfo.username
            #UserChara.csvから取得したデータ(身長、体重、性別)を出力配列に追加
            $OutPutData.height += $Chara.height
            $OutPutData.weight += $Chara.weight
            $OutPutData.("性別") += $Chara.("性別")
            [void]$OutPutDatas.Add($OutPutData) 
        }
    }
    
}

#UserInfo_Join_data_yyyyMMddhhmmss.csvとして出力
$OutputFile = ".\UserInfo_Join_data_" + $FileaddName + ".csv"
$OutPutDatas | Export-Csv $OutputFile -Encoding UTF8 -NoTypeInformation

参考になれば幸いです。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?