PSHVTag.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
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
Write-Verbose 'Importing from [C:\MyProjects\PSHVTag\PSHVTag\private]'
# .\PSHVTag\private\Convert-VMNoteTagsToObject.ps1
function Convert-VMNoteTagsToObject
{
    <#
    .SYNOPSIS
    Converts a Vm object to Vm object including tags
    
    .DESCRIPTION
    Converts a Hyper-V VM object including State, Status and Notes to a VMWithTag Object including the custom Tag information
    
    .PARAMETER VM
    A Hyper-V VM object including, State, Status and Notes.
    
    .EXAMPLE
    Get-VM -Name 'Test1' | Convert-VMNoteTagsToObject
    
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline)]
        $VM
    )
    $VM.Notes -Match '(\<Env\>(?<Environment>.+)\<\/Env\>|\<Service\>(?<Service>.+)\<\/Service\>|\<DependsOn\>(?<DependsOn>.+)\<\/DependsOn\>)+' |Out-Null
    #[VMWithTag]::new('PI-LAB-GW03',(New-Guid),'PI-LAB','Network',@('HY','AZ'))
    #$VmWithTag = [VMWithTag]::new($VM.Name, $Vm, ($Matches.Environment -split ','), ($Matches.Service -split ','), ($Matches.DependsOn -split ','))
    $VmWithTag = [VMWithTag]::new($VM.Name, $Vm, ($Matches.Environment -split ',' | Where-Object {$_ -ne ''}), ($Matches.Service -split ','| Where-Object {$_ -ne ''}), ($Matches.DependsOn -split ','| Where-Object {$_ -ne ''}))
    $VmWithTag
}
# .\PSHVTag\private\Get-ClonedObject.ps1

