Skip to content

Instantly share code, notes, and snippets.

@midnightfreddie
Last active January 7, 2016 03:21
Show Gist options
  • Save midnightfreddie/facd15b3eb945cf8a6b5 to your computer and use it in GitHub Desktop.
Save midnightfreddie/facd15b3eb945cf8a6b5 to your computer and use it in GitHub Desktop.
param (
[Parameter(Mandatory=$True,Position=1)] $UserName,
$SharePath = "\\vsnhq1coap01mes\HomeMDB",
$CsvsToSearch = 1
)
# From CSV which lists Exchange server for each store,
# Create a lookup hash table
$StoreServer = @{}
Import-Csv -Path "store-server.csv" |
ForEach-Object { $StoreServer[$_.Store] = $_.Server }
if (Test-Path $SharePath) {
Get-ChildItem -Path $SharePath |
Sort CreationTime -Descending |
Select -First $CsvsToSearch -ExpandProperty FullName |
ForEach-Object {
$CSVFilePath = $_
$MiniCsv = New-Object System.Collections.ArrayList
# Get the CSV header row
$MiniCsv.Add((Get-Content -Path $CSVFilePath -TotalCount 1)) | Out-Null
# Add any rows matching $UserName
Get-Content $CSVFilePath |
Select-String -SimpleMatch $UserName |
ForEach-Object { $MiniCsv.Add($_) | Out-Null }
$CsvData = ($MiniCsv | Out-String | ConvertFrom-Csv)
$CSVData |
Where-Object {$_."user id" -eq $UserName} |
Select-Object "User ID",Mail,Store, @{Name = "Exchange Server"; Expression = {$StoreServer[$_.Store]}}
}
}
Store Server
DB001 RSNHQ1COEX01MBR
DB002 RSNHQ1COEX01MBR
DB003 RSNHQ1COEX01MBR
DB004 RSNHQ1COEX01MBR
DB005 RSNHQ1COEX01MBR
DB006 RSNHQ1COEX01MBR
DB007 RSNHQ1COEX01MBR
DB008 RSNHQ1COEX01MBR
DB009 RSNHQ1COEX01MBR
DB010 RSNHQ1COEX01MBR
DB011 RSNHQ1COEX01MBR
DB012 RSNHQ1COEX02MBR
DB013 RSNHQ1COEX02MBR
DB014 RSNHQ1COEX02MBR
DB015 RSNHQ1COEX02MBR
DB016 RSNHQ1COEX02MBR
DB017 RSNHQ1COEX02MBR
DB018 RSNHQ1COEX02MBR
DB019 RSNHQ1COEX02MBR
DB020 RSNHQ1COEX02MBR
DB021 RSNHQ1COEX02MBR
DB022 RSNHQ1COEX02MBR
DB023 RSNHQ1COEX03MBR
DB024 RSNHQ1COEX03MBR
DB025 RSNHQ1COEX03MBR
DB026 RSNHQ1COEX03MBR
DB027 RSNHQ1COEX03MBR
DB028 RSNHQ1COEX03MBR
DB029 RSNHQ1COEX03MBR
DB030 RSNHQ1COEX03MBR
DB031 RSNHQ1COEX03MBR
DB032 RSNHQ1COEX03MBR
DB033 RSNHQ1COEX03MBR
DB034 RSNHQ1COEX04MBR
DB035 RSNHQ1COEX04MBR
DB036 RSNHQ1COEX04MBR
DB037 RSNHQ1COEX04MBR
DB038 RSNHQ1COEX04MBR
DB039 RSNHQ1COEX04MBR
DB040 RSNHQ1COEX04MBR
DB041 RSNHQ1COEX04MBR
DB042 RSNHQ1COEX04MBR
DB043 RSNHQ1COEX04MBR
DB044 RSNHQ1COEX04MBR
DB045 RSGRL1COEX01MBR
DB046 RSGRL1COEX01MBR
DB047 RSGRL1COEX01MBR
DB048 RSGRL1COEX01MBR
DB049 RSGRL1COEX01MBR
DB050 RSGRL1COEX01MBR
DB051 RSGRL1COEX01MBR
DB052 RSGRL1COEX01MBR
DB053 RSGRL1COEX01MBR
DB054 RSGRL1COEX01MBR
DB055 RSGRL1COEX01MBR
DB056 RSGRL1COEX02MBR
DB057 RSGRL1COEX02MBR
DB058 RSGRL1COEX02MBR
DB059 RSGRL1COEX02MBR
DB060 RSGRL1COEX02MBR
DB061 RSGRL1COEX02MBR
DB062 RSGRL1COEX02MBR
DB063 RSGRL1COEX02MBR
DB064 RSGRL1COEX02MBR
DB065 RSGRL1COEX02MBR
DB066 RSGRL1COEX02MBR
DB067 RSGRL1COEX03MBR
DB068 RSGRL1COEX03MBR
DB069 RSGRL1COEX03MBR
DB070 RSGRL1COEX03MBR
DB071 RSGRL1COEX03MBR
DB072 RSGRL1COEX03MBR
DB073 RSGRL1COEX03MBR
DB074 RSGRL1COEX03MBR
DB075 RSGRL1COEX03MBR
DB076 RSGRL1COEX03MBR
DB077 RSGRL1COEX03MBR
DB078 RSGRL1COEX04MBR
DB079 RSGRL1COEX04MBR
DB080 RSGRL1COEX04MBR
DB081 RSGRL1COEX04MBR
DB082 RSGRL1COEX04MBR
DB083 RSGRL1COEX04MBR
DB084 RSGRL1COEX04MBR
DB085 RSGRL1COEX04MBR
DB086 RSGRL1COEX04MBR
DB087 RSGRL1COEX04MBR
DB088 RSGRL1COEX04MBR
PDB01 RSNHQ1COEX01MBR
PDB02 RSGRL1COEX01MBR
Training RSNHQ1COEX03MBR
@midnightfreddie
Copy link
Author

This script hurt me the last time around. Here is how I might have done it.

First, I put the store/server information into CSV format which really should probably be a separate file. Then after importing or converting the csv I make a hash out of it so $StoreServer["storename"] is the Exchange server which makes our logic much easier.

Then I saw my comment from the previous thread about 40k-line CSVs and /u/mystikphish 's Select-String suggestion and took a page from his/her book. First I capture the header row of the csv into an arraylist, then I use Select-String to capture any lines containing the username into the arraylist. Then I pipe the arraylist into Out-String and then to ConvertFrom-Csv, so I'm not converting the entire CSV to search for presumably one line.

Then instead of parsing the data more than once I just select the items desired plus a calculated field using the hash store/server lookup.

Also, parameters. Call the script with -CsvsToSearch 2 to search the two most recent CSVs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment