LUM.SfB.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
function format-hours{
    param(
    $businessHours,
    $day,
    $prepend
    )

    If(($businesshours."$($day)hours1".closetime.Totalminutes -eq 0) -and ($businesshours."$($day)hours2".closetime.Totalminutes -eq 1439)){
        $x = "Open all day on $prepend"   
        write-host $x -ForegroundColor Green
        return
    }

    If(($businesshours."$($day)hours1".closetime.Totalminutes -eq 1439) -and ($businesshours."$($day)hours2".closetime.Totalminutes -eq 0)){
        $x = "Open all day on $prepend"   
        write-host $x -ForegroundColor Green
        return
    }

    If(($businesshours."$($day)hours1".closetime.Totalminutes -eq $null) -and ($businesshours."$($day)hours2".closetime.Totalminutes -eq 1439)){
        $x = "Open all day on $prepend"   
        write-host $x -ForegroundColor Green
        return
    }

    If(($businesshours."$($day)hours1".closetime.Totalminutes -eq 1439) -and ($businesshours."$($day)hours2".closetime.Totalminutes -eq $null)){
        $x = "Open all day on $prepend"   
        write-host $x -ForegroundColor Green
        return
    }

    If(($businesshours."$($day)hours1".opentime.Totalminutes -eq $businesshours."$($day)hours1".closetime.Totalminutes) -and ($businesshours."$($day)hours2".opentime.Totalminutes -eq $businesshours."$($day)hours2".closetime.Totalminutes)){
        $x = "Closed all day on $prepend"
        write-host $x -ForegroundColor Red
        return
    }

    If(($businesshours."$($day)hours1".opentime.Totalminutes -eq $businesshours."$($day)hours1".closetime.Totalminutes) -and ($businesshours."$($day)hours2".opentime.Totalminutes -ne $businesshours."$($day)hours2".closetime.Totalminutes)){
        $openH2 = $businesshours."$($day)hours2".opentime.hours
        if($openH2 -lt 10){$openH2 = "0$($openH2.tostring())"}
        $openM2 = $businesshours."$($day)hours2".opentime.minutes
        if($openM2 -lt 10){$openM2 = "0$($openM2.tostring())"}
        $closeH2 = $businesshours."$($day)hours2".closetime.hours
        if($closeH2 -lt 10){$closeH2 = "0$($closeH2.tostring())"}
        $closeM2 = $businesshours."$($day)hours2".closetime.minutes
        if($closeM2 -lt 10){$closeM2 = "0$($closeM2.tostring())"}
        write-host "From $($openH2):$openM2 to $($closeH2):$closeM2 on $prepend" -ForegroundColor Cyan
    }

    If(($businesshours."$($day)hours1".opentime.Totalminutes -ne $businesshours."$($day)hours1".closetime.Totalminutes) -and ($businesshours."$($day)hours2".opentime.Totalminutes -eq $businesshours."$($day)hours2".closetime.Totalminutes)){
        $openH1 = $businesshours."$($day)hours1".opentime.hours
        if($openH1 -lt 10){$openH1 = "0$($openH1.tostring())"}
        $openM1 = $businesshours."$($day)hours1".opentime.minutes
        if($openM1 -lt 10){$openM1 = "0$($openM1.tostring())"}
        $closeH1 = $businesshours."$($day)hours1".closetime.hours
        if($closeH1 -lt 10){$closeH1 = "0$($closeH1.tostring())"}
        $closeM1 = $businesshours."$($day)hours1".closetime.minutes
        if($closeM1 -lt 10){$closeM1 = "0$($closeM1.tostring())"}  
        write-host "From $($openH1):$openM1 to $($closeH1):$closeM1 on $prepend" -ForegroundColor Cyan
    }

    If(($businesshours."$($day)hours1".opentime.Totalminutes -ne $businesshours."$($day)hours1".closetime.Totalminutes) -and ($businesshours."$($day)hours2".opentime.Totalminutes -ne $businesshours."$($day)hours2".closetime.Totalminutes)){
        $openH1 = $businesshours."$($day)hours1".opentime.hours
        if($openH1 -lt 10){$openH1 = "0$($openH1.tostring())"}
        $openM1 = $businesshours."$($day)hours1".opentime.minutes
        if($openM1 -lt 10){$openM1 = "0$($openM1.tostring())"}
        $closeH1 = $businesshours."$($day)hours1".closetime.hours
        if($closeH1 -lt 10){$closeH1 = "0$($closeH1.tostring())"}
        $closeM1 = $businesshours."$($day)hours1".closetime.minutes
        if($closeM1 -lt 10){$closeM1 = "0$($closeM1.tostring())"}

        $openH2 = $businesshours."$($day)hours2".opentime.hours
        if($openH2 -lt 10){$openH2 = "0$($openH2.tostring())"}
        $openM2 = $businesshours."$($day)hours2".opentime.minutes
        if($openM2 -lt 10){$openM2 = "0$($openM2.tostring())"}
        $closeH2 = $businesshours."$($day)hours2".closetime.hours
        if($closeH2 -lt 10){$closeH2 = "0$($closeH2.tostring())"}
        $closeM2 = $businesshours."$($day)hours2".closetime.minutes
        if($closeM2 -lt 10){$closeM2 = "0$($closeM2.tostring())"}
        write-host "From $($openH1):$openM1 to $($closeH1):$closeM1 and from $($openH2):$openM2 to $($closeH2):$closeM2 on $prepend" -ForegroundColor Cyan

    }

}

