SharedLibCode (DeployableResource)

Element properties:

TypeDeployableResource
File NameShared.ps1
AccessibilityPublic

Source Code:

<DeployableResource ID="SharedLibCode" Accessibility="Public" FileName="Shared.ps1" HasNullStream="false"/>

File Content: Shared.ps1

function GetLogLevel ($default){

if (Test-Path HKLM:\SOFTWARE\PureStorage\SCOM\LogLevel){
$item = Get-ItemProperty "HKLM:\SOFTWARE\PureStorage\SCOM\LogLevel"
$log_level = $item.'(default)'
$log_level
}
else
{
$default
}
}

function InitLogging () {
$GLOBAL:MOMAPI = New-Object -comObject "Mom.ScriptAPI"
$GLOBAL:mpVersion = (Get-SCOMManagementPack -name "PureStorageFlashArray").Version.ToString()
if(!$GLOBAL:mpVersion) {$GLOBAL:mpVersion = 'unknown'}
$GLOBAL:clientName = "SCOM"
##### LOG LEVEL ####
# 1 Errors
# 2 Warnings
# 3 Information
# 4 Verbose
$GLOBAL:ERROR_LEVEL = 1
$GLOBAL:WARNING_LEVEL = 2
$GLOBAL:INFO_LEVEL = 3
$GLOBAL:VERBOSE_LEVEL = 4

$GLOBAL:CURRENT_LOG_LEVEL = GetLogLevel $GLOBAL:VERBOSE_LEVEL

$GLOBAL:LOG_CODES_MAP = @{}
$GLOBAL:DEFAULT_LOG_CODE = 5555
################## Discoveries ##################
$GLOBAL:LOG_CODES_MAP.Add("PureVolumeDiscovery.ps1", 2001)
$GLOBAL:LOG_CODES_MAP.Add("PurePortDiscovery.ps1", 2002)
$GLOBAL:LOG_CODES_MAP.Add("PureHostDiscovery.ps1", 2003)
$GLOBAL:LOG_CODES_MAP.Add("PureControllerDiscovery.ps1", 2004)
$GLOBAL:LOG_CODES_MAP.Add("PureArrayDiscovery.ps1", 2005)
$GLOBAL:LOG_CODES_MAP.Add("InitialArrayDiscovery.ps1", 2006)
##################### Rules #######################
$GLOBAL:LOG_CODES_MAP.Add("AlertsRule.ps1", 2101)
$GLOBAL:LOG_CODES_MAP.Add("ArrayPerformanceRule.ps1", 2102)
$GLOBAL:LOG_CODES_MAP.Add("HostgroupPerformanceRule.ps1", 2103)
$GLOBAL:LOG_CODES_MAP.Add("HostPerformanceRule.ps1", 2104)
$GLOBAL:LOG_CODES_MAP.Add("VolumePerformanceRule.ps1", 2105)
#################### Monitors ######################
$GLOBAL:LOG_CODES_MAP.Add("AlertsRuleMonitor.ps1", 2201)
$GLOBAL:LOG_CODES_MAP.Add("ConnectionHealthMonitor.ps1", 2203)
$GLOBAL:LOG_CODES_MAP.Add("ControllerHealthMonitor.ps1", 2204)
$GLOBAL:LOG_CODES_MAP.Add("VolumeIOMonitor.ps1", 2205)
$GLOBAL:LOG_CODES_MAP.Add("HostIOMonitor.ps1", 2206)
$GLOBAL:LOG_CODES_MAP.Add("PortHealthMonitor.ps1", 2207)
$GLOBAL:LOG_CODES_MAP.Add("PodMediatorStatus.ps1", 2208)
##################### Tasks #######################
$GLOBAL:LOG_CODES_MAP.Add("NewHostTask.ps1", 2301)
$GLOBAL:LOG_CODES_MAP.Add("NewVolumeTask.ps1", 2302)
$GLOBAL:LOG_CODES_MAP.Add("VerifyEndpointTask.ps1", 2303)
################### Dashboards ####################
$GLOBAL:LOG_CODES_MAP.Add("PureVolumeState.ps1", 2401)
$GLOBAL:LOG_CODES_MAP.Add("PureArrayDashboardIOPSData.ps1", 2402)
$GLOBAL:LOG_CODES_MAP.Add("PureArrayDashboardLatencyData.ps1", 2403)
$GLOBAL:LOG_CODES_MAP.Add("PureArrayDashboardBandwidthData.ps1", 2404)
$GLOBAL:LOG_CODES_MAP.Add("PureHostState.ps1", 2405)
$GLOBAL:LOG_CODES_MAP.Add("PureArrayDashboardSpaceMetricsData.ps1", 2406)
}

