[string] $kempUser = "$RunAs[Name='CO.Kemp.KempRunasProfile']/UserName$" # Not sure if you can do this, lets try
[string] $kempPass = "$RunAs[Name='CO.Kemp.KempRunasProfile']/Password$" # Not sure if you can do this, lets try
# Create SCOM API Object
$scomAPI = New-Object -comObject 'MOM.ScriptAPI'
$knownDebugHosts = @(
"Visual Studio Code Host"
"Windows PowerShell ISE Host"
)
if ($host.Name -in $knownDebugHosts) {
# script is running in a known debug environment, set debug values
$tempDir = "$env:TEMP\CO.Kemp"
# needed a slightly more secure way to debug these scripts
# using serialized credentials (encrypted) from file
# if file is missing, script will ask for credentials, and save them for later use
$credPath = $tempDir + "\kempcreds.xml"
if (Test-Path -Path $credPath) {
$credentials = Import-Clixml -Path $credPath
} else {
$credentials = Get-Credential -Message "Enter Kemp Login"
Export-Clixml -Path $credPath -InputObject $credentials
}
[string] $kempUser = $credentials.UserName
[string] $kempPass = $credentials.GetNetworkCredential().Password
$LoadMasterBaseUrls = @("https://avmk01.westeurope.cloudapp.azure.com:8443/") #my free tier azure appliance, perfect for development, may be offline
if (!(Test-Path -Path $tempDir)) {New-Item -Path $tempDir -ItemType Directory}
$isDebugging = $true
}
else {
# Get all LoadMaster instances and their base urls
$lmClass = Get-SCOMClass -Name "CO.Kemp.LoadMaster"
$lmInstances = Get-SCOMClassInstance -Class $lmClass
[string[]] $LoadMasterBaseUrls = $lmInstances.'[CO.Kemp.LoadMaster].managementurl'
}
$scomAPI.LogScriptEvent($scriptName, $eventId, 0, "`nScript probe started by $(whoami) in `"$($host.Name)`".`nLoadMasterBaseUrls = $($LoadMasterBaseUrls -join ",")`nDebug=$($isDebugging)")
$logString = "`n"
#region KempClass
class Kemp {
# Kemp Base URL (LoadMaster admin adress?)
[string] $AdminAdress
[System.Net.NetworkCredential] $Credential
[System.Xml.XmlElement] $StatsXml
[System.Xml.XmlElement] LoadStatsXML() {
# using this member to avoid multiple API-checks when data is already loaded
if ($null -eq $this.StatsXml) {
#TODO: Need better StatsXML check
$statsResult = $this.QueryKempApi("access/stats", @{}).Response.Success.Data
#$statsResult.InnerXml | Out-File -FilePath ".\stats.xml" -Force
$this.StatsXml = $statsResult
}
return $this.StatsXml
}
hidden [Boolean] ValidateUrl($Url) {
if (!$Url.StartsWith("https://")) {
return $false
}
return $true
}
}
#endregion KempClass
function Send-PropertyBag {
param (
[System.Collections.ArrayList] $Properties
,[bool] $Debug
)
$omApi = New-Object -ComObject "MOM.ScriptApi"
foreach($propertyHT in $Properties) {
$pb = $omApi.CreatePropertyBag()
foreach ($key in $propertyHT.Keys) {
$pb.AddValue($key, $propertyHT[$key])
}
if ($Debug) {
$omApi.AddItem($pb)
}
else {
$pb
}
}
if ($Debug) {
$omApi.ReturnItems()
$scomAPI.LogScriptEvent($scriptName, $eventId, 0, "`nTHIS EVENT IS ONLY IN DEBUG!!!")
}
}
try {
$vsHt = $kemp.GetVirtualServices() #VirtualService (incl. SubVS) information
$rsHt = $kemp.GetRealServers() # RealServer information
$allHt = $kemp.GetAll() # This is where you get LoadMaster node information
$vsStatsHt = $kemp.GetVSStats()
$rsStatsHt = $kemp.GetRSStats()
$lmStatsHt = $kemp.GetLMStats()
# Cluster API is not accessible unless you're admin, we'll deal with that later
#$clHt = $kemp.GetClusters()
}
catch {
Continue
}
# Saving to disk, only for manual analysis during development
if ($isDebugging) {
$vsHt | ConvertTo-Json | Out-File -FilePath "$tempDir\vs.json"
$rsHt | ConvertTo-Json | Out-File -FilePath "$tempDir\rs.json"
$allHt | ConvertTo-Json | Out-File -FilePath "$tempDir\all.json"
$vsStatsHt | ConvertTo-Json | Out-File -FilePath "$tempDir\vsStatsHt.json"
$rsStatsHt | ConvertTo-Json | Out-File -FilePath "$tempDir\rsStatsHt.json"
$lmStatsHt | ConvertTo-Json | Out-File -FilePath "$tempDir\lmStatsHt.json"
# Cluster API is not accessible unless you're admin, we'll deal with that later
#$clHt | ConvertTo-Json | Out-File -FilePath "$($env:TEMP)\cl.json"
}
if ($allHt.Count -gt 0) {
# got data in allHT, which means there's a LoadMaster returned.
[string] $identifier = $allHt.managementhost
if (($allHt.ha1hostname.Length -gt 0) -and ($allHt.hostname -eq $allHt.ha1hostname)) {
# ha1 is active
$ha1Active = 1
$ha2Active = 0
} elseif (($allHt.ha2hostname.Length -gt 0) -and ($allHt.hostname -eq $allHt.ha2hostname)) {
# ha2 is active
$ha1Active = 0
$ha2Active = 1
} else {
# not a cluster
$ha1Active = 0
$ha2Active = 0
}