# This Function Returns the Selected Value and Closes the Form

function Return-DropDown {
    if ($DropDown.SelectedItem -eq $null){
        $DropDown.SelectedItem = $DropDown.Items[0]
        $script:Choice = $DropDown.SelectedItem.ToString()
        $Form.Close()
    }
    else{
        $script:Choice = $DropDown.SelectedItem.ToString()
        $Form.Close()
    }
}

function SelectGroup{
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")


    $Form = New-Object System.Windows.Forms.Form

    $Form.width = 500
    $Form.height = 150
    $Form.Text = �DropDown�

    $DropDown = new-object System.Windows.Forms.ComboBox
    $DropDown.Location = new-object System.Drawing.Size(200,10)
    $DropDown.Size = new-object System.Drawing.Size(260,30)

    [void] $DropDown.Items.Add("Multiple_Response_Groups")
    ForEach ($Item in $DropDownArray) {
     [void] $DropDown.Items.Add($Item)
    }

    $Form.Controls.Add($DropDown)

    $DropDownLabel = new-object System.Windows.Forms.Label
    $DropDownLabel.Location = new-object System.Drawing.Size(10,10) 
    $DropDownLabel.size = new-object System.Drawing.Size(100,40) 
    $DropDownLabel.Text = "Select Group:"
    $Form.Controls.Add($DropDownLabel)

    $Button = new-object System.Windows.Forms.Button
    $Button.Location = new-object System.Drawing.Size(100,50)
    $Button.Size = new-object System.Drawing.Size(100,20)
    $Button.Text = "Select an Item"
    $Button.Add_Click({Return-DropDown})
    $form.Controls.Add($Button)
    $form.ControlBox = $false

    $Form.Add_Shown({$Form.Activate()})
    [void] $Form.ShowDialog()


    return $script:choice
}