function GetLogCode ($filename) {
$code = $GLOBAL:LOG_CODES_MAP[$filename]
if ($code -eq $null) {
$code = $DEFAULT_LOG_CODE
}
return $code
}

function GetSCOMLogLevel ($level) {
switch ($level){
$GLOBAL:ERROR_LEVEL {1}
$GLOBAL:WARNING_LEVEL {2}
$GLOBAL:INFO_LEVEL {0}
$GLOBAL:VERBOSE_LEVEL {0}
}
}

function Log ($script_name, $level, $message) {
if ($level -le $GLOBAL:CURRENT_LOG_LEVEL){
$code = GetLogCode $script_name
$level = GetSCOMLogLevel $level
if($GLOBAL:mpVersion -eq 'unknown') {$GLOBAL:mpVersion = (Get-SCOMManagementPack -name "PureStorageFlashArray").Version.ToString()}
$fullScriptName = "$script_name" + " [$GLOBAL:mpVersion]"
$GLOBAL:MOMAPI.LogScriptEvent($fullScriptName, $code, $level, $message)
}
}

function LoadPowerShellSDK ($script_name){
Log $script_name $GLOBAL:VERBOSE_LEVEL "Importing PowerShell SDK in $script_name"

try {
Log $script_name $GLOBAL:VERBOSE_LEVEL "Loading PowerShell SDK dependencies"
Add-Type -Path $newtonsoft_path, $renci_path, $rest_shared_path, $rest_path

Log $script_name $GLOBAL:VERBOSE_LEVEL "Importing PowerShell SDK"
Import-Module $sdk_path -Verbose
}
catch {
Log $script_name $GLOBAL:ERROR_LEVEL "Failed to import PowerShell SDK"
}
}

function LoadOperationsManagerModule ($script_name)
{
Log $script_name $GLOBAL:INFO_LEVEL "Importing Operations Manager module in $script_name"
try
{
# https://blogs.technet.microsoft.com/cchamp/2015/09/30/loading-the-scom-powershell-module-for-real-this-time/
$OMCmdletsTest = (Get-Module|% {$_.Name}) -Join ' '
If (!$OMCmdletsTest.Contains('OperationsManager')) {
$ModuleFound = $false
$SetupKeys = @('HKLM:\Software\Microsoft\Microsoft Operations Manager\3.0\Setup',
'HKLM:\SOFTWARE\Microsoft\System Center Operations Manager\12\Setup')
foreach($setupKey in $SetupKeys) {
If ((Test-Path $setupKey) -and ($ModuleFound -eq $false)) {
$setupKey = Get-Item -Path $setupKey
$installDirectory = $setupKey.GetValue('InstallDirectory')
$psmPath = $installdirectory + '\Powershell\OperationsManager\OperationsManager.psm1'
If (Test-Path $psmPath) {
$ModuleFound = $true
}
}
}
If ($ModuleFound) {
Import-Module $psmPath
} else {
Import-Module OperationsManager
}
}
else { Log $ScriptName $GLOBAL:INFO_LEVEL "Operations Manager module is already present" }
}
catch
{
Log $script_name $GLOBAL:INFO_LEVEL "Failed to import Operations Manager: $_"
}
}

function Format-Number($value, $digits, $unit)
{
return [string][Math]::Round($value, $digits) + $unit
}

function Format-ByteNumber($value, $suffix)
{
if ($null -eq $value) {
return '-'
} elseif ($value -ge 1PB){
$v = $value/1PB
$unit =' PB'
} elseif ($value -ge 1TB){
$v = $value/1TB
$unit = ' TB'
} elseif ($value -ge 1GB){
$v = $value/1GB
$unit = ' GB'
} elseif ($value -ge 1MB){
$v = $value/1MB
$unit = ' MB'
} elseif ($value -ge 1kB){
$v = $value/1kB
$unit = ' kB'
} else {
$v = $value
$unit =' B'
}

return Format-Number $v 2 ($unit + $suffix)
}

function Get-PerfCounterValue($instance, $counterName, $startTime, $endTime)
{
$criteria = New-Object Microsoft.EnterpriseManagement.Monitoring.MonitoringPerformanceDataCriteria("CounterName='$counterName'")
$performanceDataCollection = $instance.GetMonitoringPerformanceData($criteria)
Log $ScriptName $GLOBAL:VERBOSE_LEVEL $performanceDataCollection
$valueReader = $performanceDataCollection.GetValueReader($startTime, $endTime)
while ($valueReader.Read()) {
Write-Output $valueReader.GetMonitoringPerformanceDataValue().SampleValue
}
}


InitLogging