molinaj31

Create Calendar Event From Existing Event

May 17th, 2018
609
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Import-Module NewOSCEXOAppointment
  2.  
  3. function Get-O365CalendarEvent
  4. {
  5. <#
  6.  
  7.    
  8. .EXAMPLE
  9.     PS C:\> Get-O365CalendarEvent -EmailAddress [email protected] -Credential (Get-Credential) | Select-Object -Property Subject, StartTimeZone, Start, End, @{L="Attendees";E={$psitem.attendees.emailaddress | Select-Object -Property name -Unique|Sort}}
  10.        
  11.     Get the calendar Events Subject, StartTimeZone,Start, End, Attendees for the last 7 days
  12.    
  13. .EXAMPLE
  14.     Get-O365CalendarEvent -EmailAddress [email protected] -Credential $cred -StartDateTime $((Get-Date).adddays(-50)) -PageResult 15
  15.    
  16. .NOTES
  17.     Francois-Xavier Cat
  18.     www.lazywinadmin.com
  19.     @lazywinadm
  20.     github.com/lazywinadmin
  21. #>
  22.    
  23.     [CmdletBinding()]
  24.     param
  25.     (
  26.         [System.String]$EmailAddress,
  27.        
  28.         [System.datetime]$StartDateTime = (Get-Date),
  29.        
  30.         [System.datetime]$EndDateTime = ((Get-Date).adddays(7)),
  31.        
  32.         [System.Management.Automation.Credential()]
  33.         [pscredential]
  34.         $Credential = [System.Management.Automation.PSCredential]::Empty,
  35.        
  36.         [ValidateNotNullOrEmpty()]
  37.         [ValidateRange(1, 50)]
  38.         $PageResult = '10',
  39.        
  40.         [ValidateSet(
  41.             'Afghanistan Standard Time',
  42.             'Alaskan Standard Time',
  43.             'Arab Standard Time',
  44.             'Arabian Standard Time',
  45.             'Arabic Standard Time',
  46.             'Atlantic Standard Time',
  47.             'AUS Central Standard Time',
  48.             'AUS Eastern Standard Time',
  49.             'Azerbaijan Standard Time',
  50.             'Azores Standard Time',
  51.             'Canada Central Standard Time',
  52.             'Cape Verde Standard Time',
  53.             'Caucasus Standard Time',
  54.             'Cen. Australia Standard Time',
  55.             'Central America Standard Time',
  56.             'Central Asia Standard Time',
  57.             'Central Brazilian Standard Time',
  58.             'Central Europe Standard Time',
  59.             'Central European Standard Time',
  60.             'Central Pacific Standard Time',
  61.             'Central Standard Time',
  62.             'Central Standard Time (Mexico)',
  63.             'China Standard Time',
  64.             'Dateline Standard Time',
  65.             'E. Africa Standard Time',
  66.             'E. Australia Standard Time',
  67.             'E. Europe Standard Time',
  68.             'E. South America Standard Time',
  69.             'Eastern Standard Time',
  70.             'Egypt Standard Time',
  71.             'Ekaterinburg Standard Time',
  72.             'Fiji Standard Time', 'FLE Standard Time',
  73.             'Georgian Standard Time',
  74.             'GMT Standard Time',
  75.             'Greenland Standard Time',
  76.             'Greenwich Standard Time',
  77.             'GTB Standard Time',
  78.             'Hawaiian Standard Time',
  79.             'India Standard Time',
  80.             'Iran Standard Time',
  81.             'Israel Standard Time',
  82.             'Korea Standard Time',
  83.             'Mid-Atlantic Standard Time',
  84.             'Mountain Standard Time',
  85.             'Mountain Standard Time (Mexico)',
  86.             'Myanmar Standard Time',
  87.             'N. Central Asia Standard Time',
  88.             'Namibia Standard Time',
  89.             'Nepal Standard Time',
  90.             'New Zealand Standard Time',
  91.             'Newfoundland Standard Time',
  92.             'North Asia East Standard Time',
  93.             'North Asia Standard Time',
  94.             'Pacific SA Standard Time',
  95.             'Pacific Standard Time',
  96.             'Romance Standard Time',
  97.             'Russian Standard Time',
  98.             'SA Eastern Standard Time',
  99.             'SA Pacific Standard Time',
  100.             'SA Western Standard Time',
  101.             'Samoa Standard Time',
  102.             'SE Asia Standard Time',
  103.             'Singapore Standard Time',
  104.             'South Africa Standard Time',
  105.             'Sri Lanka Standard Time',
  106.             'Taipei Standard Time',
  107.             'Tasmania Standard Time',
  108.             'Tokyo Standard Time',
  109.             'Tonga Standard Time',
  110.             'US Eastern Standard Time',
  111.             'US Mountain Standard Time',
  112.             'Vladivostok Standard Time',
  113.             'W. Australia Standard Time',
  114.             'W. Central Africa Standard Time',
  115.             'W. Europe Standard Time',
  116.             'West Asia Standard Time',
  117.             'West Pacific Standard Time',
  118.             'Yakutsk Standard Time'
  119.         )]
  120.         [System.String]$Timezone
  121.     )
  122.    
  123.     PROCESS
  124.     {
  125.         TRY
  126.         {
  127.             $FunctionName = (Get-Variable -Name MyInvocation -Scope 0 -ValueOnly).MyCommand
  128.            
  129.             Write-Verbose -Message "[$FunctionName] Create splatting"
  130.             $Splatting = @{
  131.                 Credential = $Credential
  132.                 Uri = "https://outlook.office365.com/api/v1.0/users/$EmailAddress/calendarview?startDateTime=$StartDateTime&endDateTime=$($EndDateTime)&`$top=$PageResult"
  133.             }
  134.            
  135.            
  136.             if ($TimeZone)
  137.             {
  138.                 Write-Verbose -Message "[$FunctionName] Add TimeZone"
  139.                 $headers = New-Object -TypeName 'System.Collections.Generic.Dictionary[[String],[String]]'
  140.                 $headers.Add('Prefer', "outlook.timezone=`"$TimeZone`"")
  141.                 $Splatting.Add('Headers', $headers)
  142.             }
  143.             if (-not $PSBoundParameters['EmailAddress'])
  144.             {
  145.                 Write-Verbose -Message "[$FunctionName] EmailAddress not specified, updating URI"
  146.                 #Query the current User
  147.                 $Splatting.Uri = "https://outlook.office365.com/api/v1.0/me/calendarview?startDateTime=$StartDateTime&endDateTime=$($EndDateTime)&`$top=$PageResult"
  148.             }
  149.             Write-Verbose -Message "[$FunctionName] URI: $($Splatting.Uri)"
  150.             Invoke-RestMethod @Splatting -ErrorAction Stop | Select-Object -ExpandProperty Value
  151.         }
  152.         CATCH
  153.         {
  154.             $PSCmdlet.ThrowTerminatingError($_)
  155.         }
  156.     }
  157. }
  158.  
  159. Connect-OSCEXOWebService -Credential (Get-Credential)
  160.  
  161. $events = (Get-O365CalendarEvent `
  162.     -EmailAddress username@domain.com `
  163.     -Credential (Get-Credential) `
  164.     -StartDateTime (get-date) `
  165.     -EndDateTime ((Get-Date).adddays(+10)) `
  166.     -PageResult 2|Select-Object -Property Subject, StartTimeZone, Start, End)
  167.  
  168. forEach($event in $events) {  
  169.     New-OSCEXOAppointment -Identity username@domain.com -Subject $event.subject -StartDate $event.Start -Body "Company Event"  -UseImpersonation -Verbose
  170.     }
Add Comment
Please, Sign In to add comment