SeveredTRUTH
SeveredTRUTH

Reputation: 73

Export Two Variables as Two Columns to CSV File

I have created two variables that pick multiple values from a particular file. Here is the part of my script for that:

#Identifying the AccountList.ain File Path
$AccountListPath = "$DataPATH\ADM\AccountList.ain"

#Create Variable for User Description & User ID
[xml]$UserInfoPath = gc $AccountListPath
$UserDescString = $UserInfoPath.AccountList.UserAccount.description
$UserInfoString = $UserInfoPath.AccountList.UserAccount.UserInfo.username

#Create User Description List & User Info (User ID) List
$UserDescObject = $UserDescString | Select-Object @{Name='Description';Expression={$_}}
$UserInfoObject = $UserInfoString | Select-Object @{Name='UserID';Expression={$_}}

These are the results for the $UserDescObject variable: enter image description here

These are the results for the $UserInfoObject variable:

enter image description here

I am trying export to csv the $UserDescObject variable as column 1 and $UserInfoObject as column 2.

I tried a two different methods (both failed for me).

Method #1: Export to CSV $UserDescObject Followed by Appending $UserInfoObject

This is the additional portion I added to the script above:

#Identifying the AccountList.ain File Path
$AccountListPath = "$DataPATH\ADM\AccountList.ain"

#Create Variable for User Description & User ID
[xml]$UserInfoPath = gc $AccountListPath
$UserDescString = $UserInfoPath.AccountList.UserAccount.description
$UserInfoString = $UserInfoPath.AccountList.UserAccount.UserInfo.username

#Export to CSV
$UserDescObject = $UserDescString | Select-Object @{Name='Description';Expression={$_}}
$UserDescObject | Export-Csv .\LDAPCrossWalkTable.csv -Force -NoType
$UserInfoObject = $UserInfoString | Select-Object @{Name='UserID';Expression={$_}}
$UserInfoObject | Export-Csv .\LDAPCrossWalkTable.csv -Append -NoTypeInformation

This is the error I see after running the script: enter image description here

Method #2: Export to CSV $UserDescObject, Import Created CSV, and Appending $UserInfoObject

#Identifying the AccountList.ain File Path
$AccountListPath = "$DataPATH\ADM\AccountList.ain"

#Create Variable for User Description & User ID
[xml]$UserInfoPath = gc $AccountListPath
$UserDescString = $UserInfoPath.AccountList.UserAccount.description
$UserInfoString = $UserInfoPath.AccountList.UserAccount.UserInfo.username

#Create User Description List & User Info (User ID) List
$UserDescObject = $UserDescString | Select-Object @{Name='Description';Expression={$_}}
$UserInfoObject = $UserInfoString | Select-Object @{Name='UserID';Expression={$_}}

#Export User Description & User Info (User ID) Lists to CSV
$UserDescObject | Export-Csv .\LDAPCrossWalkTable.csv -Force -NoType
$ImportCSV = Import-CSV .\LDAPCrossWalkTable.csv
$ImportCSV | Select-Object *,@{Name='UserInfo';Expression={$UserInfoObject}} | Export-Csv .\LDAPCrossWalkTable.csv -NoTypeInformation

I was almost able to get both columns to display correctly. I received two columns. However all lines of text in $UserInfoObject was added to each row in the CSV file rather than broken apart properly: enter image description here

This is what I want displayed in the csv file:

enter image description here

Any input is greatly appreciated!!

Upvotes: 0

Views: 253

Answers (1)

SeveredTRUTH
SeveredTRUTH

Reputation: 73

I was able to resolve the issue buy using an array. Here is the script:

#Identifying the AccountList.ain File Path
$AccountListPath = "$DataPATH\ADM\AccountList.ain"

#Create Variable for User Description & User ID
[xml]$UserInfoPath = gc $AccountListPath
$UserDescString = $UserInfoPath.AccountList.UserAccount.description
$UserInfoString = $UserInfoPath.AccountList.UserAccount.UserInfo.username

#Combine Description & Info as well as Export to CSV
$arr = @()
for ($i = 0; $i -lt $UserDescString.length; $i++) {
    $arr += [pscustomobject]@{Descripton=$UserDescString[$i];UserID=$UserInfoString[$i]}
}
$arr | Export-Csv .\LDAPCrossWalkTable.csv -Force -NoTypeInformation

Upvotes: 1

Related Questions