<# Input: CSV file Output: Valid CPR unsubscribe file #> # Constants: CPR requires us to use the following information (along with CPR number) #Each line in the file contains a record, and the record contains the following information (always 80 characters per rrecord/line) $CPR_DataType = '06' # 06 means subscribe/unsubscribe $CPR_Customer = '4211' # Our customer ID in the CPR system $CPR_Constant = '00' # Documentation tells us this constant has to be included and written as 00 (always) $CPR_EnableRemove = 'SL' # SL means delete subscription, OP means create subscription #CPRNumber CPR number, obviously not a constant $CPR_KeyConstant = ' ' # 15 obligatory characters (use 15 whitespace characters if not in use) that can be used to mark each entry. CPR will not use this, but will return the string along with the result of the entry. Some companies use it to recognisethe CPR response $CPR_Constant2 = ' ' # 45 obligatory blank spaces at the end of each record $InputFile = 'unsubscribe.csv' $OutputFile = "d" + $(Get-Date -Format "yyMMdd") + ".i" + $CPR_Customer + $CPR_DataType Write-Output "Deleting old outputfile if exists: $OutputFile" Remove-Item .\$OutputFile -ErrorAction SilentlyContinue Write-Output "Reading Inputfile: $InputFile" $UnsubscribeList = Import-CSV $InputFile -delimiter ',' -Encoding UTF7 Foreach ($Unsubscribe in $UnsubscribeList) { Write-Output "Writing entry: $($Unsubscribe.CPR)" $UnsubscribeString = $CPR_DataType + $CPR_Customer + $CPR_Constant + $CPR_EnableRemove + $Unsubscribe.CPR + $CPR_KeyConstant + $CPR_Constant2 #Add-content $OutputFile -value $UnsubscribeString -Encoding $([System.Text.Encoding]::GetEncoding('iso-8859-1').WindowsCodePage) # please note that CPR only supports ISO-8859-1, that is a std parameter, I therefore have to ask the operating system for codepage number [System.IO.File]::AppendAllText($(Get-Location).Path + "\" + $OutputFile,$UnsubscribeString + "`r`n",[System.Text.Encoding]::GetEncoding('iso-8859-1')) }