function Get-ClonedObject
{
    <#
    .SYNOPSIS
    Clones object on Binary level
    
    .DESCRIPTION
    Clones object on Binary level
    
    .PARAMETER DeepCopyObject
    Object to Clone
    
    .EXAMPLE
    $currentEdgeList = [hashtable] (Get-ClonedObject $edgeList)
    
    .NOTES
    Idea from http://stackoverflow.com/questions/7468707/deep-copy-a-dictionary-hashtable-in-powershell
    borrowed from http://stackoverflow.com/questions/8982782/does-anyone-have-a-dependency-graph-and-topological-sorting-code-snippet-for-pow
    #>

    param($DeepCopyObject)
    $memStream = new-object IO.MemoryStream
    $formatter = new-object Runtime.Serialization.Formatters.Binary.BinaryFormatter
    $formatter.Serialize($memStream, $DeepCopyObject)
    $memStream.Position = 0
    $formatter.Deserialize($memStream)
}
# .\PSHVTag\private\Get-EdgeHashtableFromVMNote.ps1
function Get-EdgeHashtableFromVMNote
{
    <#
    .SYNOPSIS
    Creates a hashtable with KeyProperty as Key and Value property as (an array of) Value(s)
    
    .DESCRIPTION
    Creates a hashtable with KeyProperty as Key and Value property as (an array of) Value(s)
    
    .PARAMETER VM
    A VMwithTag object
    
    .PARAMETER KeyProperty
    The property of the VM object which should be used as Key in the hashtable to create
    
    .PARAMETER ValueProperty
    The property of the VM object to use as value of the hashtable to create
    
    .EXAMPLE
    Get-EdgeHashtableFromVMNote -VM $VM -KeyProperty Service -ValueProperty DependsOn
        
    #>

    [CmdletBinding()]
    [OutputType([System.Collections.Hashtable])]
    param(

        # Vm object with tags
        [Parameter(Mandatory = $true)]
        [VMwithTag[]]
        $VM,
        #Property to use as Key
        [Parameter(Mandatory = $true)]
        [string]
        $KeyProperty,
        #Property to use as Value
        [Parameter(Mandatory = $true)]
        [string]
        $ValueProperty
    )

    $EdgeList = @{}
    foreach ($Node in $VM)
    {
        Write-Verbose -Message ('Processing VM ' + $Node.Name + ' Key Property: ' + $KeyProperty)
        Foreach ($Key in $Node.$KeyProperty)
        {
            Write-Verbose -Message ('Processing VM ' + $Node.Name + ' Key Property: ' + $KeyProperty + ' Key value: ' + $Key)
            If (!($EdgeList.ContainsKey($Key)))
            {
                Write-Verbose -Message ('Creating new Key for ' + $Key)
                $EdgeList.Add($Key, $null)                
            }
            Foreach ($Value in $Node.$ValueProperty)
            {
                Write-Verbose -Message ('Processing VM ' + $Node.Name + ' Key Property: ' + $Key + ' Value Property: ' + $Value)
                If ($EdgeList.Item($Key) -notcontains $Value)
                {
                    Write-Verbose -Message ('Adding ' + $Value + ' to Key Property ' + $Key + ' values')
                    [array]$EdgeList.Item($Key) += [string]$Value
                }
            }
        }
    }
    $EdgeList #.GetEnumerator() | Sort-Object -Property Name
}
# .\PSHVTag\private\Get-TopologicalSort.ps1
function Get-TopologicalSort
{
    <#
    .SYNOPSIS
    Sorts Keys of a Hashtable containing dependent Keys as Value array as a topology.
    
    .DESCRIPTION
    Sorts Keys of a Hashtable containing dependent Keys as Value array as a topology. And returns an orederd list.
    
    .PARAMETER edgeList
    A hashtable containing the edges from the Key to the values
    
    .EXAMPLE
    Get-TopologicalSort -edgeList @{ServiceA=@(ServiceB,ServiceC),ServiceB=@(ServiceC)}
    
    .NOTES
    Thanks to http://stackoverflow.com/questions/8982782/does-anyone-have-a-dependency-graph-and-topological-sorting-code-snippet-for-pow
    Input is a hashtable of @{ID = @(Depended,On,IDs);...}
    #>

    param(
        [Parameter(Mandatory = $true, Position = 0)]
        [hashtable] $edgeList
    )
  
    # Make sure we can use HashSet
    Add-Type -AssemblyName System.Core
  
    # Clone it so as to not alter original
    $currentEdgeList = [hashtable] (Get-ClonedObject $edgeList)
  
    # algorithm from http://en.wikipedia.org/wiki/Topological_sorting#Algorithms
    $topologicallySortedElements = New-Object System.Collections.ArrayList
    $setOfAllNodesWithNoIncomingEdges = New-Object System.Collections.Queue
  
    $fasterEdgeList = @{}
  
    # Keep track of all nodes in case they put it in as an edge destination but not source
    $allNodes = New-Object -TypeName System.Collections.Generic.HashSet[object] -ArgumentList (, [object[]] $currentEdgeList.Keys)
  
    foreach ($currentNode in $currentEdgeList.Keys)
    {
        $currentDestinationNodes = [array] $currentEdgeList[$currentNode]
        if ($currentDestinationNodes.Length -eq 0)
        {
            $setOfAllNodesWithNoIncomingEdges.Enqueue($currentNode)
        }
  
        foreach ($currentDestinationNode in $currentDestinationNodes)
        {
            if (!$allNodes.Contains($currentDestinationNode))
            {
                [void] $allNodes.Add($currentDestinationNode)
            }
        }
  
        # Take this time to convert them to a HashSet for faster operation
        If ($currentDestinationNodes -ne $null)
        {
            $currentDestinationNodes = New-Object -TypeName System.Collections.Generic.HashSet[object] -ArgumentList (, [object[]] $currentDestinationNodes )
        }    
        [void] $fasterEdgeList.Add($currentNode, $currentDestinationNodes)        
    }
  
    # Now let's reconcile by adding empty dependencies for source nodes they didn't tell us about
    foreach ($currentNode in $allNodes)
    {
        if (!$currentEdgeList.ContainsKey($currentNode))
        {
            [void] $currentEdgeList.Add($currentNode, (New-Object -TypeName System.Collections.Generic.HashSet[object]))
            $setOfAllNodesWithNoIncomingEdges.Enqueue($currentNode)
        }
    }
  
    $currentEdgeList = $fasterEdgeList
  
    while ($setOfAllNodesWithNoIncomingEdges.Count -gt 0)
    {        
        $currentNode = $setOfAllNodesWithNoIncomingEdges.Dequeue()
        [void] $currentEdgeList.Remove($currentNode)
        [void] $topologicallySortedElements.Add($currentNode)
  
        foreach ($currentEdgeSourceNode in $currentEdgeList.Keys)
        {
            $currentNodeDestinations = $currentEdgeList[$currentEdgeSourceNode]
            if ($null -ne $currentNodeDestinations -and $currentNodeDestinations.Contains($currentNode))
            {
                [void] $currentNodeDestinations.Remove($currentNode)
  
                if ($currentNodeDestinations.Count -eq 0)
                {
                    [void] $setOfAllNodesWithNoIncomingEdges.Enqueue($currentEdgeSourceNode)
                }                
            }
        }
    }
  
    if ($currentEdgeList.Count -gt 0)
    {
        throw "Graph has at least one cycle!"
    }
  
    return $topologicallySortedElements
}
# .\PSHVTag\private\Get-VMEnvironment.ps1
function Get-VMEnvironment
{
    <#
    .SYNOPSIS
    Creates a (array of) VMEnvironment object(s)
    
    .DESCRIPTION
    Creates a (array of) VMEnvironment object(s) based on the list of tagged VMs defnied by the parameter VM.
    
    .PARAMETER VM
    A (list of) VM object(s) with Tags from which to build the VMEnvironment object
    
    .EXAMPLE
    Get-VMEnvironment -VM (Get-VMWithTag | Convert-VMNoteTagsToObject)
    
    #>

    [CmdletBinding()]
    param(
        # VM with Tag object gathered by Convert-VMNoteTagsToObject
        [Parameter(Mandatory = $true)]
        [VMWithTag[]] 
        $VM
    )
    $EdgeList = (Get-EdgeHashtableFromVMNote -VM $VM -KeyProperty 'Environment' -ValueProperty 'Service')
    [array]$VMEnvironment = @()
    foreach ($Environment in $EdgeList.Keys)
    {
        $EnvironmentVM = $VM | Where-Object -Property Environment -eq $Environment
        $Service = Get-VMService -VM $EnvironmentVM
        $EnvironmentServiceEdgeList = Get-EdgeHashtableFromVMNote -VM $EnvironmentVM -KeyProperty 'Service' -ValueProperty 'DependsOn'
        $EnvironmentOrder = Get-TopologicalSort -edgeList $EnvironmentServiceEdgeList
        If (Compare-Object -ReferenceObject ($Service | Select-Object -ExpandProperty Name) -DifferenceObject $EnvironmentOrder)
        {
            Throw 'One or more required services are not provided by a VM'
        }
        [array]$VMEnvironment += [VMEnvironment]::new($Environment, $Service, $EnvironmentVM, $EnvironmentServiceEdgeList, $EnvironmentOrder)
    }
    $VMEnvironment
}
# .\PSHVTag\private\Get-VMService.ps1
function Get-VMService
{
    <#
    .SYNOPSIS
    Creates a (array of) VMService object(s)
    
    .DESCRIPTION
    Creates a (array of) VMService object(s) based on the list of tagged VMs defined by the parameter VM.
    
    .PARAMETER VM
    A (list of) VM object(s) with Tags from which to build the VMEnvironment object
    
    .EXAMPLE
    Get-VMService -VM (Get-VMWithTag | Convert-VMNoteTagsToObject)
    
    #>

    [CmdletBinding()]
    [OutputType([array])]
    param(
        # VM with Tag object gathered by Convert-VMNoteTagsToObject
        [Parameter(Mandatory = $true)]
        [VMWithTag[]] 
        $VM
    )
    $EdgeList = Get-EdgeHashtableFromVMNote -VM $VM -KeyProperty 'Service' -ValueProperty 'DependsOn'
    [array]$VMService = @()
    foreach ($Service in $EdgeList.Keys)
    {
        [array]$VmService += [VMService]::new($Service, ($VM | Where-Object -Property Service -eq $Service), $EdgeList.Item($Service))
    }
    $VMService
}
# .\PSHVTag\private\Get-VMWithTag.ps1
function Get-VMWithTag
{
    <#
    .SYNOPSIS
    Gets all VMs containing Tags on a given host
    
    .DESCRIPTION
    Gets all VMs containing Tags on a Hyper-V-Host given by the parameter Computername
    
    .PARAMETER Computername
    The Hyper-V-Host getting the VMs conatining Tags from
    
    .EXAMPLE
    Get-VMWithTag -Computername localhost
    
    #>

    [CmdletBinding()]
    param(
        # Specifies the VM Host.
        [Parameter(Mandatory = $false,
            Position = 0,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage = "Name of VM Host computer(s)")]
        [ValidateNotNullOrEmpty()]
        [string]
        $Computername = 'localhost'
    )
    Get-VM -ComputerName $Computername | Where-Object {$_.Notes -match '\<.*\>'}
}

