sClass.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
class sClass
{
    static [pscustomobject] GetProtGrpInfo ([psobject] $pgrp)
    {
        $pgpn = $null
        $pgnm = $pgrp.GetInfo().Name.ToString()
        $pgvm = $pgrp.ListProtectedVms()
        $pgcn = $pgvm.Where({$_.NeedsConfiguration -eq $true}).VmName
        $pgfl = $pgvm.Where({$null -ne $_.Faults}).VmName
        $pgok = $pgrp.CheckConfigured()
        $pgst = $pgrp.GetProtectionState().ToString()

        switch ($pgok)
        {
            {$_ -eq $false -and $pgst -ne 'Shadowing'}
            {
                $pgpn = [sClass]::GetUnProtVM( ($pgrp.ListProtectedDatastores().Moref) , ($pgvm.VmName) )    #Change
                break
            }

            {$_ -eq $false -and $pgst -eq 'Shadowing'}
            {
                $pgpn = "See Help"
                break
            }
        }

        $lo = [pscustomobject]@{
            Name = $pgnm
            State = $pgst
            ConfigOK = $pgok
            ConfigNeeded = $pgcn
            ProtectionNeeded = $pgpn
            Faults = $pgfl
        }
        return $lo
    }

    static [psobject] GetUnProtVM ( [psobject] $pgds , [psobject] $pgvn )
    {
        $dss = Get-Datastore -id $pgds
        $vms = (Get-VM -Datastore $dss).Name
        $npt = $vms.Where({$pgvn -notcontains $_})

        if ($npt)
        {
            return $npt
        }
        else
        {
            return $null
        }
    }

    static [hashtable] MakeHash( [string] $quoi )
    {
        $src = $null
        $shash = @{}

        switch ($quoi)
        {
            ds { $src = Get-Datastore -Name * }
            ex { $src = Get-VMHost -Name * }
            vm { $src = Get-VM -Name * }
        }

        foreach ($s in $src)
        {
            $shash.add($s.Id , $s.Name)
        }
        return $shash
    }

    static [hashtable] MakePgHash ([psobject] $pgroups )
    {
        $pghash = @{}
        foreach ($p in $pgroups)
        {
            $pghash.Add($p.ListProtectedDatastores().Moref,$p)
        }
        return $pghash
    }

    static [pscustomobject] MakeObj( [string] $reason , [string] $VMname, [string] $VMmoref )
    {
        $nil = "None"
        $lo = [pscustomobject]@{
            VM = $VMname
            VMMoRef = $VMmoref
            Status = "Not Attempted. "+$reason
            Error = $nil
            Task = $nil
            TaskMoRef = $nil
        }
        $lo.PSObject.TypeNames.Insert(0,'SupSkiFun.SRM.Protect.Info')
        return $lo
    }

    static [pscustomobject] MakeObj( [psobject] $protstat , [string] $VMname, [string] $VMmoref , [string] $VMdsName)
    {
        $lo = [pscustomobject]@{
            VM = $VMname
            VMMoRef = $VMmoref
            Status = $protstat.Status.ToString()
            DataStore = $VMdsName
            ProtectionGroup = $protstat.ProtectionGroupName
            RecoveryPlan = $protstat.RecoveryPlanNames
            ProtectedVm    = $protstat.ProtectedVm
            PeerProtectedVm = $protstat.PeerProtectedVm
        }
        $lo.PSObject.TypeNames.Insert(0,'SupSkiFun.SRM.VM.Info')
        return $lo
    }

    static [pscustomobject] MakeObj( [string] $reason , [string] $VMname, [string] $VMmoref , [string] $VMdsName , [string] $nd )
    {
        $lo = [pscustomobject]@{
            VM = $VMname
            VMMoRef = $VMmoref
            Status = "Not Attempted. "+$reason
            DataStore = $VMdsName
            ProtectionGroup = $nd
            RecoveryPlan = $nd
            ProtectedVm    = $nd
            PeerProtectedVm = $nd
        }
        $lo.PSObject.TypeNames.Insert(0,'SupSkiFun.SRM.VM.Info')
        return $lo
    }

    static [pscustomobject] MakePGInfoObj ( [psobject] $pg )
    {
        $pginfo = $pg.GetInfo()
        $pgvms = $pg.ListProtectedVms().VMName
        $vmpgcnt = $pgvms.count

        $lo = [pscustomobject] @{
            ProtectionGroup = $pginfo.Name
            Description = $pginfo.Description
            Configured = $pg.CheckConfigured()
            State = $pg.GetProtectionState().ToString()
            Type = $pginfo.Type.ToString()
            Category = $pg.GetType().Name
            VMCount = $vmpgcnt
            VMNames = $pgvms
        }
        $lo.PSObject.TypeNames.Insert(0,'SupSkiFun.SRM.Protection.Group.Info')
        return $lo
    }

    static [pscustomobject] MakeRPInfoObj ( [psobject] $rp )
    {
        $arr1 = [System.Collections.Arraylist]::new()
        $arr2 = [System.Collections.Arraylist]::new()
        [int] $rpvmcnt = 0
        $rpinfo = $rp.GetInfo()
        $prgcnt = $rpinfo.ProtectionGroups.Count
        foreach ($pg in $rpinfo.ProtectionGroups)
        {
            $pgo = [sClass]::MakePGInfoObj($pg)
            $arr1.add($pgo)
            $qpg = [sClass]::QueryPGObj($pgo)
            $rpvmcnt += $qpg.Count
            if ($qpg.Name)
            {
                $arr2.add($qpg.Name)
            }
        }

        if ( $arr2 )
        {
            $MTpg = $true
        }
        elseif ($prgcnt -eq 0)
        {
            $MTpg = "NoProtectionGroupsExist"
        }
        else
        {
            $MTpg = $false
        }

        $lo = [pscustomobject]@{
            RecoveryPlan = $rpinfo.Name
            Description = $rpinfo.Description
            State = $rpinfo.State.ToString()
            Type = $rp.GetType().Name.ToString()
            RecoveryPlanVMCount = $rpvmcnt
            EmptyProtectionGroup = $MTpg
            EmptyProtectionGroupName = $arr2
            ProtectionGroupCount = $prgcnt
            ProtectionGroups = $arr1
        }
        $lo.PSObject.TypeNames.Insert(0,'SupSkiFun.SRM.Recovery.Plan.Info')
        return $lo
    }

    static [hashtable] QueryPGObj ( [psobject] $pgo )
    {
        $nom = ""
        $vmc = $pgo.VMCount
        if ($vmc -eq 0)
        {
            $nom = $pgo.ProtectionGroup
        }
        $hash = @{
            Name = $nom
            Count = $vmc
        }
        return $hash
    }
}