I am a novice at programing and Powershell, so I am looking for some help to modify my current code.
I get an email report that used to come in the format of “Country Date/Time User” with multiple lines. The “User” field could contain one or many email addresses seperated by a space (example below). Once I highlighted and copied all of the lines, I would run the included script and it would accept input from the clipboard and then would lookup the users in our Active Directory.
France Fri Apr 6 04:05:54 2018 Ga01@domain.com Gr01@domain.com He01@domain.com Je01@domain.com br01@domain.com
The report format has changed and now comes as “Country Date/Time User” and then subsequent users are on their own line. I need to fix the routine so it will look for the additional users in the new format.
France Fri Apr 6 04:05:54 2018 Ga01@domain.com
Gr01@domain.com
He01@domain.com
Je01@domain.com
br01@domain.com
# Import from clipboard to $importUsers
$importUsers = Get-Clipboard
$Array1 = @()
$Array2 = @()
$Array3 = @()
foreach ($row in $importUsers) {
if ($row -eq ''){ break} # incase last line of paste is blank
$cnt = 0
$country = $row.Split("`t")[0]
$userField = $row.Split("`t")[2]
$user = $userField.split(" ")
$cnt = $user.count
for ($i = 0; $i -lt $cnt) {
$userSamAccount = $user[$i].split("@")[0]
$userInfo = get-aduser $userSamAccount
$userSamAccount # Remove "#" for troubleshooting
if ($userInfo -like "*OU=QMM_CO1_Objects*" -eq $True) {
write-host $user[$i].split("@")[0] "is a CO1 user"
$CO1user = $user[$i].split("@")[0]
$record = New-Object PSObject
Add-Member -InputObject $Record -NotePropertyName "CO1/Company3/CO2" -NotePropertyValue "CO1 User"
Add-Member -InputObject $Record -NotePropertyName SamAccountName -NotePropertyValue $CO1user
Add-Member -InputObject $Record -NotePropertyName Name -NotePropertyValue $userInfo.name
Add-Member -InputObject $Record -NotePropertyName LoginCountry -NotePropertyValue $country
$Array2 += $record
Clear-Variable Record
}
if ($userInfo -like "*OU=CO3*" -eq $True) {
$i
$cnt
write-host $user[$i].split("@")[0] "is a Company3 user"
$Company3user = $user[$i].split("@")[0]
$record = New-Object PSObject
Add-Member -InputObject $Record -NotePropertyName "CO1/Company3/CO2" -NotePropertyValue "Company3 User"
Add-Member -InputObject $Record -NotePropertyName SamAccountName -NotePropertyValue $Company3user
Add-Member -InputObject $Record -NotePropertyName Name -NotePropertyValue $userInfo.name
Add-Member -InputObject $Record -NotePropertyName LoginCountry -NotePropertyValue $country
$Array1 += $record
Clear-Variable Record
}
if ($userInfo -like "*OU=CO2,DC=oldcastle,DC=corp*" -eq $True) {
write-host $user[$i].split("@")[0] "is an CO2 user"
$CO2user = $user[$i].split("@")[0]
$record = New-Object PSObject
Add-Member -InputObject $Record -NotePropertyName "CO1/Company3/CO2" -NotePropertyValue "CO2 User"
Add-Member -InputObject $Record -NotePropertyName SamAccountName -NotePropertyValue $CO2user
Add-Member -InputObject $Record -NotePropertyName Name -NotePropertyValue $userInfo.name
Add-Member -InputObject $Record -NotePropertyName LoginCountry -NotePropertyValue $country
$Array3 += $record
Clear-Variable Record
}
$i = $i + 1
}
}
write-host "These are Company3 users:"
$Array1 | Sort-Object -property SamAccountName | Out-GridView
write-host "These are CO1 users: "
$Array2 | Sort-Object -property SamAccountName | Out-GridView
write-host "These are CO2 users: "
$Array3 | Sort-Object -property SamAccountName | Out-GridView
This is the first time I am posting here, so if I left something out or didn’t format something correctly, please advise.