I need to check if the column value from the first csv file, exists in any of the three other csv files and return a value into a new csv file, in order of precedence.
So if the username field from allStaff.csv exists in the list of usernames in the sessionVPNct.csv file, put the static text into the final csv file as 'VPN'. If it does not exist, check the next csv file: sessionCRXct.csv then put the static text 'CRX', if not check the last csv file: sessionTMSct.csv then put the static text: TM if not the put the static text 'none' into the final csv file.
I have four csv files as below:
1. allStaff.csv 2. VPN.csv 3. CRX.csv 4. TMS.csv
I have imported the csv files into variables as below:
$allUsers = Import-Csv -Path "C:\allStaff.csv"
$vpn = Import-Csv -Path "C:\VPN.csv" | Select-Object -ExpandProperty UserName
$crx = Import-Csv -Path "C:\CRX.csv" | Select-Object -ExpandProperty UserName
$tms = Import-Csv -Path "C:\TMS.csv" | Select-Object -ExpandProperty UserName
The $allUsers variable displays the following:
Firstname LastName Username Position Area --------- -------- -------- -------- ---- Joe Bloggs jbloggs Gardener Maintenance Jim Smith jsmith Accountant Finance Bob Seger bseger HR Advisor Human Resources Adam Boson aboson Customer Support IT Adele bree abree Payroll Finance
The $vpn variable displays the following:
Username -------- jbloggs jsmith
The $crx variable displays the following:
Username -------- jbloggs jsmith bseger
The $tms variable displays the following:
Username -------- jbloggs jsmith bseger aboson
Then I have the following line to start the result csv file
$result = $allUsers | Select-Object *,ConnectionMethod
Not quite sure how to do the final query, which I believe should be an if else loop to go through all rows in the $result variable, and check the other csv if the username field exists, then return the static text.
$result | Export-Csv -Path "C:\allStaffConnections.csv"
This is how I need the final allStaffConnections.csv file to be displayed.
Firstname LastName Username Position Area ConnectionMethod --------- -------- -------- -------- ---- -------------- Joe Bloggs jbloggs Gardener Maintenance VPN Jim Smith jsmith Accountant Finance VPN Bob Seger bseger HR Advisor Human Resources CRX Adam Boson aboson Customer Support IT TMS Adele bree abree Payroll Finance none
Am I on the right track with the below code?
$allUsers = Import-Csv -Path "C:\allStaff.csv"
$vpn = Import-Csv -Path "C:\VPN.csv" | Select-Object -ExpandProperty UserName
$crx = Import-Csv -Path "C:\CRX.csv" | Select-Object -ExpandProperty UserName
$tms = Import-Csv -Path "C:\TMS.csv" | Select-Object -ExpandProperty UserName
$vpnText = 'VPN'
$crxText = 'CRX'
$txsText = 'TMS'
$noneText = 'none'
$allUsersExtended = $allUsers | Select-Object *,ConnectionMethod
$results = $allUsersExtended.ForEach(
{
if($vpn -Contains $PSItem.UserName) {
# add $vpnText to ConnectionMethod column for that row in the $result
$PSItem.ConnectionMethod = $vpnText
}elseif($crx -Contains $PSItem.UserName) {
# add $crxText to ConnectionMethod column for that row in the $result
$PSItem.ConnectionMethod = $crxText
}elseif($tms -Contains $PSItem.UserName) {
# add $txsText to ConnectionMethod column for that row in the $result
$PSItem.ConnectionMethod = $tmsText
}else {
# add $noneText to ConnectionMethod column for that row in the $result
$PSItem.ConnectionMethod = $noteText
}
})
$results | Export-Csv -Path "C:\allStaffConnections.csv" -NoTypeInformation
This gives me an empty allStaffConnections.csv file.
I have run the code line by line and can get as far as:
$allUsersExtended = $allUsers | Select-Object *,ConnectionMethod
Which gives me the extra column "ConnectionMethod", but after running the loop, it gives me an empty allStaffConnections.csv file.
$AllUserslist, find any matches in the others based on the shared value, build a new[PSCusomtObject]with the combined results, and finally write that out to a new CSV file.Select-Objecton it since the way you defined that select throws out the other properties. keep them all. then, in the loop, use$Vpn.Where({$_.UserName -eq $CurrentUserName})or$Vpn - $CurrentUserName. i think the 2nd will work, but i am very confident that the 1st will work. [grin]-Containto-Contains. however, you haveJBloggsin 3 different connection type lists ... but your final report shows that there is only one connection type per account. how do you handle multiple matches?