@@ -1082,8 +1082,6 @@ New-Alias "opc-getcalendar" OpCon_GetCalendar
1082
1082
# Updates a calendar
1083
1083
function OpCon_UpdateCalendar ($url , $token , $name , $id , $date )
1084
1084
{
1085
- $hdr = @ {" authorization" = $token }
1086
-
1087
1085
if ($name -or $id )
1088
1086
{
1089
1087
$counter = 0
@@ -1096,35 +1094,96 @@ function OpCon_UpdateCalendar($url,$token,$name,$id,$date)
1096
1094
$calendar | ForEach-Object { $counter ++ }
1097
1095
1098
1096
if ($counter -ne 1 )
1097
+ { Write-Host " More than 1 or no calendars returned!" }
1098
+ else
1099
1099
{
1100
- Write-Host " More than 1 or no calendars returned!"
1101
- }
1102
- }
1103
- else
1104
- {
1105
- Write-Host " No name or id specified!"
1106
- }
1107
-
1108
- if ($date )
1109
- {
1110
- $uriput = $url + " /api/calendars/" + $calendar [0 ].id
1111
- $calendar [0 ].dates += " $date "
1112
- $body = $calendar [0 ] | ConvertTo-JSON - Depth 7
1100
+ if ($date )
1101
+ {
1102
+ if ($calendar [0 ].dates)
1103
+ {
1104
+ if ($date.IndexOf (" ;" ) -ge 0 )
1105
+ {
1106
+ $date.Split (" ;" ) | ForEach-Object {
1107
+ if ($_ -notin $calendar [0 ].dates)
1108
+ {
1109
+ if ($null -eq $dateList )
1110
+ { $dateList = $_ }
1111
+ else
1112
+ { $dateList = $dateList + " ;" + $_ }
1113
+ }
1114
+ }
1115
+ }
1116
+ else
1117
+ {
1118
+ if ($date -notin $calendar [0 ].dates)
1119
+ { $dateList = $date }
1120
+ }
1121
+
1122
+ if ($null -ne $dateList )
1123
+ {
1124
+ $calendar [0 ].dates += $dateList
1125
+ $body = $calendar [0 ]
1126
+
1127
+ try
1128
+ { $calendaradd = Invoke-RestMethod - Method PUT - Uri ($url + " /api/calendars/" + $calendar [0 ].id) - Body ($body | ConvertTo-JSON - Depth 7 ) - Headers @ {" authorization" = $token } - ContentType " application/json" }
1129
+ catch [Exception ]
1130
+ {
1131
+ Write-Host $_
1132
+ Write-Host $_.Exception.Message
1133
+ }
1134
+
1135
+ return $calendaradd
1136
+ }
1137
+ else
1138
+ { Write-Host " Date/s $date already in calendar $name !" }
1139
+ }
1140
+ else
1141
+ {
1142
+ if (! $calendar [0 ].description)
1143
+ { $description = " " }
1144
+ else
1145
+ { $description = $calendar [0 ].description }
1146
+
1147
+ if (! $calendar [0 ].schedule)
1148
+ {
1149
+ $body = @ {
1150
+ " id" = $calendar [0 ].id;
1151
+ " type" = $calendar [0 ].type;
1152
+ " name" = $calendar [0 ].Name;
1153
+ " dates" = @ ( $date );
1154
+ " description" = $description
1155
+ }
1156
+ }
1157
+ else
1158
+ {
1159
+ $schedule = $calendar [0 ].schedule
1160
+ $body = @ {
1161
+ " id" = $calendar [0 ].id;
1162
+ " type" = $calendar [0 ].type;
1163
+ " schedule" = $schedule ;
1164
+ " name" = $calendar [0 ].Name;
1165
+ " dates" = @ ( $date );
1166
+ " description" = $description
1167
+ }
1168
+ }
1113
1169
1114
- try
1115
- {
1116
- $calendaradd = (Invoke-RestMethod - Method PUT - Uri $uriput - Body $body - Headers $hdr - ContentType " application/json" )
1117
- }
1118
- catch [Exception ]
1119
- {
1120
- Write-Host $_
1121
- Write-Host $_.Exception.Message
1170
+ try
1171
+ { $calendaradd = Invoke-RestMethod - Method PUT - Uri ($url + " /api/calendars/" + $calendar [0 ].id) - Body ($body | ConvertTo-JSON - Depth 7 ) - Headers @ {" authorization" = $token } - ContentType " application/json" }
1172
+ catch [Exception ]
1173
+ {
1174
+ Write-Host $_
1175
+ Write-Host $_.Exception.Message
1176
+ }
1177
+
1178
+ return $calendaradd
1179
+ }
1180
+ }
1181
+ else
1182
+ { Write-Host " No date specified!" }
1122
1183
}
1123
-
1124
- return $calendaradd
1125
1184
}
1126
1185
else
1127
- { Write-Host " No date specified!" }
1186
+ { Write-Host " No name or id specified!" }
1128
1187
}
1129
1188
New-Alias " opc-updatecalendar" OpCon_UpdateCalendar
1130
1189
0 commit comments