Home > front end >  Data is going left to right
Data is going left to right

Time:06-30

I have a data set

email,code 1, code 2, code 3, ..
[email protected],abcd,code-efg,code-hij
[email protected],code-lmn,code-opq,code-ble
...

The email address are unique and there are 8 or more codes each line

I know that I can do

$PeopleAndCodes = Import-Csv C:\temp\newryan.csv

foreach ($Person in $PeopleAndCodes) {
    Write-host "$Person.'New Hire Name'"

    
    for ($count = 0, $count -lt 8, $count   ) {
      
    }

}

I can get each person the problem I am having is then what?

I think I need to suck the codes into a variable so the are not left to right.

The end goal is to send one email to the person with the related codes.

Also, the for loop above is giving me this error

Could not compare "0" to "8 11". Error: "Cannot convert the "System.Object[]" value of type "System.Object[]" to type 
"System.Int32"."

If I $PeopleAndCodes | gm or $person | gm the data is

   TypeName: System.Management.Automation.PSCustomObject

Name          MemberType   Definition
----          ----------   ----------
Equals        Method       bool Equals(System.Object obj)
GetHashCode   Method       int GetHashCode()
GetType       Method       type GetType()
ToString      Method       string ToString()
Code 1        NoteProperty string Code 1=code- abcd
Code 2        NoteProperty string Code 2=code-efg
Code 3        NoteProperty string Code 3=code-hij
Code 4        NoteProperty string Code 4=code-lmn
Code 5        NoteProperty string Code 5=code-opq
Code 6        NoteProperty string Code 6=code-rst
Code 7        NoteProperty string Code 7=code-tuv
Code 8        NoteProperty string Code 8=code-wxyz
New Hire Name NoteProperty string New Hire [email protected]

How do I get the codes so I can then send 1 email to each person with there codes?

CodePudding user response:

The following uses the intrinsic psobject property to reflect on each CSV object (row) to get the value of those properties (columns) whose name starts with "code ":

@'
email,code 1, code 2, code 3
[email protected],abcd,code-efg,code-hij
[email protected],code-lmn,code-opq,code-ble
'@ | 
  ConvertFrom-Csv |  # Simulates your CSV-file import
  ForEach-Object {
    [pscustomobject] @{
      email = $_.email
      codes = $_.psobject.Properties.Where({ $_.Name -like 'code *'}).Value
    }
  }

Output (the codes property contains the individual code values as an array):

email        codes
-----        -----
[email protected] {abcd, code-efg, code-hij}
[email protected] {code-lmn, code-opq, code-ble}

CodePudding user response:

This is what I did.

$PeopleAndCodes = Import-Csv C:\temp\newryan.csv

$Toemail = $PeopleAndCodes | ForEach-Object {
    [pscustomobject] @{
        email = $_.'New Hire Name'
        codes = $_.psobject.Properties.Where({ $_.Name -like 'code *' }).Value
    }
}

$Toemail | ForEach-Object {
    $thecode = $null
    foreach ($code in $_.codes) {
        $thecode  = $code   '<br>'
    }
    
    $PersonName = $null
    [string]$PersonName = (Get-culture).TextInfo.ToTitleCase($_.email).split('@')[0].Replace('.', ' ')

    $body = $null
    [string]$Body ="
    Hello $PersonName,<br>
    ...blah...
    $thecode
    "

    Send-MailMessage -to <Email> -Body $body -Subject "$PersonName here are your FY 22 Product discount codes." -From <Email> -SmtpServer '.com' -port '25' -BodyAsHtml
}
  • Related