function Get-RgsWorkflow{
    <#
    .SYNOPSIS
       Displays one or more RGS workflows
    .DESCRIPTION
       Shows Workflow to queue to group with all parameters
    .EXAMPLE
       Get-RgsWorkflow
    .INPUTS
       Selection of RGSWorkflow Name
    .OUTPUTS
       Report on workflow in Write-Host
    .NOTES
    +---------------------------------------------------------------------------------------------+
    | ORIGIN STORY |
    +---------------------------------------------------------------------------------------------+
    | DATE : 2018.10.29
    | AUTHOR : Michael L�rsen
    | E-Mail : michael.luersen@mondaycoffee.com
    +---------------------------------------------------------------------------------------------+
    #>

    if(!(get-module SkypForBusiness)){
        Import-Module SkypeForBusiness
    }

    if($psise -ne $null){
        Write-Warning "the formatting looks better when you run this cmd-let from the Console instead ISE"
    }

    [array]$DropDownArrayItems = (get-csrgsworkflow).name
    [array]$DropDownArray = $DropDownArrayItems | sort

    $Group = $null
    $Group = SelectGroup
    while ($Group -like ""){
        $Group = SelectGroup
    }

    if($group -eq "Multiple_Response_Groups"){
        $prefix = read-host -Prompt "Type the first letter(s) of the response group for which to return results" 
        $rgsworkflows = get-csrgsworkflow | Where-Object{$_.name -like "$prefix*"}

    }else{
        $rgsworkflows = get-csrgsworkflow -name $group
    }


    foreach($rgsworkflow in $rgsworkflows){
        
        $rgsqueue = Get-CsRgsQueue | Where-Object{$_.identity.instanceID.guid -eq $rgsworkflow.DefaultAction.QueueID.InstanceId}

        $rgsgroups = @()
        foreach($agentGroup in $rgsqueue.AgentGroupIDList){
            $rgsgroups += Get-CsRgsAgentGroup | where-object{$_.identity.instanceid.guid -eq $agentgroup.instanceID.Guid}
        }


        write-host "`n======== GENERAL INFO ========" -BackgroundColor Gray
        if($Rgsworkflow.Active -eq $true){
            write-host "workflow is active" -ForegroundColor Green
        }else{
            write-host "workflow is inactive" -ForegroundColor Gray
        }

        if($Rgsworkflow.EnabledForFederation -eq $true){
            write-host "workflow is enabled for federation" -ForegroundColor Green
        }else{
            write-host "workflow is not enabled for federation" -ForegroundColor Gray
        }

        if($Rgsworkflow.Anonymous -eq $true){
            write-host "workflow is enabled for agent anonymity" -ForegroundColor Green
        }else{
            write-host "workflow is not enabled for agent anonymity" -ForegroundColor Gray
        }

        write-host "Sip Address: `t`t $($rgsworkflow.primaryuri)" -ForegroundColor Cyan
        write-host "Displayname: `t`t $($rgsworkflow.name)" -ForegroundColor Cyan
        write-host "Telephone Number: `t $($rgsworkflow.Lineuri)" -ForegroundColor Cyan
        write-host "DisplayNumber: `t`t $($rgsworkflow.DisplayNumber)" -ForegroundColor Cyan
        if($rgsworkflow.Description -ne $null){
            write-host "Description: `t`t $($rgsworkflow.Description)" -ForegroundColor Cyan
        }

        if($Rgsworkflow.managed -eq $true){
            write-host "workflow type is managed by:" -ForegroundColor Green
            foreach($manager in $rgsworkflow.ManagersByUri){
                write-host "`t`t`t$manager" -ForegroundColor Cyan
            }
        }else{
            write-host "workflow type is unmanaged" -ForegroundColor Gray
        }

        write-host "`n======== Language ========" -BackgroundColor Gray
        write-host "Workflow Language: `t $($rgsworkflow.Language)" -ForegroundColor Cyan

        write-host "`n======== Welcome Message ========" -BackgroundColor Gray
        if($rgsworkflow.DefaultAction.Prompt.AudioFilePrompt -ne $null){
            write-host "Audiofile: `t`t $($rgsworkflow.DefaultAction.Prompt.AudioFilePrompt)" -ForegroundColor Cyan
        }
        if($rgsworkflow.DefaultAction.Prompt.TextToSpeechPrompt -ne $null){
            write-host "Text to Speech: `t`t $($rgsworkflow.DefaultAction.Prompt.TextToSpeechPrompt)" -ForegroundColor Cyan
        }
        write-host "`n======== Business Hours ========" -BackgroundColor Gray
        if($rgsworkflow.NonBusinessHoursAction.Prompt.AudioFilePrompt -ne $null){
            write-host "Message outside of business hours - Audiofile: `t`t $($rgsworkflow.NonBusinessHoursAction.Prompt.AudioFilePrompt)" -ForegroundColor Cyan
        }
        if($($rgsworkflow.NonBusinessHoursAction.Prompt.TextToSpeechPrompt).length -ne 0){
            write-host "Message outside of business hours - Text to Speech: `t`t $($rgsworkflow.NonBusinessHoursAction.Prompt.TextToSpeechPrompt)" -ForegroundColor Cyan
        }

        write-host "Action outside of business hours: `t $($rgsworkflow.NonBusinessHoursAction.action)" -ForegroundColor Cyan
        $businesshours = Get-CsRgsHoursOfBusiness -Identity $rgsworkflow.BusinessHoursID
        if(($businesshours.MondayHours1.OpenTime -eq $businesshours.TuesdayHours1.OpenTime) -and ($businesshours.MondayHours1.CloseTime -eq $businesshours.TuesdayHours1.CloseTime) -and ($businesshours.MondayHours2.OpenTime -eq $businesshours.TuesdayHours2.OpenTime) -and ($businesshours.MondayHours2.CloseTime -eq $businesshours.TuesdayHours2.CloseTime)){
            if(($businesshours.TuesdayHours1.OpenTime -eq $businesshours.WednesdayHours1.OpenTime) -and ($businesshours.TuesdayHours1.CloseTime -eq $businesshours.WednesdayHours1.CloseTime) -and ($businesshours.TuesdayHours2.OpenTime -eq $businesshours.WednesdayHours2.OpenTime) -and ($businesshours.TuesdayHours2.CloseTime -eq $businesshours.WednesdayHours2.CloseTime)){
                if(($businesshours.WednesdayHours1.OpenTime -eq $businesshours.ThursdayHours1.OpenTime) -and ($businesshours.WednesdayHours1.CloseTime -eq $businesshours.ThursdayHours1.CloseTime) -and ($businesshours.WednesdayHours2.OpenTime -eq $businesshours.ThursdayHours2.OpenTime) -and ($businesshours.WednesdayHours2.CloseTime -eq $businesshours.ThursdayHours2.CloseTime)){
                    if(($businesshours.ThursdayHours1.OpenTime -eq $businesshours.FridayHours1.OpenTime) -and ($businesshours.ThursdayHours1.CloseTime -eq $businesshours.FridayHours1.CloseTime) -and ($businesshours.ThursdayHours2.OpenTime -eq $businesshours.FridayHours2.OpenTime) -and ($businesshours.ThursdayHours2.CloseTime -eq $businesshours.FridayHours2.CloseTime)){
                        if(($businesshours.FridayHours1.OpenTime -eq $businesshours.SaturdayHours1.OpenTime) -and ($businesshours.FridayHours1.CloseTime -eq $businesshours.SaturdayHours1.CloseTime) -and ($businesshours.FridayHours2.OpenTime -eq $businesshours.SaturdayHours2.OpenTime) -and ($businesshours.FridayHours2.CloseTime -eq $businesshours.SaturdayHours2.CloseTime)){
                            if(($businesshours.SaturdayHours1.OpenTime -eq $businesshours.SundayHours1.OpenTime) -and ($businesshours.SaturdayHours1.CloseTime -eq $businesshours.SundayHours1.CloseTime) -and ($businesshours.SaturdayHours2.OpenTime -eq $businesshours.SundayHours2.OpenTime) -and ($businesshours.SaturdayHours2.CloseTime -eq $businesshours.SundayHours2.CloseTime)){

                                    format-hours -businessHours $businesshours -day "Sunday" -prepend "Monday - Sunday"
                            }Else{
                                format-hours -businessHours $businesshours -day "Saturday" -prepend "Monday - Saturday"
                                format-hours -businessHours $businesshours -day "Sunday" -prepend "Sunday: "
                            }
                        }Else{
                            format-hours -businessHours $businesshours -day "Friday" -prepend "Monday - Friday"
                            format-hours -businessHours $businesshours -day "Saturday" -prepend "Saturday"
                            format-hours -businessHours $businesshours -day "Sunday" -prepend "Sunday"
                        }
                    }Else{
                        format-hours -businessHours $businesshours -day "Thursday" -prepend "Monday - Thursday"
                        format-hours -businessHours $businesshours -day "Friday" -prepend "Friday"
                        format-hours -businessHours $businesshours -day "Saturday" -prepend "Saturday"
                        format-hours -businessHours $businesshours -day "Sunday" -prepend "Sunday"
                    }
                }Else{
                    format-hours -businessHours $businesshours -day "Wednesday" -prepend "Monday - Wednesday"
                    format-hours -businessHours $businesshours -day "Thursday" -prepend "Thursday"
                    format-hours -businessHours $businesshours -day "Friday" -prepend "Friday"
                    format-hours -businessHours $businesshours -day "Saturday" -prepend "Saturday"
                    format-hours -businessHours $businesshours -day "Sunday" -prepend "Sunday"
        
                }
            }Else{
                format-hours -businessHours $businesshours -day "Tuesday" -prepend "Monday - Wednesday"
                format-hours -businessHours $businesshours -day "Wednesday" -prepend "Wednesday"
                format-hours -businessHours $businesshours -day "Thursday" -prepend "Thursday"
                format-hours -businessHours $businesshours -day "Friday" -prepend "Friday"
                format-hours -businessHours $businesshours -day "Saturday" -prepend "Saturday"
                format-hours -businessHours $businesshours -day "Sunday" -prepend "Sunday" 
            }
        } Else{
            format-hours -businessHours $businesshours -day "Monday" -prepend "Monday"
            format-hours -businessHours $businesshours -day "Tuesday" -prepend "Tuesday"
            format-hours -businessHours $businesshours -day "Wednesday" -prepend "Wednesday"
            format-hours -businessHours $businesshours -day "Thursday" -prepend "Thursday"
            format-hours -businessHours $businesshours -day "Friday" -prepend "Friday"
            format-hours -businessHours $businesshours -day "Saturday" -prepend "Saturday"
            format-hours -businessHours $businesshours -day "Sunday" -prepend "Sunday"
        }

        write-host "`n======== Holidays ========" -BackgroundColor Gray
        foreach($item in $rgsworkflow.HolidaySetIDList){

            $service = (get-csservice | ?{$_.serviceID -eq $($item.ServiceId)}).identity
            $identity = $service + "/" + $item.instanceid
            write-host (Get-CsRgsHolidaySet -Identity $identity).name  -ForegroundColor Cyan
        }


        write-host "`n======== Connected queue ========" -BackgroundColor Gray
        $rgsqueuename = (get-csrgsqueue -Identity $rgsworkflow.DefaultAction.QueueID).name 
        write-host $rgsqueuename -ForegroundColor Cyan

        write-host "`n======== Music on Hold ========" -BackgroundColor Gray
        write-host $rgsworkflow.CustomMusicOnHoldFile.OriginalFileName -ForegroundColor Cyan

        write-host "`n======== Queue settings ========" -BackgroundColor Gray
        $rgsqueue = Get-CsRgsQueue -Name $rgsqueuename
        write-host "Name: `t`t`t`t`t" $rgsqueue.name  -ForegroundColor Cyan
        if($rgsqueue.Description -ne $null){
            write-host "Description: `t`t`t`t" $rgsqueue.Description  -ForegroundColor Cyan
        }
        if($rgsqueue.TimeoutThreshold -ne $null){
            write-host "Queue Timeout Seconds: `t`t`t" $rgsqueue.TimeoutThreshold -ForegroundColor Cyan
            write-host "Queue Timeout Action: `t`t`t" $rgsqueue.Timeoutaction.action -ForegroundColor Cyan
            if($rgsqueue.Timeoutaction.Prompt -ne $null){
                write-host "Queue Timeout Prompt: `t`t`t" $rgsqueue.Timeoutaction.Prompt -ForegroundColor Cyan
            }
            if($rgsqueue.Timeoutaction.Question -ne $null){
                write-host "Queue Timeout Question: `t`t`t" $rgsqueue.Timeoutaction.Question -ForegroundColor Cyan
            }
            if($rgsqueue.Timeoutaction.QueueID -ne $null){
                $rgsTimeoutqueuename = (get-csrgsqueue -Identity $rgsqueue.Timeoutaction.QueueID).name
                write-host "Queue Timeout Forward Queue: `t`t" $rgsTimeoutqueuename -ForegroundColor Cyan
            }
            if($rgsqueue.Timeoutaction.Uri -ne $null){
                write-host "Queue Timeout Forward to: `t`t" $rgsqueue.Timeoutaction.Uri -ForegroundColor Cyan
            }
        }
        If($rgsqueue.OverflowThreshold -ne $null){
            write-host "Queue Overflow Threshold: `t`t" $rgsqueue.OverflowThreshold -ForegroundColor Cyan
            if($rgsqueue.OverflowCandidate -ne $null){
                write-host "Queue Overflow Candidate: `t`t" $rgsqueue.OverflowCandidate -ForegroundColor Cyan
            }
            if($rgsqueue.OverflowAction.Prompt -ne $null){
                write-host "Queue Overflow Prompt: `t`t" $rgsqueue.OverflowAction.Prompt -ForegroundColor Cyan
            }
            if($rgsqueue.OverflowAction.Question -ne $null){
                write-host "Queue Overflow Question: `t`t" $rgsqueue.OverflowAction.Question -ForegroundColor Cyan
            }
            if($rgsqueue.OverflowAction.QueueID -ne $null){
                $rgsOverflowqueuename = (get-csrgsqueue -Identity $rgsqueue.OverflowAction.QueueID).name
                write-host "Queue Overflow Forward Queue: `t`t`t" $rgsOverflowqueuename -ForegroundColor Cyan
            }
            if($rgsqueue.OverflowAction.Uri -ne $null){
                write-host "Queue Overflow Forward to: `t`t" $rgsqueue.OverflowAction.Uri -ForegroundColor Cyan
            }
        }



        write-host "`n======== Groups & Agents ========" -BackgroundColor Gray
        foreach($item in $rgsqueue.AgentGroupIDList){
            $service = (get-csservice | ?{$_.serviceID -eq $($item.ServiceId)}).identity
            $identity = $service + "/" + $item.instanceid
            $agentgroup = (Get-CsRgsAgentGroup -Identity $identity)
            write-host "Agent group Name: `t`t`t" $agentgroup.name  -ForegroundColor Cyan
            write-host "Agent group Alert Time: `t`t" $agentgroup.AgentAlertTime -ForegroundColor Cyan
            if($agentGroup.Description -ne $null){
                write-host "Agent group Description: `t" $agentgroup.Description -ForegroundColor Cyan
            }
            write-host "Agent group participation policy: `t" $agentgroup.ParticipationPolicy -ForegroundColor Cyan
            write-host "Agent group routing method: `t`t" $agentgroup.RoutingMethod -ForegroundColor Cyan
            $x = $false
            foreach($member in $agentGroup.agentsbyURI){
                $agent = $member.ToString()
                if($x -eq $false){
                    $x = $true
                    write-host "Agent group members: `t`t`t" $agent.Replace("sip:","")  -ForegroundColor Green
                }else{
                    write-host "`t`t`t`t`t" $agent.Replace("sip:","")  -ForegroundColor Green
                }
            }
    
        }
    }
    write-host "`n`n" 
    Read-Host -Prompt "Press Enter to exit"
}


