Fujitsu.Servers.PRIMERGY.PS.CIMClient (DeployableResource)

Element properties:

TypeDeployableResource
File NameCIMClient.psm1
AccessibilityPublic

Source Code:

<DeployableResource ID="Fujitsu.Servers.PRIMERGY.PS.CIMClient" Accessibility="Public" FileName="CIMClient.psm1" HasNullStream="false"/>

File Content: CIMClient.psm1

#-------------------------------------------------------------------

# Fujitsu
# Copyright 2015-2017 FUJITSU LIMITED
#
# CIMClient.psm1
#
# Summary:
# CIM client module for PowerShell
#-------------------------------------------------------------------

$script:FTSNameSpace = "root/svs"
$script:CIMNameSpace = "root/cimv2"

$script:invokerClass = "Fujitsu.Servers.PRIMERGY.AssemblyInvoker.AssemblyInvoker"
$script:cimclientClass = "Fujitsu.Servers.PRIMERGY.CIMClient.CIMClient"

function New-CIMClient {
<#
.SYNOPSIS
Creates new CIMClient object for future requests.
.DESCRIPTION
Function creates new CIMClient object using AssemblyInvoker object.
.PARAMETER InvokerDllPath
Path to AssemblyInvoker dll file.
.PARAMETER ClientDllPath
Path to CIMClient dll file.
.PARAMETER ClientAddress
IPAddress for CIMOM.
.PARAMETER ClientPort
Port for CIMOM.
.PARAMETER ClientUser
Username for CIMOM.
.PARAMETER ClientPass
Password for CIMOM.
.PARAMETER CIMNamespace
Namespace for CIMOM queries.
.PARAMETER Logger
Logger object.
.EXAMPLE
$cim = New-CIMClient -InvokerDllPath "invokerExamplePath.dll" -ClientDllPath "clientExamplePath.dll" -ClientAddress "127.0.0.1" -ClientPort 5989 -ClientUser "test" -ClientPass "test" -CIMNamespace "root/cimv2"
#>
Param (
[string] $InvokerDllPath = $(Throw New-Object System.ArgumentException "Parameter -InvokerDllPath must be set.", "InvokerDllPath"),
[string] $ClientDllPath = $(Throw New-Object System.ArgumentException "Parameter -ClientDllPath must be set.", "ClientDllPath"),
[string] $ClientUser = $(Throw New-Object System.ArgumentException "Parameter -ClientUser must be set.", "ClientUser"),
[string] $ClientPass = $(Throw New-Object System.ArgumentException "Parameter -ClientPass must be set.", "ClientPass"),
[string] $ClientAddress = "127.0.0.1",
[int] $ClientPort = 5989,
[string] $CIMNamespace = $script:FTSNameSpace,
[object] $Logger = $Null
)

$disposeClient = {
if ($this.Client -ne $Null) {
$this.Client.Dispose() | Out-Null
$this.Client = $Null
}
}

$refreshSettings = {
if ($this.Client -eq $Null) { return }
if (($this.Address -ne $Null) -and ($this.Address -ne "")) {
$this.Client.Invoke("SetTarget", @([ipaddress]$this.Address, [int]$this.Port)) | Out-Null
}
$this.Client.Set("CimNamespace", [string]$this.Namespace) | Out-Null
$this.Client.Set("Timeout", $this.Timeout) | Out-Null
$this.Client.Set("CimRequestTries", $this.RequestTries) | Out-Null
$this.Client.Set("CimRetriesInterval", $this.RetriesInterval) | Out-Null
$this.Client.Set("CertificateValidation", $this.CheckCert) | Out-Null
if ($this.Protocol -ieq "http" -or $this.Protocol -ieq "https") {
$this.Client.Set("ProtocolPrefix", "$($this.Protocol)://") | Out-Null
}
}

$invokeQuery = {
<#
.SYNOPSIS
Invokes query to the CIMOM.
.DESCRIPTION
Invokes CIM query to the CIMOM.
.PARAMETER CIMClass
CIM class to query.
.PARAMETER CIMElement
Optional, CIM element name to select from return set.
.PARAMETER Silent
Optional, do not throw any errors if true.
.PARAMETER NamesOnly
Optional, enumerate names only.
.EXAMPLE
$cim.Query("CIMClass")
#>
Param (
[string] $CIMClass = $(Throw New-Object System.ArgumentException "Parameter -CIMClass must be set.", "CIMClass"),
[string] $CIMElement = "",
[bool] $Silent = $False,
[bool] $NamesOnly = $False
)

if ($this.Client -eq $Null) { return }
$this.Refresh()
if ($CIMElement -eq "") {
$queriedClass = "$($CIMClass)"
} else {
$queriedClass = "$($CIMClass):$($CIMElement)"
}

if ($NamesOnly) {
$method = "EnumerateInstanceNames"
} else {
$method = "EnumerateInstances"
}

$this.LastError = ""
try {
$CimData = $this.Client.Invoke($method, $CIMClass)
if (($CimData -ne $Null) -and ($CimData.Error -ne $Null)) {
throw $CimData.Error
}
if ($CIMElement -ne "") {
foreach ($instance in $CimData) {
if ($instance.ElementName -match $CIMElement) {
return $instance
}
}
return $Null
}
return $CimData
} catch [Exception] {
$this.LastError = "CIM $($queriedClass): $($_.Exception.Message)"
if ($this.Logger -ne $Null -and $Silent) {
$this.Logger.Debug($this.LastError)
return $Null
} else {
throw $this.LastError
}
}
}

$invokeMethod = {
<#
.SYNOPSIS
Invokes method from CIM class.
.DESCRIPTION
Invokes method from provided CIM class.
.PARAMETER CIMClass
CIM class to query.
.PARAMETER Method
Method name to invoke from CIMClass.
.PARAMETER Params
Optional, params for method.
.PARAMETER Silent
Optional, do not throw any errors if true.
.EXAMPLE
$cim.Invoke("CIMClass", "MethodFromCIMClass")
#>
Param (
[string] $CIMClass = $(Throw New-Object System.ArgumentException "Parameter -CIMClass must be set.", "CIMClass"),
[string] $Method = $(Throw New-Object System.ArgumentException "Parameter -Method must be set.", "Method"),
[hashtable] $Params = @{},
[bool] $Silent = $False
)

if ($this.Client -eq $Null) { return }
$this.Refresh()
$this.LastError = ""
try {
$CimData = $this.Client.Invoke("CallMethod", @($CIMClass, $Method, $Params))
if (($CimData -ne $Null) -and ($CimData.Error -ne $Null)) {
throw $CimData.Error
}
return $CimData
} catch [Exception] {
$this.LastError = "CIM $($CIMClass): $($_.Exception.Message)"
if ($this.Logger -ne $Null -and $Silent) {
$this.Logger.Debug($this.LastError)
return $Null
} else {
throw $this.LastError
}
}
}

# Include AssemblyInvoker
if (-not ([System.Management.Automation.PSTypeName]'Fujitsu.Servers.PRIMERGY.AssemblyInvoker.AssemblyInvoker').Type) {
Add-Type -Path $InvokerDllPath
}

# To allow change CIMClient dll in future in ESXi management pack, this assembly must be loaded to separate appdomain using AssemblyInvoker.
$CIMClient = New-Object Fujitsu.Servers.PRIMERGY.AssemblyInvoker.AssemblyInvoker($ClientDllPath, $script:cimclientClass, $CIMNamespace)

if (($CIMClient -ne $Null) -and ($CIMClient.GetType().ToString() -eq $script:invokerClass)) {
$CIMClient.Invoke("SetTarget", @([ipaddress]$ClientAddress, [int]$ClientPort)) | Out-Null
$CIMClient.Invoke("SetCredentials", @([string]$ClientUser, [string]$ClientPass)) | Out-Null
} else {
throw "CIM: AssemblyInvoker is not instantiated"
}

$CIMClientObject = New-Object -TypeName PSObject -Property @{
Type = "Fujitsu.Servers.PRIMERGY.PS.CIMClient"
Logger = $Logger
Client = $CIMClient
Port = $ClientPort
Address = $ClientAddress
Namespace = $CimNamespace
Protocol = "https"
CheckCert = $True
Timeout = 25000
LastError = ""
RequestTries = 3
RetriesInterval = 5000
}
$CIMClientObject | Add-Member -MemberType ScriptMethod -Name "Dispose" -Value $disposeClient
$CIMClientObject | Add-Member -MemberType ScriptMethod -Name "Refresh" -Value $refreshSettings
$CIMClientObject | Add-Member -MemberType ScriptMethod -Name "Query" -Value $invokeQuery
$CIMClientObject | Add-Member -MemberType ScriptMethod -Name "Invoke" -Value $invokeMethod

return $CIMClientObject
}

#-------------------------------------------------------------------

Export-ModuleMember -Variable 'FTSNameSpace', 'CIMNamespace'
Export-ModuleMember -Function 'New-CIMClient'