Windows Shell script:CSVDEでエクスポートされたObjectSIDを実際のSIDに変換する

少し古い実行モジュールの利用に関するお話です。
Active Directoryから情報をエクスポートする際にCSVDE.exe(CSV Data Exchange)が利用される時があります。これには多くの利点がありますが出力されるObjectSIDを実際のSIDに可読するためには変換する必要があります。今回はその変換プロセスを簡単に行うためのスクリプトを紹介します。
Microsoftもサポートしてくれないのでなかなか情報が無いんですよね、、、

CSVDEについて

CSVDE (Comma Separated Value Data Exchange) は、Active DirectoryオブジェクトをCSVフォーマットでインポートおよびエクスポートするWindowsコマンドラインユーティリティです。このユーティリティは主にバルク操作に使用され、ユーザー、グループ、コンピューターなどのオブジェクトを一括でエクスポートまたはインポートすることができます。

1. 変数の準備

作業に必要な変数を準備します。
この段階で「作業ディレクトリ」、「タイムスタンプ」、「ホスト名」、「ユーザ名」などの情報を設定します。

$csvchange_work = “¥¥hogehoge¥dir¥”
$timestamp_str = (Get-Date).ToString(“yyyyMMddHHmmss”)
$hostname_str = $Env:COMPUTERNAME
$username_str = $Env:USERNAME
$csvchange_work_file = $csvchange_work+$timestamp_str+”_”+$hostname_str+”_”+$username_str+”.temp”

2. CSVファイルの選択

CSVDE.exeを利用しエクスポートしたCSVファイルを選択できるダイアログボックスを表示します。

Add-Type -AssemblyName System.Windows.Forms
$DialogPath = New-Object System.Windows.Forms.OpenFileDialog
$DialogPath.Filter = “CSV file|*.csv”
$DialogPath.Title = “Select the CSV file you exported with CSV Data Exchange.”
if($DialogPath.showDialog() -eq ‘OK’){
	$arg_DialogPath = $DialogPath.FileName
} else {
	[System.Windows.forms.messagebox]::Show(“No file selection.”)
	exit
}
$arg_filename = [System.IO.Path]::GetFileNameWithoutExtention($arg_DialogPath)
$arg_filepath = [System.IO.Path]::GetDirectoryName($arg_DialogPath)
$output_filename = $arg_filepath+”¥”+$arg_filename+”_”+$timestamp_str+”.csv”

3. ObjectSIDからSIDへの変換

選択されたCSVファイルからObjectSIDを取り出し可読可能なSIDに変換します。

Write-host “Conversion from objectSid to SID started.”
$csvde_imp = Import-Csv -Path $arg_DialogPath
foreach($csvde_sid_tmp in $csvde_imp.objectSid){
	$csvde_sid = $csvde_sid_tmp -replace “‘”,”” -replace “X”,””
	if([string]::IsNullOrEmpty($csvde_sid))
	{
		continue
	}
	try
	{
		$bytes = [byte[]]@(
			for ($i = 0; $i -lt $csvde_sid.Length - 1; $i += 2)
			{
				[byte]::Parse($csvde_sid.Substring($i, 2), [System.Globalization.NumberStyles]::AllowHexSpecifier)
			}
		)
		$sid = New-Object System.Security.Principal.SecurityIdentifier($bytes, 0)
		write-output $csvde_sid_tmp’,’$sid | out-file -FilePath $csvchange_work_file -Append
	}
	catch
	{
		throw “String $csvde_sid is unknown.”
	}
}
Write-host “Conversion from objectSid to SID completed.”

4. 2つのCSVファイルを結合して出力

変換したSIDと元のCSVファイルの他の情報を結合し新しいCSVファイルとして出力します。

$change_sid = Import-Csv -Header “objectSid”,”SID” -path $csvchange_work_file
$outPutDatas = New-Object System.Collections.ArrayList
foreach ($csvde_imp_tmp in $csvde_imp) {
	foreach ($change_sid_tmp in $change_sid) {
		if(($change_sid_tmp.objectSid) -eq ($csvde_imp_tmp.objectSid)){
			$outPutData_tmp = New-Object psobject | Select-object objectSid,SID,name,mail,manager,memberOf,member,”member;range=0-1499”
			$outPutData_tmp.objectSid += $csvde_imp_tmp.objectSid
			$outPutData_tmp.SID += $change_sid_tmp.SID
			$outPutData_tmp.name += csvde_imp_tmp.name
			$outPutData_tmp.mail += $csvde_imp_tmp.mail
			$outPutData_tmp.manager += $csvde_imp_tmp.manager
			$outPutData_tmp.memberOf += $csvde_imp_tmp.memberOf
			$outPutData_tmp.member += $csvde_imp_tmp.member
			$outPutData_tmp.”member;range=0-1499” += $csvde_imp_tmp.”member;range=0-1499”
			[void]$outPutDatas.Add(outPutData_tmp)
		}
	}
}
$outPutDatas | Export-Csv -path $output_filename -NoTypeInformation
Write-host “Output file generated.”
Remove-item $csvchange_work_file

まとめ

このスクリプトはCSVDEでエクスポートされたデータのObjectSIDを人間が読み取れる形式のSIDに変換するプロセスを自動化するツールです。変換後のデータを利用することで、さらなる分析や管理作業を効率的に進めることができます。

このコードはカスタマイズしてご使用いただけますがテスト環境で十分にテストした後本番環境での使用をお勧めします。

タイトルとURLをコピーしました