Additions to fix script due to Input Format Changes

This topic contains 2 replies, has 2 voices, and was last updated by  J Marquis 4 days, 2 hours ago.

  • Author
    Posts
  • #98857

    J Marquis
    Participant

    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.

  • #98860

    Don Jones
    Keymaster

    Honestly, this is EXACTLY what ConvertFrom-String is designed to solve. You give it patterns to look for and let it figure out how to extract the data. Would you mind reviewing its documentation and examples (https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertfrom-string?view=powershell-5.1) and seeing if it would help? There are also a bunch of articles on the Internet with more examples.

  • #98863

    J Marquis
    Participant

    I looked at convert to string orginally and I could not seem to get the pattern to work. I was trying it with a template. I think it is just because I don't understand it exactly.

    When I use the $test = get-clipboard, the items come in like below. So, each "line" could be a little different.

    France Sun Apr 15 04:24:02 2018 Je@domain.com
    Ju@domain.com
    QuenMoss01@domain.com
    United Kingdom Sun Apr 15 04:10:51 2018 Ju01@domain.com
    da02@domain.com
    ev01@domain.com
    ne01@domain.com
    Netherlands Sun Apr 15 02:58:12 2018 mi01@domain.com
    India Sun Apr 15 02:42:22 2018 ri01@domain.com
    Ireland Sun Apr 15 01:35:33 2018 Vi01@domain.com
    er01@domain.com
    ik01@domain.com

You must be logged in to reply to this topic.