This topic contains 6 replies, has 4 voices, and was last updated by
January 17, 2014 at 8:33 am #12609
January 20, 2014 at 7:08 am #12621ParticipantPoints: 0Rank: Member
You could try to let Sort-Object -Unique do the work for you. However without a sample of the CSV it's hard for me to see what you're needing. I understand if you can not post the actual user IDs that's good IT security but if you could sample the CSV and post a cleansed version that would help me to be able to find a solution.
January 20, 2014 at 7:53 am #12622ParticipantPoints: 0Rank: Member
Thanks Vern. Here's a few sample linesof the CSV with for computers with my userID's...it's basically an export from a Symantec report that shows latest user logged in.
Computer Name,Computer Domain Name,Current login domain,Current User,Operating System
COV7P01-019,am.tsacorp.com,AM.TSACORP.COM,carnesm,Windows 7 Ultimate Edition
OMA7P03-009,am.tsacorp.com,AM.TSACORP.COM,carnesm,Windows 7 Ultimate Edition
NRC7P06-009,am.tsacorp.com,AM.TSACORP.COM,carnesma,Windows XP Professional
NRC7P05-001,am.tsacorp.com,AM.TSACORP.COM,carnesm,Windows 7 Ultimate Edition
COV7P07-TEST2,am.tsacorp.com,AM.TSACORP.COM,carnesma,Windows 7 Ultimate Edition
The Text file that I'm using for get-content would just have the names of computers that I'm looking for the information on (vsphere info, last login, and username)
Happy to provide any add'l information that is needed.
January 20, 2014 at 2:33 pm #12633MemberPoints: 0Rank: Member
The PowerShell team made a blog post on a similar topic. It was regarding joining CSV files, rather than one CSV and one TXT file, but you can accomplish the same thing by changing this line:
$value1 = get-content C:\List\List1.txt # to this: $value1 = Import-Csv -Path C:\List\List1.txt -Header 'ComputerName'
The blog post is here: http://blogs.msdn.com/b/powershell/archive/2012/07/13/join-object.aspx
January 26, 2014 at 10:33 am #12756ParticipantPoints: 0Rank: Member
Oh, that's weird, because I just wrote this in the last two days and posted the article this morning: http://www.powershelladmin.com/wiki/An_Advanced_Cmdlet_to_Merge_Csv_Files_in_PowerShell
I swear I'm psychic sometimes.
I hadn't seen this before doing that. It works with custom PowerShell objects (like the ones created from Import-Csv), so I might as well have named it Merge-Object, or, indeed, Join-Object, I guess. I didn't read the article on MSDN thoroughly yet, but it looks like similar functionality. I bet mine sucks in comparison? 🙁
January 26, 2014 at 10:41 am #12757MemberPoints: 0Rank: Member
No worries Joakim, I'm guilty of reinventing the wheel almost every time I sit down to write a script. I get a fair amount of enjoyment and satisfaction from producing my own code, anyway. Also, sometimes I feel like it takes so much time to search, read, understand, and potentially debug and fix the code that I find online, I'd have had it done faster if I just wrote it from scratch in the first place.
January 26, 2014 at 12:48 pm #12759ParticipantPoints: 0Rank: Member
I have to agree. My threshold for using someone else's code is way too high, I think, though; I probably need to work on changing that. For people like you and me, who are probably capable of writing most of the code we need ourselves, it's often more "comfortable" writing it yourself. I'm not entirely sure, but I tend to read 100-line scripts carefully before considering using them sometimes, so... I know it must be similar for a lot of people.
For those who aren't capable, finding helpful, pre-written functions/cmdlets is probably very nice. Writing this Merge-Csv cmdlet was painful enough for me not to want to do it over anytime soon, so I hope someone finds some use in it. I'm likely to use it myself later.
I skimmed through parts of the code of the PowerShell team's Join-Object cmdlet and they have some filtering in place in the cmdlet ([ValidateSet("AllInLeft", "OnlyIfInBoth", "AllInBoth", "AllInRight")]). This seems to indicate they only support two files, and I just looked to see a "-Left" and "-Right" argument which further indicate this. I considered imposing that limitation, but aimed a bit higher. 🙂 I made the number of CSV files arbitrary (you can have three or more) and made such filtering optional afterwards by just using Select-Object and property titles, as demonstrated briefly in the article. A bit less dynamic in that respect. Shrug. Design decisions are sometimes the hardest part.
The topic ‘Linking values from imported CSV’ is closed to new replies.