WiP/Function.GetObject.WiP.ps1

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
#Lookup ResourceId from Name
Function GetObject([String]$vRopsObjName, [String]$vRopsServer, $User, $Password){

$wc = new-object system.net.WebClient
$wc.Credentials = new-object System.Net.NetworkCredential($User, $Password)
[xml]$Checker = $wc.DownloadString("https://$vRopsServer/suite-api/api/resources?name=$vRopsObjName")

$AlertReport = @()

# Check if we get more than 1 result and apply some logic
    If ([Int]$Checker.resources.pageInfo.totalCount -gt '1') {

        $DataReceivingCount = $Checker.resources.resource.resourceStatusStates.resourceStatusState.resourceStatus -eq 'DATA_RECEIVING'

            If ($DataReceivingCount.count -gt 1){
            $CheckerOutput = ''
            return $CheckerOutput 
            }
            
            Else 
            {

            ForEach ($Result in $Checker.resources.resource){

                IF ($Result.resourceStatusStates.resourceStatusState.resourceStatus -eq 'DATA_RECEIVING'){

                     $PropertiesLink = $Result.links.link | where Name -eq 'latestPropertiesOfResource'
                     $Propertiesurl = 'https://' +$vRopsServer + $PropertiesLink.href
                     [xml]$Properties = $wc.DownloadString($Propertiesurl)

                     switch($Result.resourceKey.resourceKindKey)
                        {

                        VirtualMachine {

                            $ParentvCenter = $Properties.'resource-property'.property | where name -eq 'summary|parentVcenter' | Select '#text'
                            $ParentCluster = $Properties.'resource-property'.property | where name -eq 'summary|parentCluster' | Select '#text'
                            $ParentHost = $Properties.'resource-property'.property | where name -eq 'summary|parentHost' | Select '#text'
                            $PowerState = $Properties.'resource-property'.property | where name -eq 'summary|runtime|powerState' | Select '#text'
                            $Memory = $Properties.'resource-property'.property | where name -eq 'config|hardware|memoryKB' | Select '#text'
                            $CPU = $Properties.'resource-property'.property | where name -eq 'config|hardware|numCpu' | Select '#text'
                            $INFO = $Properties.'resource-property'.property | where name -eq 'config|guestFullName' | Select '#text'

                            }


                        HostSystem {

                            $ParentvCenter = $Properties.'resource-property'.property | where name -eq 'summary|parentVcenter' | Select '#text'
                            $ParentCluster = $Properties.'resource-property'.property | where name -eq 'summary|parentCluster' | Select '#text'
                            $ParentHost = $Properties.'resource-property'.property | where name -eq 'summary|parentHost' | Select '#text'
                            $PowerState = $Properties.'resource-property'.property | where name -eq 'runtime|powerState' | Select '#text'
                            $Memory = $Properties.'resource-property'.property | where name -eq 'runtime|memoryCap' | Select '#text'
                            $CPU = $Properties.'resource-property'.property | where name -eq 'hardware|cpuInfo|numCpuPackages' | Select '#text'
                            $CPUcores = $Properties.'resource-property'.property | where name -eq 'hardware|cpuInfo|numCpuCores' | Select '#text'
                            $INFO = $Properties.'resource-property'.property | where name -eq 'cpu|cpuModel' | Select '#text'

                            }

                     }
                    $CheckerOutput = New-Object PsObject -Property @{Name=$vRopsObjName; resourceId=$Result.identifier; resourceKindKey=$Result.resourceKey.resourceKindKey; vCenter=$ParentvCenter.'#text'; Cluster=$ParentCluster.'#text'; Host=$ParentHost.'#text'; State=$PowerState.'#text'; Memory=([Int]$Memory.'#text')/1024/1024; CPU=([Int]$CPU.'#text'); CPUcores=([Int]$CPUcores.'#text'); INFO=$INFO.'#text'}

                    #GetAlerts
                     $ResID = $CheckerOutput.resourceId
                     [xml]$Alerts = $wc.DownloadString("https://$vRopsServer/suite-api/api/alerts?resourceId=$ResID")

                     ForEach ($Alert in $alerts.alerts.alert){

                        $AlertReport += New-Object PSObject -Property @{

                            Name                = $vRopsObjName
                            alertDefinitionName = $Alert.alertDefinitionName
                            alertLevel          = $Alert.alertLevel
                            status              = $Alert.status
                            controlState        = $Alert.controlState
                            startTime           = If ([int64]$Alert.startTimeUTC -gt '') {([TimeZone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddMilliSeconds([int64]$Alert.startTimeUTC))).tostring("dd/MM/yyyy HH:mm:ss")} else {}
                            cancelTime          = If ([int64]$Alert.cancelTimeUTC -gt '') {([TimeZone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddMilliSeconds([int64]$Alert.cancelTimeUTC))).tostring("dd/MM/yyyy HH:mm:ss")} else {}
                            updateTime          = If ([int64]$Alert.updateTimeUTC -gt '') {([TimeZone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddMilliSeconds([int64]$Alert.updateTimeUTC))).tostring("dd/MM/yyyy HH:mm:ss")} else {}
                            suspendUntilTime    = If ([int64]$Alert.suspendUntilTimeUTC -gt ''){([TimeZone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddMilliSeconds([int64]$Alert.suspendUntilTimeUTC))).tostring("dd/MM/yyyy HH:mm:ss")} else {}
                            alertId             = $Alert.alertId

                        }

                    }

                    Return $CheckerOutput, $AlertReport
                    
                }   
            }
    }  
 }
    else
    {

                     $PropertiesLink = $Checker.resources.resource.links.link | where Name -eq 'latestPropertiesOfResource'
                     $Propertiesurl = 'https://' +$vRopsServer + $PropertiesLink.href
                     [xml]$Properties = $wc.DownloadString($Propertiesurl)

                     switch($Checker.resources.resource.resourceKey.resourceKindKey)
                        {

                        VirtualMachine {

                            $ParentvCenter = $Properties.'resource-property'.property | where name -eq 'summary|parentVcenter' | Select '#text'
                            $ParentCluster = $Properties.'resource-property'.property | where name -eq 'summary|parentCluster' | Select '#text'
                            $ParentHost = $Properties.'resource-property'.property | where name -eq 'summary|parentHost' | Select '#text'
                            $PowerState = $Properties.'resource-property'.property | where name -eq 'summary|runtime|powerState' | Select '#text'
                            $Memory = $Properties.'resource-property'.property | where name -eq 'config|hardware|memoryKB' | Select '#text'
                            $CPU = $Properties.'resource-property'.property | where name -eq 'config|hardware|numCpu' | Select '#text'
                            $INFO = $Properties.'resource-property'.property | where name -eq 'config|guestFullName' | Select '#text'

                            }


                        HostSystem {

                            $ParentvCenter = $Properties.'resource-property'.property | where name -eq 'summary|parentVcenter' | Select '#text'
                            $ParentCluster = $Properties.'resource-property'.property | where name -eq 'summary|parentCluster' | Select '#text'
                            $ParentHost = $Properties.'resource-property'.property | where name -eq 'summary|parentHost' | Select '#text'
                            $PowerState = $Properties.'resource-property'.property | where name -eq 'runtime|powerState' | Select '#text'
                            $Memory = $Properties.'resource-property'.property | where name -eq 'runtime|memoryCap' | Select '#text'
                            $CPU = $Properties.'resource-property'.property | where name -eq 'hardware|cpuInfo|numCpuPackages' | Select '#text'
                            $CPUcores = $Properties.'resource-property'.property | where name -eq 'hardware|cpuInfo|numCpuCores' | Select '#text'
                            $INFO = $Properties.'resource-property'.property | where name -eq 'cpu|cpuModel' | Select '#text'

                            }

                     }
    
    $CheckerOutput = New-Object PsObject -Property @{Name=$vRopsObjName; resourceId=$Checker.resources.resource.identifier; resourceKindKey=$Checker.resources.resource.resourceKey.resourceKindKey; vCenter=$ParentvCenter.'#text'; Cluster=$ParentCluster.'#text'; Host=$ParentHost.'#text'; State=$PowerState.'#text'; Memory=([Int]$Memory.'#text')/1024/1024; CPU=([Int]$CPU.'#text'); CPUcores=([Int]$CPUcores.'#text'); INFO=$INFO.'#text'}

                    #GetAlerts
                     $ResID = $CheckerOutput.resourceId
                     [xml]$Alerts = $wc.DownloadString("https://$vRopsServer/suite-api/api/alerts?resourceId=$ResID")

                     ForEach ($Alert in $alerts.alerts.alert){

                        $Alertreport += New-Object PSObject -Property @{

                            Name                = $vRopsObjName
                            alertDefinitionName = $Alert.alertDefinitionName
                            alertLevel          = $Alert.alertLevel
                            status              = $Alert.status
                            controlState        = $Alert.controlState
                            startTime           = If ([int64]$Alert.startTimeUTC -gt '') {([TimeZone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddMilliSeconds([int64]$Alert.startTimeUTC))).tostring("dd/MM/yyyy HH:mm:ss")} else {}
                            cancelTime          = If ([int64]$Alert.cancelTimeUTC -gt '') {([TimeZone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddMilliSeconds([int64]$Alert.cancelTimeUTC))).tostring("dd/MM/yyyy HH:mm:ss")} else {}
                            updateTime          = If ([int64]$Alert.updateTimeUTC -gt '') {([TimeZone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddMilliSeconds([int64]$Alert.updateTimeUTC))).tostring("dd/MM/yyyy HH:mm:ss")} else {}
                            suspendUntilTime    = If ([int64]$Alert.suspendUntilTimeUTC -gt ''){([TimeZone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddMilliSeconds([int64]$Alert.suspendUntilTimeUTC))).tostring("dd/MM/yyyy HH:mm:ss")} else {}
                            alertId             = $Alert.alertId

                        }

                    }

                    Return $CheckerOutput, $AlertReport

    }
}