function Get-SfbAssignedNumbers{
    <#
    .SYNOPSIS
       Exports all assigned numbers from SfB / AudioCodes
    .DESCRIPTION
       Shows Workflow to queue to group with all parameters
    .PARAMETER audiocodesIniDir
        One or more directories containing ini backup files from Audiocodes SBCs
    .PARAMETER outputDir
        Path where the output is stored
    .PARAMETER gatewayType
        Name that will be given to numbers routed to a gateway (Default value is DECT)
    .PARAMETER lanInterfaceName
        Interface name of the internal LAN interface in Audiocodes (Default value is SIP_LAN)
    .PARAMETER mucIpGroupname
        IP Group Name of the SfB group (Default value is SfB)
    .PARAMETER reduce
        When an unassigned number range is configured with +99, this will be reduced by 58000000000 by
        default to end up with a +41 number. Change accordingly if you have a different country code.
        Assumption is made that you have a 12 digit number including the +. Reason for including a +99
        unassigned number range can be that you want internal callers calling this range be sent back
        to the SBC instead of to the unassigned number target. For example pre-migration scenarios or where
        part of the range belongs to numbers from the gateway.
    .EXAMPLE
       Get-RgsWorkflow -audiocodesDir 'c:\Audiocodes\Backup\10.0.0.10\','c:\Audiocodes\Backup\10.0.0.12\' -outputDir 'c:\SfB\AssignedNumbers\'
    .EXAMPLE
       Get-RgsWorkflow -audiocodesDir 'c:\Audiocodes\Backup\10.0.0.10\' -outputDir 'c:\SfB\AssignedNumbers\' -gatewayType 'PRI_Phones' -lanInterfaceName 'Internal' -mucIpGroupname 'Lync' -reduce 56000000000
    .INPUTS
       Parameters
    .OUTPUTS
       file in outputdir\history containing assigned number range files
       file .\outputdir\SfbAssignedNumbers.csv containing currently assigned numbers
       file .\outputdir\AvailableNumbers.csv containing unassigned numbers based on unassigned number ranges and assignedNumbers
    .NOTES
    +---------------------------------------------------------------------------------------------+
    | ORIGIN STORY |
    +---------------------------------------------------------------------------------------------+
    | DATE : 2018.10.29
    | AUTHOR : Michael L�rsen
    | E-Mail : michael.luersen@mondaycoffee.com
    +---------------------------------------------------------------------------------------------+
    #>

    #
    # This script queries Lync for all assigned numbers and displays in a formatted table with the option to export to CSV.
    # During processing LineURI's are run against a regex pattern to extract the DDI/DID and the extension to a separate column.
    #
    # Numbers Queried: LineURI, Private Line, Analouge Lines, Common Area Phones, RGS Workflows, Exchange UM Contacts, Trusted Applications and Conferencing Numbers

    # Also queries the Fax number from a backup the audiocodes gateway, as well as gateway numbers, which as displayed as DECT as those are typically

    Param(
    [CmdletBinding(
                  SupportsShouldProcess=$false, 
                  PositionalBinding=$false,
                  ConfirmImpact='Low')]
    [Parameter(Mandatory=$true)]
    [string]$audiocodesIniDir,
    [Parameter(Mandatory=$true)]
    [string]$outputDir,
    [Parameter(Mandatory=$false)]
    [string]$gatewayType = 'DECT',
    [Parameter(Mandatory=$false)]
    [string]$lanInterfaceName = 'SIP_LAN',
    [Parameter(Mandatory=$false)]
    [string]$mucIpGroupname = 'SfB',
    [Parameter(Mandatory=$false)]
    [int]$reduce = 58000000000
    )


    if($outputDir.Substring($outputDir.Length -1) -ne "\"){
        $outputDir = $outputDir + '\'
    }

    if(!(test-path $audiocodesIniDir)){
        Write-Error -Message 'audiocodesIniDir does not exist'
        break
    }
    if(!(test-path $outputDir)){
        Write-Error -Message 'outputDir does not exist'
        break
    }



    $FileName = "SfbAssignedNumbers_" + (Get-Date -Format s).replace(":","-") +".csv"
    $FilePath = "$($outputDir)History\$FileName"
    $FilePath2 = "$($outputDir)SfbAssignedNumbers.csv"


    if(!(get-module SkypForBusiness)){
        Import-Module SkypeForBusiness
    }
    if(!(get-module Lync)){
        Import-Module Lync
    }

    $Regex1 = '^(?:tel:)?(?:\+)?(\d+)(?:;ext=(\d+))?(?:;([\w-]+))?$'

    $Array1 = @()

    #Get Fax numbers
    #get most recent ini file
    $ipRouting = $null
    foreach($backupPath in $audiocodesIniDir){
        $backupFile = get-content (Get-ChildItem -path $backupPath -filter "*.ini" | Sort-Object -Property LastWriteTime -Descending  | Select-Object -First 1).fullname
        $match = $false

        foreach($line in $backupFile){
            if($line -eq '[ \IP2IPRouting ]'){
                #stop capturing strings after this
                $match = $false
            }
            if($match -eq $true){
                #capture strings
                $ipRouting += $line 
                $ipRouting += "`n"
            }

            if($line -eq '[ IP2IPRouting ]'){
                #start capturing strings
                $match = $true
            }

        }
    }


    $ipRouting = $ipRouting.trim()
    $ipRouting = ConvertFrom-Csv $ipRouting -Delimiter "," 
    #select only fax numbers (not going to SfB and no empty lines)
    #include a destination type of 8 so that numbers sent to the gateway (ie. DECT) are included too.
    $faxNumbers = $ipRouting | Where-Object{$_.IP2IPRouting_DestSipInterfaceName -eq $lanInterfaceName -and ($_.IP2IPRouting_DestIPGroupName -notin $mucIpGroupname,'' -or $_.IP2IPRouting_DestType -eq 8)}
        foreach($item in $faxNumbers){
        
            if($item.IP2IPRouting_DestUsernamePrefix.IndexOf('[') -ne -1){ #add lines for ranges and series
                $number = $item.IP2IPRouting_DestUsernamePrefix.split('[')[0]
                $number = $number.replace('+','')
                if( ($item.IP2IPRouting_DestUsernamePrefix.split('[')[1]).trim(']').indexof('-') -ne -1){ #found a - in the [] // create a range
                    $range = ($item.IP2IPRouting_DestUsernamePrefix.split('[')[1]).trim(']').split('-')
                    $range[0]..$range[1] | foreach-object{
                        $myObject1 = New-Object System.Object
                        $myObject1 | Add-Member -type NoteProperty -name "LineURI"  -Value $item.IP2IPRouting_DestUsernamePrefix
                        if($_ -lt 10){
                            $myObject1 | Add-Member -type NoteProperty -name "DDI" -Value "$($number)0$($_)"
                        }else{
                            $myObject1 | Add-Member -type NoteProperty -name "DDI" -Value $number$_
                        }
                    
                        $myObject1 | Add-Member -type NoteProperty -name "Ext" -Value ""
                        $myObject1 | Add-Member -type NoteProperty -name "Name" -Value $Item.IP2IPRouting_DestIPGroupName
                        $myObject1 | Add-Member -type NoteProperty -name "FirstName" -Value ""
                        $myObject1 | Add-Member -type NoteProperty -name "LastName" -Value ""
                        if($item.IP2IPRouting_DestType -eq 8){
                            $myObject1 | Add-Member -type NoteProperty -name "Type" -Value $gatewayType
                        }Else{
                            $myObject1 | Add-Member -type NoteProperty -name "Type" -Value "Fax"
                        }
                        $Array1 += $myObject1 
                    }
                }  
                if( ($item.IP2IPRouting_DestUsernamePrefix.split("[")[1]).trim("]").indexof(",") -ne -1){#found a , in the [] // create a series
                    $range = ($item.IP2IPRouting_DestUsernamePrefix.split("[")[1]).trim("]").split(",")
                    foreach($thing in $range){
                        $myObject1 = New-Object System.Object
                        $myObject1 | Add-Member -type NoteProperty -name "LineURI" -Value $item.IP2IPRouting_DestUsernamePrefix
                        $myObject1 | Add-Member -type NoteProperty -name "DDI" -Value $number$thing
                        $myObject1 | Add-Member -type NoteProperty -name "Ext" -Value ""
                        $myObject1 | Add-Member -type NoteProperty -name "Name" -Value $Item.IP2IPRouting_DestIPGroupName
                        $myObject1 | Add-Member -type NoteProperty -name "FirstName" -Value ""
                        $myObject1 | Add-Member -type NoteProperty -name "LastName" -Value ""
                        if($item.IP2IPRouting_DestType -eq 8){
                            $myObject1 | Add-Member -type NoteProperty -name "Type" -Value $gatewayType
                        }Else{
                            $myObject1 | Add-Member -type NoteProperty -name "Type" -Value "Fax"
                        }
                        $Array1 += $myObject1 
                    }
                }
            }  
            Else{# just add the number as is
                $myObject1 = New-Object System.Object
                $myObject1 | Add-Member -type NoteProperty -name "LineURI" -Value $item.IP2IPRouting_DestUsernamePrefix
                $myObject1 | Add-Member -type NoteProperty -name "DDI" -Value $item.IP2IPRouting_DestUsernamePrefix.replace("+","")
                $myObject1 | Add-Member -type NoteProperty -name "Ext" -Value ""
                $myObject1 | Add-Member -type NoteProperty -name "Name" -Value $Item.IP2IPRouting_DestIPGroupName
                $myObject1 | Add-Member -type NoteProperty -name "FirstName" -Value ""
                $myObject1 | Add-Member -type NoteProperty -name "LastName" -Value ""
                if($item.IP2IPRouting_DestType -eq 8){
                    $myObject1 | Add-Member -type NoteProperty -name "Type" -Value $gatewayType
                }Else{
                    $myObject1 | Add-Member -type NoteProperty -name "Type" -Value "Fax"
                }
                $Array1 += $myObject1 
            }         
        }


    #Get Users with LineURI
    $UsersLineURI = Get-CsUser -Filter {LineURI -ne $Null}
    if($UsersLineURI -ne $null){
        foreach($item in $UsersLineURI)    {                  
            $Matches = @()
            $Item.LineURI -match $Regex1 | out-null
            
            $myObject1 = New-Object System.Object
            $myObject1 | Add-Member -type NoteProperty -name "LineURI" -Value $Item.LineURI
            $myObject1 | Add-Member -type NoteProperty -name "DDI" -Value $Matches[1]
            $myObject1 | Add-Member -type NoteProperty -name "Ext" -Value $Matches[2]
            $myObject1 | Add-Member -type NoteProperty -name "Name" -Value $Item.Name
            $myObject1 | Add-Member -type NoteProperty -name "FirstName" -Value $Item.FirstName
            $myObject1 | Add-Member -type NoteProperty -name "LastName" -Value $Item.LastName
            $myObject1 | Add-Member -type NoteProperty -name "Type" -Value "User"
            $Array1 += $myObject1          
        }
    }

    #Get Users with Private Line
    $UsersPrivateLine = Get-CsUser -Filter {PrivateLine -ne $Null} 
    if($UsersPrivateLine -ne $null){
        foreach($item in $UsersPrivateLine)    {                   
            $Matches = @()
            $Item.PrivateLine -match $Regex1 | out-null
            
            $myObject1 = New-Object System.Object
            $myObject1 | Add-Member -type NoteProperty -name "LineURI" -Value $Item.PrivateLine
            $myObject1 | Add-Member -type NoteProperty -name "DDI" -Value $Matches[1]
            $myObject1 | Add-Member -type NoteProperty -name "Ext" -Value $Matches[2]
            $myObject1 | Add-Member -type NoteProperty -name "Name" -Value $Item.Name
            $myObject1 | Add-Member -type NoteProperty -name "FirstName" -Value $Item.FirstName
            $myObject1 | Add-Member -type NoteProperty -name "LastName" -Value $Item.LastName
            $myObject1 | Add-Member -type NoteProperty -name "Type" -Value "UserPrivateLine"
            $Array1 += $myObject1          
        }
    }

    #Get analouge lines
    $AnalougeLineURI = Get-CsAnalogDevice -Filter {LineURI -ne $Null}  
    if($AnalougeLineURI -ne $null){
        foreach($item in $AnalougeLineURI)    {                  
            $Matches = @()
            $Item.LineURI -match $Regex1 | out-null
            
            $myObject1 = New-Object System.Object
            $myObject1 | Add-Member -type NoteProperty -name "LineURI" -Value $Item.LineURI
            $myObject1 | Add-Member -type NoteProperty -name "DDI" -Value $Matches[1]
            $myObject1 | Add-Member -type NoteProperty -name "Ext" -Value $Matches[2]
            $myObject1 | Add-Member -type NoteProperty -name "Name" -Value $Item.Name
            $myObject1 | Add-Member -type NoteProperty -name "Type" -Value "AnalougeLine"
            $Array1 += $myObject1          
        }
    }

    #Get common area phones
    $CommonAreaLineURI = Get-CsCommonAreaPhone -Filter {LineURI -ne $Null} 
    if($CommonAreaLineURI -ne $null){
        foreach($item in $CommonAreaLineURI)    {                    
            $Matches = @()
            $Item.LineURI -match $Regex1 | out-null
            
            $myObject1 = New-Object System.Object
            $myObject1 | Add-Member -type NoteProperty -name "LineURI" -Value $Item.LineURI
            $myObject1 | Add-Member -type NoteProperty -name "DDI" -Value $Matches[1]
            $myObject1 | Add-Member -type NoteProperty -name "Ext" -Value $Matches[2]
            $myObject1 | Add-Member -type NoteProperty -name "Name" -Value $Item.Name
            $myObject1 | Add-Member -type NoteProperty -name "Type" -Value "CommonArea"
            $Array1 += $myObject1          
        }
    }

    #Get RGS workflows
    $WorkflowLineURI = Get-CsRgsWorkflow
    if($WorkflowLineURI -ne $null){
        foreach($item in $WorkflowLineURI)    {                 
            $Matches = @()
            $Item.LineURI -match $Regex1 | out-null
            
            $myObject1 = New-Object System.Object
            $myObject1 | Add-Member -type NoteProperty -name "LineURI" -Value $Item.LineURI
            $myObject1 | Add-Member -type NoteProperty -name "DDI" -Value $Matches[1]
            $myObject1 | Add-Member -type NoteProperty -name "Ext" -Value $Matches[2]
            $myObject1 | Add-Member -type NoteProperty -name "Name" -Value $Item.Name
            $myObject1 | Add-Member -type NoteProperty -name "Type" -Value "RGSWorkflow"
            $Array1 += $myObject1          
        }
    }

    #Get Exchange UM Contacts
    $ExUmContactLineURI = Get-CsExUmContact -Filter {LineURI -ne $Null}
    if($ExUmContactLineURI -ne $null){
        foreach($item in $ExUmContactLineURI)    {                   
            $Matches = @()
            $Item.LineURI -match $Regex1 | out-null
            
            $myObject1 = New-Object System.Object
            $myObject1 | Add-Member -type NoteProperty -name "LineURI" -Value $Item.LineURI
            $myObject1 | Add-Member -type NoteProperty -name "DDI" -Value $Matches[1]
            $myObject1 | Add-Member -type NoteProperty -name "Ext" -Value $Matches[2]
            $myObject1 | Add-Member -type NoteProperty -name "Name" -Value $Item.Name
            $myObject1 | Add-Member -type NoteProperty -name "Type" -Value "ExUmContact"
            $Array1 += $myObject1          
        }
    }

    #Get trusted applications
    $TrustedApplicationLineURI = Get-CsTrustedApplicationEndpoint -Filter {LineURI -ne $Null}
    if($TrustedApplicationLineURI -ne $null){
        foreach($item in $TrustedApplicationLineURI){                   
            $Matches = @()
            $Item.LineURI -match $Regex1 | out-null
            
            $myObject1 = New-Object System.Object
            $myObject1 | Add-Member -type NoteProperty -name "LineURI" -Value $Item.LineURI
            $myObject1 | Add-Member -type NoteProperty -name "DDI" -Value $Matches[1]
            $myObject1 | Add-Member -type NoteProperty -name "Ext" -Value $Matches[2]
            $myObject1 | Add-Member -type NoteProperty -name "Name" -Value $Item.Name
            $myObject1 | Add-Member -type NoteProperty -name "Type" -Value "TrustedApplication"
            $Array1 += $myObject1          
        }
    }

    #Get conferencing numbers
    $DialInConfLineURI = Get-CsDialInConferencingAccessNumber -Filter {LineURI -ne $Null}
    if($DialInConfLineURI -ne $null){
        foreach($Item in $DialInConfLineURI){                 
            $Matches = @()
            $Item.LineURI -match $Regex1 | out-null

            $myObject1 = New-Object System.Object
            $myObject1 | Add-Member -type NoteProperty -name "LineURI" -Value $Item.LineURI
            $myObject1 | Add-Member -type NoteProperty -name "DDI" -Value $Matches[1]
            $myObject1 | Add-Member -type NoteProperty -name "Ext" -Value $Matches[2]
            $myObject1 | Add-Member -type NoteProperty -name "Name" -Value $Item.DisplayName
            $myObject1 | Add-Member -type NoteProperty -name "Type" -Value "DialInConf"
            $Array1 += $myObject1          
        }
    }

    $Array1 | export-csv $FilePath -NoTypeInformation -Encoding unicode
    $Array1 | export-csv $FilePath2  -NoTypeInformation -Encoding unicode -force

    Write-Host "ALL DONE!! Your file has been saved to $FilePath"
    Write-Host "Creating available numbers list"

    Remove-Item -path "$outputDir\Available-numbers.csv" -Force


    $ranges = Get-CsUnassignedNumber 

    $latest = Get-ChildItem -Path $outputPath -filter "*.csv" | Sort-Object LastAccessTime -Descending | Select-Object -First 1
    $assignedNumbers = Import-Csv -LiteralPath $latest.VersionInfo.FileName

    $result = "Range,RangeStart,RangeEnd,AvailableNumber"

    foreach($range in $ranges){
        $start = [long]$range.NumberRangeStart.Substring(5,11)
        $end = [long]$range.NumberRangeEnd.Substring(5,11)
        #reduce to a +41 number in case the range has been put in as +99
        if($start -gt 98999999999){$start = $start - $reduce}
        if($end -gt 98999999999){$end = $end - $reduce}
        $available = @()
        $x = $start
        while($x -le $end){
            if($x -notin $assignedNumbers.DDI){
                $available += $x 
            }
            $x = $x + 1
        }
    
        foreach($item in $available){
            $result += [environment]::NewLine
            $result += "$($range.Identity),$start,$end,$item"
        }
        #add the ranges even if they don't have free numbers for the excel script output.
        $result += [environment]::NewLine
        $result += "$($range.Identity),$start,$end" 
    }

    $result | Out-File -FilePath "$outputDir\Available-numbers.csv" -Encoding unicode
}