Write-Verbose 'Importing from [C:\MyProjects\PSHVTag\PSHVTag\public]'
# .\PSHVTag\public\Get-VMTopology.ps1
function Get-VMTopology
{
    <#
    .SYNOPSIS
    Gets a VM Topology object for tagged VMs
    
    .DESCRIPTION
    Gets a VM Topology object for tagged VMs on the Hyper-V-Host defined by the paramater Computername
    
    .PARAMETER Computername
    A Hyper-V-Host with tagged VMs
    
    .EXAMPLE
    Get-VMTopology -Computername hyper-v-host.contoso.com
    
    #>

    [CmdletBinding()]
    param(
        # Parameter help description
        [Parameter(Mandatory = $false)]
        [string]
        $Computername = 'localhost'
    )
    $VM = Get-VMWithTag -Computername $Computername | ForEach-Object {Convert-VMNoteTagsToObject -VM $_}
    $Environment = Get-VMEnvironment -VM $VM
    $VMTopology = [VMTopology]::new($Computername, $VM, $Environment)
    $VMTopology
}
# .\PSHVTag\public\Get-VMTopologyGraph.ps1
#Requires -Modules PSGraph
function Get-VMTopologyGraph
{
    <#
    .SYNOPSIS
    Exports a Topological Graph
    
    .DESCRIPTION
    Exports a Topological Graph as JPEG and opens it
    
    .PARAMETER VMTopology
    A VM Topology object
    
    .EXAMPLE
    Get-VMTopology -VMTopology (Get-VMTopology)

    #>

    [CmdletBinding()]
    param(
        # VMTopology to show
        [Parameter(Mandatory = $true)]
        [VMTopology]
        $VMTopology
    )
    Import-Module PSGraph
    $Graph = graph "myGraph" {
        node -Default @{shape = 'box'}
        $subGraphID = 0
        ForEach ($Environment in $VMTopology.Environment)
        {
            subgraph $subGraphID -Attributes @{label = $Environment.Name} {
                Foreach ($Service in $Environment.Service)
                {
                    #Node ($Environment.Name + '|' + $Service.Name) @{label = $Service.Name}
                    Record -Name ($Environment.Name + '|' + $Service.Name) -Rows ((($VMTopology.Environment | Where-Object -Property Name -Value $Environment.Name -EQ).Service | Where-Object -Property Name -Value $Service.Name -EQ).VM.Name) -Label $Service.Name
                    ForEach ($DependsOn in $Service.DependsOn)
                    {
                        If ($DependsOn -ne '')
                        {
                            edge -from ($Environment.Name + '|' + $Service.Name)  -to ($Environment.Name + '|' + $DependsOn)
                        }
                    }      
                }
            }
            $subGraphID = $subGraphID + 1
        }
    } 
    $Graph
}
# .\PSHVTag\public\Set-VMTag.ps1
function Set-VMTag
{
    <#
    .SYNOPSIS
    Tag a VM
    
    .DESCRIPTION
    This function tags one or more VMs with the Environment, Service and DependsOn Tag
    
    .PARAMETER Environment
    The environment the VM belongs to
    
    .PARAMETER Service
    The service the VM provides
    
    .PARAMETER DependsOn
    The Services the VM depends on
    
    .PARAMETER VMName
    The name of the VM
    
    .PARAMETER VM
    The VM object
    
    .PARAMETER Computername
    The VM host
    
    .PARAMETER Force
    Overwrite existing values
    
    .EXAMPLE
    Set-VMTag -Environment 'LAB01' -Service 'Domain' -DependsOn @('Gateway','DHCP') -VMName 'DomainController01' -Force
    #>

    [CmdletBinding(SupportsShouldProcess = $true)]
    
    param(
        # Name of the VMEnvironment the VM belongs to
        [Parameter(Mandatory = $false)]
        [string[]]
        $Environment,
        # Name of the VMService the VM provides
        [Parameter(Mandatory = $false)]
        [string[]]
        $Service,
        # Name of the VMServices the VM depends on
        [Parameter(Mandatory = $false)]
        [string[]]
        $DependsOn,
        # The name of the VM
        [Parameter(ParameterSetName = 'Name', Mandatory = $true)]
        [string[]]
        $VMName,
        # The VM object
        [Parameter(ParameterSetName = 'VMObject', Mandatory = $true)]
        $VM,
        # Specifies the VM Host.
        [Parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [string]
        $Computername = 'localhost',
        # Force overwrite of existing Tag
        [Parameter()]
        [switch]
        $Force
    )
    Begin
    {
        $TagPattern = '\<Env\>.*\<\/Env\>\<Service\>.*\<\/Service\>\<DependsOn\>.*\<\/DependsOn\>'
        $returnObject = [pscustomobject]@{ Success = @(); Error = @()} 
        If ($PSCmdlet.ParameterSetName -eq 'Name')
        {
            $VM = @()
            foreach ($Name in $VMName)
            {
                try
                {
                    $VM += Get-VM -ComputerName $Computername | Where-Object -Property Name -EQ $Name
                }
                catch
                {
                    Write-Verbose "Can not find a VM with the name $Name at the host $Computername"
                    $returnObject.Error += $Name    
                }
            }
        }
        If (!($Vm))
        {
            Throw 'Can not find any VM for these parameters'
        }
    }
    Process
    {
        foreach ($Node in $VM)
        {
            If ($Node.Notes -Match $TagPattern -and (-not $Force))
            {
                Write-Verbose -Message "VM: $($VM.Name) already has a Tag and the paramater -force is not used"
                $returnObject.Error += $Node.Name  
            }
            elseif (($Node.Notes -Match $TagPattern -and $Force) -or ($Node.Notes -notMatch $TagPattern))
            {
                $Tag = '<Env>' + ($Environment -join ',') + '</Env><Service>' + ($Service -join ',') + '</Service><DependsOn>' + ($DependsOn -join ',') + '</DependsOn>'
                if ($Node.Notes -Match $TagPattern)
                {
                    $NewNotes = $Node.Notes -replace $TagPattern, $Tag
                }
                Elseif ($null -ne $Node.Notes -and $Node.Notes -ne '')
                {
                    $NewNotes = $Node.Notes + "`r`n" + $Tag + "`r`n"
                }
                Else
                {
                    $NewNotes = $Tag + "`r`n"
                }
                Write-Verbose -Message "Setting Tag on VM $($VM.Name) to $NewNotes"
                if ($PSCmdlet.ShouldProcess(('Setting VM Notes of: ' + $Node.Name + ' to ' + $NewNotes)))
                {
                    Try
                    {
                        Set-Vm -VM $Node -Notes $NewNotes
                        $returnObject.Success += $Node.Name 
                    }
                    catch
                    {
                        Write-Verbose -Message "Could not set Tag on VM $($VM.Name) to $NewNotes"
                        $returnObject.Error += $Node.Name 
                    }
                }
            }
        }
    }
    End
    {
        $returnObject
    }
}
# .\PSHVTag\public\Start-VMService.ps1
function Start-VMService
{
    <#
    .SYNOPSIS
    Starts all VMs of a VM Service in a VM Topology
    
    .DESCRIPTION
    Starts all VMs of a VM Service in a VM Topology and all VMs of required Vm Services
    
    .PARAMETER ServiceName
    The name of the VM Service to start
    
    .PARAMETER EnvironmentName
    The name of the VM Environment the VM Service is in
    
    .PARAMETER Service
    VMService object of the VM Service to start
    
    .PARAMETER Environment
    VMEnvironment object of the VM Service to start
    
    .PARAMETER VMTopology
    The VMTopology containing all VM Services and Environments
    
    .PARAMETER Recurse
    Start all required VM Services before starting the current VM Service
    
    .PARAMETER AdditionalWaitTime
    Additional time to wait after all VMs of a service are started successfully

    .PARAMETER VMWaitFor
    Item to wait for to determine if a VM is started successfully (IPAddress or Heartbeat)
    
    .EXAMPLE
    Start-VMService -ServiceName Domain -Environment Lab -VMTopology (Get-VMTopology) -Recurse
    
    #>

    [CmdletBinding(DefaultParameterSetName = 'String', SupportsShouldProcess = $true)]
    [OutputType([Bool])]
    param(
        # Name of the VM Service to start
        [Parameter(ParameterSetName = 'String', Mandatory = $true)]
        [Parameter(ParameterSetName = 'Object-String', Mandatory = $true)]     
        [string]
        $ServiceName,
        # Name of the Environment of the service to start
        [Parameter(ParameterSetName = 'String', Mandatory = $true)]
        [Parameter(ParameterSetName = 'String-Object', Mandatory = $true)]
        [string]
        $EnvironmentName,
        # Name of the VM Service to start
        [Parameter(ParameterSetName = 'Object', Mandatory = $true)]
        [Parameter(ParameterSetName = 'String-Object', Mandatory = $true)]     
        [VMService]
        $Service,
        # Name of the Environment of the service to start
        [Parameter(ParameterSetName = 'Object', Mandatory = $true)]
        [Parameter(ParameterSetName = 'Object-String', Mandatory = $true)]     
        [VMEnvironment]
        $Environment,
        # VMTopology
        [Parameter(Mandatory = $true)]
        [VMTopology]
        $VMTopology,
        # Start VM Services recursively
        [Parameter()]
        [switch]
        $Recurse,
        # Additional seconds to wait after VM is started
        [Parameter()]
        [int]
        $AdditionalWaitTime = 20,
        # Wait For
        [Parameter()]
        [String]
        [ValidateSet('IPAddress', 'Heartbeat')]
        $VMWaitFor = 'IPAddress'
    )
    Begin
    {
        switch ($PsCmdlet.ParameterSetName)
        {
            'Object'
            { 
                $ServiceName = $Service.Name
                $EnvironmentName = $Environment.Name
            }
            'Object-String'
            {
                $EnvironmentName = $Environment.Name
            }
            'String-Object'
            {
                $ServiceName = $Service.Name
            }
        }
        $Service = ($VMTopology.Environment | where-object -property name -eq $EnvironmentName).Service | Where-Object Name -eq $ServiceName
        If (!($Service))
        {
            Throw ('Could not find any VM for the service ' + $ServiceName + ' in the environment ' + $EnvironmentName)
        }
    }
    Process
    {
        if ($Recurse)
        {
            foreach ($Dependency in $Service.DependsOn )
            {
                Write-Verbose ('Starting required service: ' + $Dependency)
                Start-VMService -ServiceName $Dependency -EnvironmentName $EnvironmentName -VMTopology $VMTopology -Recurse | Out-Null
            }
        }
        foreach ($Node in $Service.VM)
        {
            If ($Node.VM.State -ne 'Running')
            {
                if ($PSCmdlet.ShouldProcess(('Starting VM: ' + $Node.Name)))
                {
                    Start-VM -VM $Node.Vm
                    $State = Wait-VM -VM $Node.vm -For $VMWaitFor -Timeout 120 -Passthru
                    Start-Sleep -Seconds $AdditionalWaitTime
                    If (!($State.State -eq 'Running'))
                    {
                        Throw ('Could not start VM: ' + $Node.Name)
                    }
                }
            }            
        }
    }
    End
    {
        Write-Verbose ('Successfully started VM Service: ' + $ServiceName)
        $true
    }
}
# .\PSHVTag\public\Stop-VMService.ps1
function Stop-VMService
{
    <#
    .SYNOPSIS
    Stopps all VMs of a VM Service in a VM Topology
    
    .DESCRIPTION
    Stopps all VMs of a VM Service in a VM Topology and all VMs of required Vm Services
    
    .PARAMETER ServiceName
    The name of the VM Service to start
    
    .PARAMETER EnvironmentName
    The name of the VM Environment the VM Service is in
    
    .PARAMETER Service
    VMService object of the VM Service to stop
    
    .PARAMETER Environment
    VMEnvironment object of the VM Service to stop
    
    .PARAMETER VMTopology
    The VMTopology containing all VM Services and Environments
    
    .PARAMETER Recurse
    Stop all required VM Services after stopping the current VM Service
    
    .PARAMETER Force
    Force shutdown of VMs
    
    .EXAMPLE
    Stop-VMService -ServiceName Domain -EnvironmentName Lab -VMTopology (Get-VMTopology) -Recurse
    
    #>

    [CmdletBinding(DefaultParameterSetName = 'String', SupportsShouldProcess = $true)]
    [OutputType([Bool])]
    param(
        # Name of the VM Service to stop
        [Parameter(ParameterSetName = 'String', Mandatory = $true)]
        [Parameter(ParameterSetName = 'Object-String', Mandatory = $true)]     
        [string]
        $ServiceName,
        # Name of the Environment of the service to stop
        [Parameter(ParameterSetName = 'String', Mandatory = $true)]
        [Parameter(ParameterSetName = 'String-Object', Mandatory = $true)]
        [string]
        $EnvironmentName,
        # Name of the VM Service to stop
        [Parameter(ParameterSetName = 'Object', Mandatory = $true)]
        [Parameter(ParameterSetName = 'String-Object', Mandatory = $true)]     
        [VMService]
        $Service,
        # Name of the Environment of the service to stop
        [Parameter(ParameterSetName = 'Object', Mandatory = $true)]
        [Parameter(ParameterSetName = 'Object-String', Mandatory = $true)]     
        [VMEnvironment]
        $Environment,
        # VMTopology
        [Parameter(Mandatory = $true)]
        [VMTopology]
        $VMTopology,
        # Stop VM Services recursively
        [Parameter()]
        [switch]
        $Recurse,
        # Force stopping of VM
        [Parameter()]
        [switch]
        $Force
    )
    Begin
    {
        switch ($PsCmdlet.ParameterSetName)
        {
            'Object'
            { 
                $ServiceName = $Service.Name
                $EnvironmentName = $Environment.Name
            }
            'Object-String'
            {
                $EnvironmentName = $Environment.Name
            }
            'String-Object'
            {
                $ServiceName = $Service.Name
            }
        }
        $Service = ($VMTopology.Environment | where-object -property name -eq $EnvironmentName).Service | Where-Object Name -eq $ServiceName
        If (!($Service))
        {
            Throw ('Could not find the service ' + $ServiceName + ' in the environment ' + $EnvironmentName)
        }
    }
    Process
    {
        foreach ($Node in $Service.VM)
        {
            If ($Node.VM.State -ne 'Off')
            {
                if ($PSCmdlet.ShouldProcess(('Stopping VM: ' + $Node.Name)))
                {
                    If ($Force)
                    {
                        $State = Stop-VM -VM $Node.Vm -Passthru -Force
                    }
                    else
                    {
                        $State = Stop-VM -VM $Node.Vm -Passthru
                    }
                    If (!($State.State -eq 'Off'))
                    {
                        Throw ('Could not stop VM: ' + $Node.Name)
                    }
                }
            }            
        }
        if ($Recurse)
        {
            foreach ($Dependency in $Service.DependsOn )
            {
                Write-Verbose ('Stopping required service: ' + $Dependency)
                If ($Force)
                {
                    Stop-VMService -ServiceName $Dependency -EnvironmentName $EnvironmentName -VMTopology $VMTopology -Recurse -Force | Out-Null
                }
                else
                {
                    Stop-VMService -ServiceName $Dependency -EnvironmentName $EnvironmentName -VMTopology $VMTopology -Recurse | Out-Null
                }
            }
        }
    }
    End
    {
        Write-Verbose ('Successfully stopped VM Service: ' + $ServiceName)
        $true
    }
}
Write-Verbose 'Importing from [C:\MyProjects\PSHVTag\PSHVTag\classes]'
# .\PSHVTag\classes\1VMWithTag.ps1
# VM with Tag
class VMWithTag
{
    # Name of the VM
    [string] $Name
    # VM ID
    $VM
    # Environment
    [String[]] $Environment
    # Service provided by VM
    [String[]] $Service
    # DependsOn Services
    [String[]] $DependsOn

    # Constructor
    VMWithTag ([string] $name, $VM, [String[]]$Environment, [String[]] $Service, [String[]] $DependsOn)
    {
        $this.Name = $name
        $this.VM = $VM
        $this.Environment = $Environment
        $this.Service = $Service
        $this.DependsOn = $DependsOn
    }
}
# .\PSHVTag\classes\2VMService.ps1
class VMService
{
    [string]$Name
    [VMWithTag[]]$VM
    [array]$DependsOn
    VMService ([string]$Name, [VMWithTag[]]$VM, [array]$DependsOn)
    {
        $this.Name = $Name
        $this.VM = $VM
        $this.DependsOn = $DependsOn
    }
}
# .\PSHVTag\classes\3VMEnvironment.ps1
class VMEnvironment
{
    [string]$Name
    [VMService[]]$Service
    [VMWithTag[]] $VM
    [System.Collections.ArrayList]$EdgeList
    [Array]$Order
    
    VMEnvironment ([string] $Name, [VMService[]]$Service, [VMWithTag[]] $VM, [System.Collections.ArrayList]$EdgeList, [Array]$Order)
    {
        $this.Name = $Name
        $this.Service = $Service
        $this.VM = $VM
        $this.EdgeList = $EdgeList
        $this.Order = $Order
    }
}
# .\PSHVTag\classes\4VMTopology.ps1
class VMTopology
{
    [string] $Computername
    [VMWithTag[]] $VM
    [VMEnvironment[]] $Environment
    # Constructor
    VMTopology ([string] $Computername, [VMWithTag[]] $VM, [VMEnvironment[]] $Environment)
    {
        $this.Computername = $Computername
        $this.VM = $VM
        $this.Environment = $Environment
    }
}