# Benjamin MOREAU - 13/05/2015
# ICS To CSV converter
$ICSpath="...\ICS\"
#Your ICS Folder (input)
$CSVpath="...\CSV\"
# Your CSV Folder (output)
$ICSlist = get-childitem $ICSPath
#Read each ICS file on your ICS folder
Foreach ($i in $ICSlist )
{
$file= $i.fullname
#Convert your ICS filename with CSV extension
$CSVFile = $CSVpath + $i.name
$CSVFile = $CSVFile.substring(0,$CSVFile.Length-3)
$CSVFile = $CSVFile + "csv"
#Create first line of CSV
"Titre,Note,StartDate,Starttime,EndDate,EndTime" | out-file -FilePath $CSVFile
#get content of your file
$content = Get-Content $file -Encoding UTF8
$content | foreach-Object {
#If we found ":", we cut on 2 parts => key:values
if($_.Contains(':')){
$z=@{ $_.split( ':')[0] =( $_.split( ':')[1]).Trim()}
if ($z.keys -eq "BEGIN")
{
#If we detect "BEGIN", It's the start of the appointment block ...but we do nothing :)
}
if ($z.keys -eq "DTSTART;VALUE=DATE") {
#If we detect date without time we use 00:00:00
$RdvStart=$z.values -replace "r\n\s"
$RdvStart=[datetime]::ParseExact($RdvStart,"yyyyMMdd" ,$null)
$RdvStartDate = $RdvStart.ToShortDateString()
$RdvStartDate = get-date $RdvStartDate -Format MM/dd/yyyy
$RdvStartTime = "00:00:00"
}
if ($z.keys -eq "DTSTART") {
$RdvStart=$z.values -replace "r\n\s"
$RdvStart=$RdvStart -replace "T"
$RdvStart=$RdvStart -replace "Z"
$RdvStart=[datetime]::ParseExact($RdvStart,"yyyyMMddHHmmss" ,$null)
$RdvStartDate = $RdvStart.ToShortDateString()
$RdvStartTime = $RdvStart.ToLongTimeString()
$RdvStartDate = get-date $RdvStartDate -Format MM/dd/yyyy
}
if ($z.keys -eq "DTEND")
{
$RdvEnd=$z.values -replace "\r\n\s"
$RdvEnd=$RdvEnd -replace "T"
$RdvEnd=$RdvEnd -replace "Z"
$RdvEnd=[datetime]::ParseExact($RdvEnd,"yyyyMMddHHmmss" ,$null)
$RdvEndDate = $RdvEnd.ToShortDateString()
$RdvEndTime = $RdvEnd.ToLongTimeString()
$RdvEndDate = get-date $RdvEndDate -Format MM/dd/yyyy
}
if ($z.keys -eq "SUMMARY")
{
$RdvTitre=$z.values -replace "\r\n\s"
$RdvTitre=$z.values -replace ",","-"
}
if ($z.keys -eq "DESCRIPTION")
{
$RdvNote=$z.values -replace "\r\n\s"
$RdvNote=$RdvNote -replace "<p>"
$RdvNote=$RdvNote -replace "</p>"
$RdvNote=$RdvNote -replace ",","-"
}
if ($z.keys -eq "DTEND;VALUE=DATE") {
#If we detect a date without time, we use 00:00:00
$RdvEnd=$z.values -replace "r\n\s"
$RdvEnd=[datetime]::ParseExact($RdvEnd,"yyyyMMdd" ,$null)
$RdvEndDate = $RdvEnd.ToShortDateString()
$RdvEndDate = get-date $RdvEndDate -Format MM/dd/yyyy
$RdvEndTime = "00:00:00"
}
if ($z.keys -eq "END") {
# We detect "END", so we add the line on the CSV File. 1 line = 1 appointment
"$RdvTitre,$RdvNote,$RdvStartDate,$RdvStartTime,$RdvEndDate,$RdvEndTime" | out-file -FilePath $CSVFile -append
# clearing vars...
$RdvStart =""
$RdvEnd =""
$RdvNote=""
$RdvTitre=""
$RdvEndDate = ""
$RdvEndTime = ""
$RdvStartDate = ""
$RdvStartTime = ""
}
}}}
# Benjamin MOREAU - 15/05/2015
# CSV to Office 365
$username = "your mail of Office 365 admin account"
$password = "your password"
$EWSURL = "https://outlook.office365.com/EWS/Exchange.asmx"
$EWSManagedApiPath = "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"
if (!(Get-Item -Path $EWSManagedApiPath -ErrorAction SilentlyContinue))
{
throw "EWS Managed API could not be found at $($EWSManagedApiPath).";cl
}
[void][Reflection.Assembly]::LoadFile($EWSManagedApiPath);
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1)
$service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials($Username,$Password);
$service.URL = New-Object Uri($EwsUrl)
$CalendarFolder = [Microsoft.Exchange.WebServices.Data.CalendarFolder]::Bind($service, [Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Calendar);
$CSVpath="...\CSV\" # Your CSV folder
$CSVlist = get-childitem $CSVPath
#Read CSV Files
Foreach ($i in $CSVList ){
$file= $i.name
$EmailAddress = $i.name
$EmailAddress = $EmailAddress.substring(0,$file.Length-4)
$CSVFile = Import-Csv -Path $i.fullname
$service.ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $EmailAddress);
Write-Host $EmailAddress
# Parse the CSV file and add the appointments
foreach ($CalendarItem in $CSVFile)
{
# Create appointment
$NoError=$true;
$Appointment = New-Object Microsoft.Exchange.WebServices.Data.Appointment($service);
$Appointment.Subject=$CalendarItem."Titre";
write-host ($CalendarItem."StartDate" + " " + $CalendarItem."StartTime")
$StartDate=[DateTime]($CalendarItem."StartDate" + " " + $CalendarItem."StartTime");
$Appointment.Start=$StartDate;
$EndDate=[DateTime]($CalendarItem."EndDate" + " " + $CalendarItem."EndTime");
$Appointment.End=$EndDate;
if ($CalendarItem.Note) {$Appointment.Body=$CalendarItem.Note}
# Upload Appointment on Office 365
$Appointment.Save([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Calendar);
Write-Host "Created $($CalendarItem."Subject")" -ForegroundColor green;
}
}
undefined
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (2)
Author
Commented:Commented:
I am very interest to read on your solutions/scripts.
However, by converting that .ICS to .CSV, it actually drop 60% of the original file size, means data are not getting into the mailbox.
What i am reading is https://msdn.microsoft.com/en-us/library/office/dn672319(v=exchg.150).aspx#sectionSection2
that is will allow EWS to import the ICS file into the Exchange Online directly.
Being not a programmer, i will need to figure out how to do so
Open in new window