Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

PoswerShell - Compare 2 x CSV files, only if data matches in column 1 add all data from file 2 to file 1 or output collated results

$
0
0

OK, 1st the background...
I have been provided a list of users of a particular group.
We needed to know what computers (or IP's) the users are connected to...

So, using Shared Folders, I have exported a list of all users connected to H drives (everyone on the domain) and I need to match their computernames (or IP) to the specific list of users I have...

That gives me 2 x CSV files. 1 x master list of users, I list of all users PC or IP names.
I need to be able to match the usernames on the master list (File1) with the usernames on File2 and append the computernames and connected times to File1 omitting the usernames that are not required...

This is to determine what PC's the user is logged into and what site their computers are at.
I also need to capture if they are logged into more than one computer (info is contained in File2)

File1 Example:

UserFirstNameLastNameADSiteHDriveLocationHDriveLocationTSOffice2010FAT
XY123456BOBBUILDERLONDON \\SERVER\%USERNAME%\\SERVER\USERNAMETRUE
ABC09876JIMINYCRICKET CHICAGO \\SERVER\USERNAME
\\SERVER\USERNAMETRUE
ADD11223 SALLYMETLARRYTOKYO\\SERVER\USERNAME\\SERVER\USERNAMEFALSE

File2 Example:

UserComputer ConnectedIdle
XY123456 192.168.1.216 3 days 06:13:180:00:03
XY123456192.168.1.254 1 day 01:21:3023:08:39
WXY46810 PCNAMEXYZ  6:44:190:00:05



I have found a PS script which "might" do what I need it to, but I haven't been able to "tweak" it right - it's omitting the details from File2 - http://powergui.org/thread.jspa?threadID=19522
Also I can't figure out how to collect multiple computernames (that users are logged into) from File 2 and add this to File1

Working with Powershell, but happy to consider any other options (EG: Excel, Macros, VBS, etc)

Here's what I have got so far: (PS)

$File1 = Import-Csv "C:\Users.csv"
$File2 = Import-Csv "C:\Shares.csv"

$Output = @()
ForEach ($Item in $File1)
{$Result = $File2 | Where { $Item.User -eq $_.User }
$Output += New-Object PSObject -Property @{
USER = $Item.User
FirstName = $Item.FirstName
LastName = $Item.LastName
ADSite = $Item.ADSite
Computer = $_.Computer
Connected = $_.Connected
Idle = $_.Idle
Office2010FAT = $Item.Office2010FAT
HDriveLocation = $Item.Connected
HDriveLocationTS = $Item.Idle
}
}
$Output | Select User,FirstName,LastName,ADSite,Computer,Connected,Idle,Office2010FAT,HDriveLocation,HDriveLocationTS | Export-Csv C:\Users\a1033965\Desktop\result.csv -NoTypeInformation


However, the output I'm getting is missing the results from File2 and I NEED multiple computers listed if a user is logged into them:
Current Output:

USERFirstNameLastNameADSiteComputerConnected Idle Office2010FAT HDriveLocation HDriveLocationTS
XY123456BOBBUILDERLONDONTRUE
ABC09876JIMINYCRICKETCHICAGOTRUE
ADD11223 SALLYMETLARRYTOKYOTRUE


(TRUE is actually in the right column, under Office2010FAT as it should be - just doesn't look like it as I post this)

Any help with this would be much appreciated. Cheers


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>