ZabbixPS.psm1
$Script:PSModuleRoot = $PSScriptRoot $Script:ModuleName = "ZabbixPS" $Script:AppDataPath = [Environment]::GetFolderPath('ApplicationData') $Script:ModuleDataRoot = (Join-Path -Path $Script:AppDataPath -ChildPath $Script:ModuleName) $Script:ModuleDataPath = (Join-Path -Path $Script:ModuleDataRoot -ChildPath "ModuleData.json") if (-not (Test-Path $Script:ModuleDataRoot)) {New-Item -ItemType Directory -Path $Script:ModuleDataRoot -Force} # Imported from [D:\a\1\s\ZabbixPS\Public] # Format-ZBXTags.ps1 function Format-ZBXTags { <# .SYNOPSIS Formats an array of tags. .DESCRIPTION Formats an array of tags. .PARAMETER Tags A comma sperated list of tags. 'Key1:Value1', 'Key2:Value2' .INPUTS None, does not support pipeline. .OUTPUTS PSObject, formatted Zabbix tags. .EXAMPLE .LINK https://www.zabbix.com/documentation/4.2/manual/api #> [CmdletBinding(DefaultParameterSetName = 'ByCredential')] Param ( [Parameter(Mandatory)] [string[]] $Tags ) begin { } process { $formattedTags = Foreach ($tag in $Tags) { $split = $tag.split(':') @{ tag = $split[0] operator = 0 value = $split[-1] } } return $formattedTags } end { } } # Get-ZBXAction.ps1 function Get-ZBXAction { <# .SYNOPSIS Returns a Zabbix action. .DESCRIPTION Returns a Zabbix action. .PARAMETER Uri The Zabbix instance uri. .PARAMETER Credential Specifies a user account that has permission to the project. .PARAMETER Proxy Use a proxy server for the request, rather than connecting directly to the Internet resource. Enter the URI of a network proxy server. .PARAMETER ProxyCredential Specifie a user account that has permission to use the proxy server that is specified by the -Proxy parameter. The default is the current user. .PARAMETER Session ZabbixPS session, created by New-ZBXSession. .PARAMETER ActionId Return only actions with the given IDs. .PARAMETER GroupId Return only actions that use the given host groups in action conditions. .PARAMETER HostId Return only actions that use the given hosts in action conditions. .PARAMETER TriggerId Return only actions that use the given triggers in action conditions. .INPUTS None, does not support pipeline. .OUTPUTS PSObject. Zabbix action. .EXAMPLE Returns all Zabbix actions. Get-ZBXAction .EXAMPLE Returns Zabbix Action with the Action name of 'myAction'. Get-ZBXAction -Name 'myAction' .LINK https://www.zabbix.com/documentation/4.2/manual/api/reference/action/get #> [CmdletBinding(DefaultParameterSetName = 'ByCredential')] [Alias("gzac")] param ( [Parameter(Mandatory, ParameterSetName = 'ByCredential')] [uri] $Uri, [Parameter(ParameterSetName = 'ByCredential')] [pscredential] $Credential, [Parameter(ParameterSetName = 'ByCredential')] [string] $Proxy, [Parameter(ParameterSetName = 'ByCredential')] [pscredential] $ProxyCredential, [Parameter(Mandatory, ParameterSetName = 'BySession')] [object] $Session, [Parameter()] [string[]] $ActionId, [Parameter()] [string[]] $GroupId, [Parameter()] [string[]] $HostId, [Parameter()] [string[]] $TriggerId ) begin { if ($PSCmdlet.ParameterSetName -eq 'BySession') { $currentSession = $Session | Get-ZBXSession -ErrorAction 'Stop' | Select-Object -First 1 if ($currentSession) { $Uri = $currentSession.Uri $Credential = $currentSession.Credential $Proxy = $currentSession.Proxy $ProxyCredential = $currentSession.ProxyCredential $ApiVersion = $currentSession.ApiVersion } } } process { $body = @{ method = 'action.get' jsonrpc = $ApiVersion id = 1 params = @{ output = 'extend' selectOperations = 'extend' selectRecoveryOperations = 'extend' selectFilter = 'extend' } } if ($ActionId) { $body.params.actionids = $ActionId } if ($GroupId) { $body.params.groupids = $GroupId } if ($HostId) { $body.params.hostids = $HostId } if ($TriggerId) { $body.params.triggerids = $TriggerId } $invokeZabbixRestMethodSplat = @{ Body = $body Uri = $Uri Credential = $Credential ApiVersion = $ApiVersion ErrorAction = 'Stop' } if ($Proxy) { $invokeZabbixRestMethodSplat.Proxy = $Proxy if ($ProxyCredential) { $invokeZabbixRestMethodSplat.ProxyCredential = $ProxyCredential } else { $invokeZabbixRestMethodSplat.ProxyUseDefaultCredentials = $true } } return Invoke-ZBXRestMethod @invokeZabbixRestMethodSplat } end { } } # Get-ZBXAlert.ps1 function Get-ZBXAlert { <# .SYNOPSIS Returns a Zabbix alert. .DESCRIPTION Returns a Zabbix alert. .PARAMETER Uri The Zabbix instance uri. .PARAMETER Credential Specifies a user account that has permission to the project. .PARAMETER Proxy Use a proxy server for the request, rather than connecting directly to the Internet resource. Enter the URI of a network proxy server. .PARAMETER ProxyCredential Specifie a user account that has permission to use the proxy server that is specified by the -Proxy parameter. The default is the current user. .PARAMETER Session ZabbixPS session, created by New-ZBXSession. .PARAMETER AlertId Return only alerts with the given IDs. .PARAMETER ActionId Return only alerts generated by the given actions. .PARAMETER EventId Return only alerts generated by the given events. .PARAMETER GroupId Return only alerts that use the given host groups in alert conditions. .PARAMETER HostId Return only alerts that use the given hosts in alert conditions. .INPUTS None, does not support pipeline. .OUTPUTS PSObject. Zabbix alert. .EXAMPLE Returns all Zabbix alerts. Get-ZBXAlert .EXAMPLE Returns Zabbix Alert with the Alert name of 'myAlert'. Get-ZBXAlert -Name 'myAlert' .LINK https://www.zabbix.com/documentation/4.2/manual/api/reference/alert/get #> [CmdletBinding(DefaultParameterSetName = 'ByCredential')] [Alias("gzal")] param ( [Parameter(Mandatory, ParameterSetName = 'ByCredential')] [uri] $Uri, [Parameter(ParameterSetName = 'ByCredential')] [pscredential] $Credential, [Parameter(ParameterSetName = 'ByCredential')] [string] $Proxy, [Parameter(ParameterSetName = 'ByCredential')] [pscredential] $ProxyCredential, [Parameter(Mandatory, ParameterSetName = 'BySession')] [object] $Session, [Parameter()] [string[]] $AlertId, [Parameter()] [string[]] $ActionId, [Parameter()] [string[]] $EventId, [Parameter()] [string[]] $GroupId, [Parameter()] [string[]] $HostId ) begin { if ($PSCmdlet.ParameterSetName -eq 'BySession') { $currentSession = $Session | Get-ZBXSession -ErrorAction 'Stop' | Select-Object -First 1 if ($currentSession) { $Uri = $currentSession.Uri $Credential = $currentSession.Credential $Proxy = $currentSession.Proxy $ProxyCredential = $currentSession.ProxyCredential $ApiVersion = $currentSession.ApiVersion } } } process { $body = @{ method = 'alert.get' jsonrpc = $ApiVersion id = 1 params = @{ output = 'extend' } } if ($AlertId) { $body.params.alertids = $AlertId } if ($ActionId) { $body.params.actionids = $ActionId } if ($EventId) { $body.params.eventids = $EventId } if ($GroupId) { $body.params.groupids = $GroupId } if ($HostId) { $body.params.hostids = $HostId } $invokeZabbixRestMethodSplat = @{ Body = $body Uri = $Uri Credential = $Credential ApiVersion = $ApiVersion ErrorAlert = 'Stop' } if ($Proxy) { $invokeZabbixRestMethodSplat.Proxy = $Proxy if ($ProxyCredential) { $invokeZabbixRestMethodSplat.ProxyCredential = $ProxyCredential } else { $invokeZabbixRestMethodSplat.ProxyUseDefaultCredentials = $true } } return Invoke-ZBXRestMethod @invokeZabbixRestMethodSplat } end { } } # Get-ZBXApplication.ps1 function Get-ZBXApplication { <# .SYNOPSIS Returns a Zabbix application. .DESCRIPTION Returns a Zabbix application. .PARAMETER Uri The Zabbix instance uri. .PARAMETER Credential Specifies a user account that has permission to the project. .PARAMETER Proxy Use a proxy server for the request, rather than connecting directly to the Internet resource. Enter the URI of a network proxy server. .PARAMETER ProxyCredential Specifie a user account that has permission to use the proxy server that is specified by the -Proxy parameter. The default is the current user. .PARAMETER Session ZabbixPS session, created by New-ZBXSession. .PARAMETER ApplicationId Return only applications with the given IDs. .PARAMETER GroupId Return only applications that use the given host groups in application conditions. .PARAMETER HostId Return only applications that use the given hosts in application conditions. .INPUTS None, does not support pipeline. .OUTPUTS PSObject. Zabbix application. .EXAMPLE Returns all Zabbix applications. Get-ZBXApplication .EXAMPLE Returns Zabbix Application with the Application name of 'myApplication'. Get-ZBXApplication -Name 'myApplication' .LINK https://www.zabbix.com/documentation/4.2/manual/api/reference/application/get #> [CmdletBinding(DefaultParameterSetName = 'ByCredential')] [Alias("gzapp")] param ( [Parameter(Mandatory, ParameterSetName = 'ByCredential')] [uri] $Uri, [Parameter(ParameterSetName = 'ByCredential')] [pscredential] $Credential, [Parameter(ParameterSetName = 'ByCredential')] [string] $Proxy, [Parameter(ParameterSetName = 'ByCredential')] [pscredential] $ProxyCredential, [Parameter(Mandatory, ParameterSetName = 'BySession')] [object] $Session, [Parameter()] [string[]] $ApplicationId, [Parameter()] [string[]] $GroupId, [Parameter()] [string[]] $HostId ) begin { if ($PSCmdlet.ParameterSetName -eq 'BySession') { $currentSession = $Session | Get-ZBXSession -ErrorAction 'Stop' | Select-Object -First 1 if ($currentSession) { $Uri = $currentSession.Uri $Credential = $currentSession.Credential $Proxy = $currentSession.Proxy $ProxyCredential = $currentSession.ProxyCredential $ApiVersion = $currentSession.ApiVersion } } } process { $body = @{ method = 'application.get' jsonrpc = $ApiVersion id = 1 params = @{ output = 'extend' } } if ($ApplicationId) { $body.params.applicationids = $ApplicationId } if ($GroupId) { $body.params.groupids = $GroupId } if ($HostId) { $body.params.hostids = $HostId } $invokeZabbixRestMethodSplat = @{ Body = $body Uri = $Uri Credential = $Credential ApiVersion = $ApiVersion ErrorApplication = 'Stop' } if ($Proxy) { $invokeZabbixRestMethodSplat.Proxy = $Proxy if ($ProxyCredential) { $invokeZabbixRestMethodSplat.ProxyCredential = $ProxyCredential } else { $invokeZabbixRestMethodSplat.ProxyUseDefaultCredentials = $true } } return Invoke-ZBXRestMethod @invokeZabbixRestMethodSplat } end { } } # Get-ZBXEvent.ps1 function Get-ZBXEvent { <# .SYNOPSIS Returns a Zabbix event. .DESCRIPTION Returns a Zabbix event. .PARAMETER Uri The Zabbix instance uri. .PARAMETER Credential Specifies a user account that has permission to the project. .PARAMETER Proxy Use a proxy server for the request, rather than connecting directly to the Internet resource. Enter the URI of a network proxy server. .PARAMETER ProxyCredential Specifie a user account that has permission to use the proxy server that is specified by the -Proxy parameter. The default is the current user. .PARAMETER Session ZabbixPS session, created by New-ZBXSession. .PARAMETER EventId Return only events with the given IDs. .PARAMETER GroupId Return only events that use the given host groups in event conditions. .PARAMETER HostId Return only events that use the given hosts in event conditions. .INPUTS None, does not support pipeline. .OUTPUTS PSObject. Zabbix event. .EXAMPLE Returns all Zabbix events. Get-ZBXEvent .EXAMPLE Returns Zabbix Event with the Event name of 'myEvent'. Get-ZBXEvent -Name 'myEvent' .LINK https://www.zabbix.com/documentation/4.2/manual/api/reference/event/get #> [CmdletBinding(DefaultParameterSetName = 'ByCredential')] [Alias("gze")] param ( [Parameter(Mandatory, ParameterSetName = 'ByCredential')] [uri] $Uri, [Parameter(ParameterSetName = 'ByCredential')] [pscredential] $Credential, [Parameter(ParameterSetName = 'ByCredential')] [string] $Proxy, [Parameter(ParameterSetName = 'ByCredential')] [pscredential] $ProxyCredential, [Parameter(Mandatory, ParameterSetName = 'BySession')] [object] $Session, [Parameter()] [string[]] $EventId, [Parameter()] [string[]] $GroupId, [Parameter()] [string[]] $HostId ) begin { if ($PSCmdlet.ParameterSetName -eq 'BySession') { $currentSession = $Session | Get-ZBXSession -ErrorAction 'Stop' | Select-Object -First 1 if ($currentSession) { $Uri = $currentSession.Uri $Credential = $currentSession.Credential $Proxy = $currentSession.Proxy $ProxyCredential = $currentSession.ProxyCredential $ApiVersion = $currentSession.ApiVersion } } } process { $body = @{ method = 'event.get' jsonrpc = $ApiVersion id = 1 params = @{ output = 'extend' select_acknowledges = 'extend' selectTags = 'extend' selectSuppressionData = 'extend' } } if ($EventId) { $body.params.eventids = $EventId } if ($GroupId) { $body.params.groupids = $GroupId } if ($HostId) { $body.params.hostids = $HostId } $invokeZabbixRestMethodSplat = @{ Body = $body Uri = $Uri Credential = $Credential ApiVersion = $ApiVersion ErrorEvent = 'Stop' } if ($Proxy) { $invokeZabbixRestMethodSplat.Proxy = $Proxy if ($ProxyCredential) { $invokeZabbixRestMethodSplat.ProxyCredential = $ProxyCredential } else { $invokeZabbixRestMethodSplat.ProxyUseDefaultCredentials = $true } } return Invoke-ZBXRestMethod @invokeZabbixRestMethodSplat } end { } } # Get-ZBXGraph.ps1 function Get-ZBXGraph { <# .SYNOPSIS Returns a Zabbix graph. .DESCRIPTION Returns a Zabbix graph. .PARAMETER Uri The Zabbix instance uri. .PARAMETER Credential Specifies a user account that has permission to the project. .PARAMETER Proxy Use a proxy server for the request, rather than connecting directly to the Internet resource. Enter the URI of a network proxy server. .PARAMETER ProxyCredential Specifie a user account that has permission to use the proxy server that is specified by the -Proxy parameter. The default is the current user. .PARAMETER Session ZabbixPS session, created by New-ZBXSession. .PARAMETER GraphId Return only graphs with the given IDs. .PARAMETER GroupId Return only graphs that use the given host groups in graph conditions. .PARAMETER HostId Return only graphs that use the given hosts in graph conditions. .INPUTS None, does not support pipeline. .OUTPUTS PSObject. Zabbix graph. .EXAMPLE Returns all Zabbix graphs. Get-ZBXGraph .EXAMPLE Returns Zabbix Graph with the Graph name of 'myGraph'. Get-ZBXGraph -Name 'myGraph' .LINK https://www.zabbix.com/documentation/4.2/manual/api/reference/graph/get #> [CmdletBinding(DefaultParameterSetName = 'ByCredential')] [Alias("gzgph")] param ( [Parameter(Mandatory, ParameterSetName = 'ByCredential')] [uri] $Uri, [Parameter(ParameterSetName = 'ByCredential')] [pscredential] $Credential, [Parameter(ParameterSetName = 'ByCredential')] [string] $Proxy, [Parameter(ParameterSetName = 'ByCredential')] [pscredential] $ProxyCredential, [Parameter(Mandatory, ParameterSetName = 'BySession')] [object] $Session, [Parameter()] [string[]] $GraphId, [Parameter()] [string[]] $GroupId, [Parameter()] [string[]] $HostId ) begin { if ($PSCmdlet.ParameterSetName -eq 'BySession') { $currentSession = $Session | Get-ZBXSession -ErrorAction 'Stop' | Select-Object -First 1 if ($currentSession) { $Uri = $currentSession.Uri $Credential = $currentSession.Credential $Proxy = $currentSession.Proxy $ProxyCredential = $currentSession.ProxyCredential $ApiVersion = $currentSession.ApiVersion } } } process { $body = @{ method = 'graph.get' jsonrpc = $ApiVersion id = 1 params = @{ output = 'extend' } } if ($GraphId) { $body.params.graphids = $GraphId } if ($GroupId) { $body.params.groupids = $GroupId } if ($HostId) { $body.params.hostids = $HostId } $invokeZabbixRestMethodSplat = @{ Body = $body Uri = $Uri Credential = $Credential ApiVersion = $ApiVersion ErrorGraph = 'Stop' } if ($Proxy) { $invokeZabbixRestMethodSplat.Proxy = $Proxy if ($ProxyCredential) { $invokeZabbixRestMethodSplat.ProxyCredential = $ProxyCredential } else { $invokeZabbixRestMethodSplat.ProxyUseDefaultCredentials = $true } } return Invoke-ZBXRestMethod @invokeZabbixRestMethodSplat } end { } } # Get-ZBXHistory.ps1 function Get-ZBXHistory { <# .SYNOPSIS Returns a Zabbix history. .DESCRIPTION Returns a Zabbix history. .PARAMETER Uri The Zabbix instance uri. .PARAMETER Credential Specifies a user account that has permission to the project. .PARAMETER Proxy Use a proxy server for the request, rather than connecting directly to the Internet resource. Enter the URI of a network proxy server. .PARAMETER ProxyCredential Specifie a user account that has permission to use the proxy server that is specified by the -Proxy parameter. The default is the current user. .PARAMETER Session ZabbixPS session, created by New-ZBXSession. .PARAMETER History History object types to return. 0 - numeric float; 1 - character; 2 - log; 3 - numeric unsigned; 4 - text. Default: 3. .PARAMETER GroupId Return only historys that use the given host groups in history conditions. .PARAMETER ItemId Return only history from the given items. .INPUTS None, does not support pipeline. .OUTPUTS PSObject. Zabbix history. .EXAMPLE Returns all Zabbix historys. Get-ZBXHistory .EXAMPLE Returns Zabbix History with the History name of 'myHistory'. Get-ZBXHistory -Name 'myHistory' .LINK https://www.zabbix.com/documentation/4.2/manual/api/reference/history/get #> [CmdletBinding(DefaultParameterSetName = 'ByCredential')] [Alias("gzhist")] param ( [Parameter(Mandatory, ParameterSetName = 'ByCredential')] [uri] $Uri, [Parameter(ParameterSetName = 'ByCredential')] [pscredential] $Credential, [Parameter(ParameterSetName = 'ByCredential')] [string] $Proxy, [Parameter(ParameterSetName = 'ByCredential')] [pscredential] $ProxyCredential, [Parameter(Mandatory, ParameterSetName = 'BySession')] [object] $Session, [Parameter()] [ValidateSet(0, 1, 2, 3, 4)] [int] $History = 3, [Parameter()] [string[]] $GroupId, [Parameter()] [string[]] $ItemId ) begin { if ($PSCmdlet.ParameterSetName -eq 'BySession') { $currentSession = $Session | Get-ZBXSession -ErrorAction 'Stop' | Select-Object -First 1 if ($currentSession) { $Uri = $currentSession.Uri $Credential = $currentSession.Credential $Proxy = $currentSession.Proxy $ProxyCredential = $currentSession.ProxyCredential $ApiVersion = $currentSession.ApiVersion } } } process { $body = @{ method = 'history.get' jsonrpc = $ApiVersion id = 1 params = @{ output = 'extend' history = $History } } if ($GroupId) { $body.params.groupids = $GroupId } if ($ItemId) { $body.params.itemids = $ItemId } $invokeZabbixRestMethodSplat = @{ Body = $body Uri = $Uri Credential = $Credential ApiVersion = $ApiVersion ErrorHistory = 'Stop' } if ($Proxy) { $invokeZabbixRestMethodSplat.Proxy = $Proxy if ($ProxyCredential) { $invokeZabbixRestMethodSplat.ProxyCredential = $ProxyCredential } else { $invokeZabbixRestMethodSplat.ProxyUseDefaultCredentials = $true } } return Invoke-ZBXRestMethod @invokeZabbixRestMethodSplat } end { } } # Get-ZBXHost.ps1 function Get-ZBXHost { <# .SYNOPSIS Returns a Zabbix host. .DESCRIPTION Returns a Zabbix host. .PARAMETER Uri The Zabbix instance uri. .PARAMETER Credential Specifies a user account that has permission to the project. .PARAMETER Proxy Use a proxy server for the request, rather than connecting directly to the Internet resource. Enter the URI of a network proxy server. .PARAMETER ProxyCredential Specifie a user account that has permission to use the proxy server that is specified by the -Proxy parameter. The default is the current user. .PARAMETER Session ZabbixPS session, created by New-ZBXSession. .PARAMETER Name The name of the host. .PARAMETER HostId Return only hosts with the given IDs. .PARAMETER GroupId Return only hosts that use the given host groups in host conditions. .PARAMETER ApplicationId Return only hosts that have the given applications. .PARAMETER DserviceId Return only hosts that are related to the given discovered services. .PARAMETER GraphId Return only hosts that have the given graphs. .PARAMETER InterfaceId Return only hosts that use the given interfaces. .PARAMETER ItemId Return only hosts that have the given items. .PARAMETER MaintenanceId Return only hosts that are affected by the given maintenances. .PARAMETER TemplateId Return only hosts that are linked to the given templates. .INPUTS None, does not support pipeline. .OUTPUTS PSObject. Zabbix host. .EXAMPLE Returns all Zabbix hosts. Get-ZBXHost .EXAMPLE Returns Zabbix Host with the Host name of 'myHost'. Get-ZBXHost -Name 'myHost' .LINK https://www.zabbix.com/documentation/4.2/manual/api/reference/host/get #> [CmdletBinding(DefaultParameterSetName = 'ByCredential')] param ( [Parameter(Mandatory, ParameterSetName = 'ByCredential')] [uri] $Uri, [Parameter(ParameterSetName = 'ByCredential')] [pscredential] $Credential, [Parameter(ParameterSetName = 'ByCredential')] [string] $Proxy, [Parameter(ParameterSetName = 'ByCredential')] [pscredential] $ProxyCredential, [Parameter(Mandatory, ParameterSetName = 'BySession')] [object] $Session, [Parameter()] [string[]] $Name, [Parameter()] [string[]] $HostId, [Parameter()] [string[]] $GroupId, [Parameter()] [string[]] $ApplicationId, [Parameter()] [string[]] $DserviceId, [Parameter()] [string[]] $GraphId, [Parameter()] [string[]] $InterfaceId, [Parameter()] [string[]] $ItemId, [Parameter()] [string[]] $MaintenanceId, [Parameter()] [string[]] $TemplateId ) begin { if ($PSCmdlet.ParameterSetName -eq 'BySession') { $currentSession = $Session | Get-ZBXSession -ErrorAction 'Stop' | Select-Object -First 1 if ($currentSession) { $Uri = $currentSession.Uri $Credential = $currentSession.Credential $Proxy = $currentSession.Proxy $ProxyCredential = $currentSession.ProxyCredential $ApiVersion = $currentSession.ApiVersion } } } process { $body = @{ method = 'host.get' jsonrpc = $ApiVersion id = 1 sortfield = 'name' params = @{ output = 'extend' selectParentTemplates = @( "templateid", "name" ) selectInterfaces = @( "interfaceid", "ip", "port" ) selectHttpTests = @( "httptestid", "name", "steps" ) selectTriggers = @( "triggerid", "description" ) selectApplications = @( "applicationid" "name" ) selectGraphs = @( "graphid" "name" ) selectMacros = @( "hostmacroid" "macro" "value" ) selectScreens = @( "screenid" "name" ) selectInventory = @( "name" "type" "os" ) } } if ($Name) { $body.params.filter = @{ host = $Name } } if ($HostId) { $body.params.hostids = $HostId } if ($GroupId) { $body.params.groupids = $GroupId } if ($ApplicationId) { $body.params.applicationids = $ApplicationId } if ($DserviceId) { $body.params.dserviceids = $DserviceId } if ($GraphId) { $body.params.graphids = $GraphId } if ($InterfaceId) { $body.params.interfaceids = $InterfaceId } if ($ItemId) { $body.params.itemids = $ItemId } if ($MaintenanceId) { $body.params.maintenanceids = $MaintenanceId } if ($TemplateId) { $body.params.templateids = $TemplateId } $invokeZabbixRestMethodSplat = @{ Body = $body Uri = $Uri Credential = $Credential ApiVersion = $ApiVersion ErrorAction = 'Stop' } if ($Proxy) { $invokeZabbixRestMethodSplat.Proxy = $Proxy if ($ProxyCredential) { $invokeZabbixRestMethodSplat.ProxyCredential = $ProxyCredential } else { $invokeZabbixRestMethodSplat.ProxyUseDefaultCredentials = $true } } return Invoke-ZBXRestMethod @invokeZabbixRestMethodSplat } end { } } # Get-ZBXHostGroup.ps1 function Get-ZBXHostGroup { <# .SYNOPSIS Returns a Zabbix host group. .DESCRIPTION Returns a Zabbix host group. .PARAMETER Uri The Zabbix instance uri. .PARAMETER Credential Specifies a user account that has permission to the project. .PARAMETER Proxy Use a proxy server for the request, rather than connecting directly to the Internet resource. Enter the URI of a network proxy server. .PARAMETER ProxyCredential Specifie a user account that has permission to use the proxy server that is specified by the -Proxy parameter. The default is the current user. .PARAMETER Session ZabbixPS session, created by New-ZBXSession. .PARAMETER Name The name of the group. .PARAMETER GroupId Return only host group that use the given host groups in hostgroup conditions. .PARAMETER MaintenanceId Return only host groups that are affected by the given maintenances. .PARAMETER TemplateId Return only host groups that contain the given templates. .INPUTS None, does not support pipeline. .OUTPUTS PSObject. Zabbix hostgroup. .EXAMPLE Returns all Zabbix host group. Get-ZBXHostGroup .EXAMPLE Returns Zabbix HostGroup with the HostGroup name of 'myHostGroup'. Get-ZBXHostGroup -Name 'myHostGroup' .LINK https://www.zabbix.com/documentation/4.2/manual/api/reference/hostgroup/get #> [CmdletBinding(DefaultParameterSetName = 'ByCredential')] [Alias("gzhg", "Get-ZBXGroup")] param ( [Parameter(Mandatory, ParameterSetName = 'ByCredential')] [uri] $Uri, [Parameter(ParameterSetName = 'ByCredential')] [pscredential] $Credential, [Parameter(ParameterSetName = 'ByCredential')] [string] $Proxy, [Parameter(ParameterSetName = 'ByCredential')] [pscredential] $ProxyCredential, [Parameter(Mandatory, ParameterSetName = 'BySession')] [object] $Session, [Parameter()] [string[]] $Name, [Parameter()] [string[]] $GroupId, [Parameter()] [string[]] $HostId, [Parameter()] [string[]] $MaintenanceId, [Parameter()] [string[]] $TemplateId ) begin { if ($PSCmdlet.ParameterSetName -eq 'BySession') { $currentSession = $Session | Get-ZBXSession -ErrorAction 'Stop' | Select-Object -First 1 if ($currentSession) { $Uri = $currentSession.Uri $Credential = $currentSession.Credential $Proxy = $currentSession.Proxy $ProxyCredential = $currentSession.ProxyCredential $ApiVersion = $currentSession.ApiVersion } } } process { $body = @{ method = 'hostgroup.get' jsonrpc = $ApiVersion id = 1 params = @{ output = 'extend' selectHosts = @( "hostid", "host" ) selectTemplates = @( "templateid", "name" ) } } if ($Name) { $body.params.filter = @{ name = $Name } } if ($GroupId) { $body.params.groupids = $GroupId } if ($HostId) { $body.params.hostids = $HostId } if ($MaintenanceId) { $body.params.maintenanceids = $MaintenanceId } if ($TemplateId) { $body.params.templateids = $TemplateId } $invokeZabbixRestMethodSplat = @{ Body = $body Uri = $Uri Credential = $Credential ApiVersion = $ApiVersion ErrorAction = 'Stop' } if ($Proxy) { $invokeZabbixRestMethodSplat.Proxy = $Proxy if ($ProxyCredential) { $invokeZabbixRestMethodSplat.ProxyCredential = $ProxyCredential } else { $invokeZabbixRestMethodSplat.ProxyUseDefaultCredentials = $true } } return Invoke-ZBXRestMethod @invokeZabbixRestMethodSplat } end { } } # Get-ZBXMaintenance.ps1 function Get-ZBXMaintenance { <# .SYNOPSIS Gets a Zabbix maintenance period. .DESCRIPTION Gets a Zabbix maintenance period. .PARAMETER Uri The Zabbix instance uri. .PARAMETER Credential Specifies a user account that has permission to the project. .PARAMETER Proxy Use a proxy server for the request, rather than connecting directly to the Internet resource. Enter the URI of a network proxy server. .PARAMETER ProxyCredential Specifie a user account that has permission to use the proxy server that is specified by the -Proxy parameter. The default is the current user. .PARAMETER Session ZabbixPS session, created by New-ZBXSession. .PARAMETER Name The maintenance name. .PARAMETER Id The maintenance id. .INPUTS None, does not support pipeline. .OUTPUTS None, does not support output. .EXAMPLE .LINK https://www.zabbix.com/documentation/4.2/manual/api/reference/maintenance/delete #> [CmdletBinding(DefaultParameterSetName = 'ByCredential')] [Alias("gzm")] param ( [Parameter(Mandatory, ParameterSetName = 'ByCredential')] [uri] $Uri, [Parameter(ParameterSetName = 'ByCredential')] [pscredential] $Credential, [Parameter(ParameterSetName = 'ByCredential')] [string] $Proxy, [Parameter(ParameterSetName = 'ByCredential')] [pscredential] $ProxyCredential, [Parameter(Mandatory, ParameterSetName = 'BySession')] [object] $Session, [Parameter()] [string] $Name, [Parameter()] [string] $Id ) begin { if ($PSCmdlet.ParameterSetName -eq 'BySession') { $currentSession = $Session | Get-ZBXSession -ErrorAction 'Stop' | Select-Object -First 1 if ($currentSession) { $Uri = $currentSession.Uri $Credential = $currentSession.Credential $Proxy = $currentSession.Proxy $ProxyCredential = $currentSession.ProxyCredential $ApiVersion = $currentSession.ApiVersion } } } process { $body = @{ method = 'maintenance.get' jsonrpc = $ApiVersion id = 1 params = @{ output = "extend" selectGroups = "extend" selectHosts = "extend" selectTimeperiods = "extend" } } if ($Name) { $body.params.filter = @{ name = $Name } } if ($Id) { $body.params.maintenanceids = $Id } $invokeZabbixRestMethodSplat = @{ Body = $body Uri = $Uri Credential = $Credential ApiVersion = $ApiVersion ErrorAction = 'Stop' } if ($Proxy) { $invokeZabbixRestMethodSplat.Proxy = $Proxy if ($ProxyCredential) { $invokeZabbixRestMethodSplat.ProxyCredential = $ProxyCredential } else { $invokeZabbixRestMethodSplat.ProxyUseDefaultCredentials = $true } } return Invoke-ZBXRestMethod @invokeZabbixRestMethodSplat } end { } } # Get-ZBXSession.ps1 function Get-ZBXSession { <# .SYNOPSIS Returns Zabbix session data. .DESCRIPTION Returns Zabbix session data that has been stored in the users local application data. Use Save-ZBXSession to persist the session data to disk. The sensetive data is returned encrypted. .PARAMETER Id Session id. .PARAMETER Name The friendly name of the session. .PARAMETER Path The path where session data will be stored, defaults to $Script:ZabbixModuleDataPath. .INPUTS None, does not support pipeline. .OUTPUTS PSObject. Zabbix session. .EXAMPLE Returns all Zabbix sessions from disk and memory. Get-ZBXSession .EXAMPLE Returns Zabbix session with the session name of 'myFirstSession'. Get-ZBXSession -Name 'myFirstSession' .LINK Zabbix documentation: https://www.zabbix.com/documentation/4.2/manual/api New-ZBXSession Save-ZBXSession Remove-ZBXSession Initialize-ZBXSession #> [CmdletBinding()] param ( [Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)] [string] $Name, [Parameter(ParameterSetName = 'ById', ValueFromPipeline, ValueFromPipelineByPropertyName)] [int] $Id, [Parameter()] [string] $Path = $Script:ZabbixModuleDataPath ) begin { } process { # Process memory sessions $_sessions = @() if ($null -ne $Global:_ZabbixSessions) { foreach ($_memSession in $Global:_ZabbixSessions) { $_sessions += $_memSession } } # Process saved sessions if (Test-Path $Path) { $data = Get-Content -Path $Path -Raw | ConvertFrom-Json foreach ($_data in $data.SessionData) { $_object = New-Object -TypeName PSCustomObject -Property @{ Id = $_data.Id Uri = $_data.Uri Name = $_data.Name ApiVersion = $_data.ApiVersion Saved = $_data.Saved } if ($_data.Credential) { $_psCredentialObject = [pscredential]::new($_data.Credential.Username, ($_data.Credential.Password | ConvertTo-SecureString)) $_object | Add-Member -NotePropertyName 'Credential' -NotePropertyValue $_psCredentialObject } if ($_data.Proxy) { $_object | Add-Member -NotePropertyName 'Proxy' -NotePropertyValue $_data.Proxy } if ($_data.ProxyCredential) { $_psProxyCredentialObject = [pscredential]::new($_data.ProxyCredential.Username, ($_data.ProxyCredential.Password | ConvertTo-SecureString)) $_object | Add-Member -NotePropertyName 'ProxyCredential' -NotePropertyValue $_psProxyCredentialObject } $_sessions += $_object } } if ($PSCmdlet.ParameterSetName -eq 'ById') { $_sessions = $_sessions | Where-Object { $PSItem.Id -eq $Id } } if ($Name) { $_sessions = $_sessions | Where-Object { $PSItem.Name -eq $Name } } if ($_sessions) { return $_sessions } else { Write-Error "[$($MyInvocation.MyCommand.Name)]: Unable to locate a ZabbixPS session." } } end { } } # Initialize-ZBXSession.ps1 function Initialize-ZBXSession { <# .SYNOPSIS Logs into the Zabbix instance and returns an authentication token. .DESCRIPTION Logs into the Zabbix instance and returns an authentication token. .PARAMETER Uri The zabbix instance uri. .PARAMETER Credential Specifies a user account that has permission to the project. .PARAMETER Proxy Use a proxy server for the request, rather than connecting directly to the Internet resource. Enter the URI of a network proxy server. .PARAMETER ProxyCredential Specifie a user account that has permission to use the proxy server that is specified by the -Proxy parameter. The default is the current user. .PARAMETER ApiVersion Version of the api to use, defaults to 2.0. .PARAMETER Path The path where module data will be stored, defaults to $Script:ZabbixModuleDataPath. .PARAMETER Session The name of the Zabbix session. .PARAMETER Force Forces an update to the authorization token. .INPUTS None. Does not support pipeline. .OUTPUTS None. Does not support output. .EXAMPLE .LINK Zabbix documentation: https://www.zabbix.com/documentation/4.2/manual/api New-ZBXSession Get-ZBXSession Save-ZBXSession Remove-ZBXSession #> [CmdletBinding(DefaultParameterSetName = 'ByName')] param ( [Parameter(Mandatory, ParameterSetName = 'ByName')] [uri] $Uri, [Parameter(Mandatory, ParameterSetName = 'ByName')] [pscredential] $Credential, [Parameter(ParameterSetName = 'ByName')] [string] $Proxy, [Parameter(ParameterSetName = 'ByName')] [pscredential] $ProxyCredential, [Parameter(ParameterSetName = 'ByName')] [string] $ApiVersion = '2.0', [Parameter(Mandatory, ParameterSetName = 'BySession')] [object] $Session, [Parameter()] [string] $Path = $Script:ZabbixModuleDataPath, [Parameter()] [switch] $Force ) begin { if ($PSCmdlet.ParameterSetName -eq 'BySession') { $currentSession = $Session | Get-ZBXSession -ErrorAction 'Stop' | Select-Object -First 1 if ($currentSession) { $Uri = $currentSession.Uri $Credential = $currentSession.Credential $Proxy = $currentSession.Proxy $ProxyCredential = $currentSession.ProxyCredential $ApiVersion = $currentSession.ApiVersion } } } process { if ($Global:_ZabbixAuthenticationToken -and (-not($Force.IsPresent))) { return $Global:_ZabbixAuthenticationToken } else { $body = @{ jsonrpc = $ApiVersion method = 'user.login' params = @{ user = $Credential.Username password = $Credential.GetNetworkCredential().password } id = 1 auth = $null } $invokeRestMethodSplat = @{ ContentType = 'application/json' Method = 'POST' UseBasicParsing = $true Uri = $Uri.AbsoluteUri Body = $body | ConvertTo-Json -Depth 20 } if ($Proxy) { $invokeRestMethodSplat.Proxy = $Proxy if ($ProxyCredential) { $invokeRestMethodSplat.ProxyCredential = $ProxyCredential } else { $invokeRestMethodSplat.ProxyUseDefaultCredentials = $true } } Write-Verbose "[$($MyInvocation.MyCommand.Name)]: Requesting an authentication token" try { $results = Invoke-RestMethod @invokeRestMethodSplat } catch { If ($PSitem -match 'File not found') { Write-Error "[$($MyInvocation.MyCommand.Name)]: Unable to locate the Zabbix api at: [$Uri], is this the correct uri? See: https://www.zabbix.com/documentation/4.2/manual/api for more information." -ErrorAction 'Stop' } } If ($results.result) { $Global:_ZabbixAuthenticationToken = $results.result return $Global:_ZabbixAuthenticationToken } } } end { } } # Invoke-ZBXRestMethod.ps1 function Invoke-ZBXRestMethod { <# .SYNOPSIS Invokes a Zabbix rest method. .DESCRIPTION Invokes Zabbix rest method. .PARAMETER Body Specifies the body of the request. The body is the content of the request that follows the headers. .PARAMETER Uri Specifies the Uniform Resource Identifier (URI) of the Internet resource to which the web request is sent. This parameter supports HTTP, HTTPS, FTP, and FILE values. .PARAMETER Credential Specifies a user account that has permission to send the request. The default is the Personal Access Token if it is defined, otherwise it is the current user. .PARAMETER Proxy Use a proxy server for the request, rather than connecting directly to the Internet resource. Enter the URI of a network proxy server. .PARAMETER ProxyCredential Specifie a user account that has permission to use the proxy server that is specified by the -Proxy parameter. The default is the current user. .PARAMETER Path The directory to output files to. .OUTPUTS System.Int64, System.String, System.Xml.XmlDocument, The output of the cmdlet depends upon the format of the content that is retrieved. .OUTPUTS PSObject, If the request returns JSON strings, Invoke-RestMethod returns a PSObject that represents the strings. .EXAMPLE .LINK Zabbix documentation: https://www.zabbix.com/documentation/4.2/manual/api #> [CmdletBinding()] Param ( [Parameter(Mandatory)] [psobject] $Body, [Parameter(Mandatory)] [uri] $Uri, [Parameter(Mandatory)] [pscredential] $Credential, [Parameter()] [string] $Proxy, [Parameter()] [pscredential] $ProxyCredential, [Parameter(Mandatory)] [string] $ApiVersion, [Parameter()] [string] $Path ) begin { } process { $initializeZabbixSessionSplat = @{ Uri = $Uri Credential = $Credential ApiVersion = $ApiVersion } if ($Proxy) { $initializeZabbixSessionSplat.Proxy = $Proxy if ($ProxyCredential) { $initializeZabbixSessionSplat.ProxyCredential = $ProxyCredential } else { $initializeZabbixSessionSplat.ProxyUseDefaultCredentials = $true } } $Body.auth = Initialize-ZBXSession @initializeZabbixSessionSplat $bodyJson = $Body | ConvertTo-Json -Depth 20 $invokeRestMethodSplat = @{ ContentType = 'application/json' Method = 'POST' UseBasicParsing = $true Uri = $Uri.AbsoluteUri Body = $bodyJson } if ($Proxy) { $invokeRestMethodSplat.Proxy = $Proxy if ($ProxyCredential) { $invokeRestMethodSplat.ProxyCredential = $ProxyCredential } else { $invokeRestMethodSplat.ProxyUseDefaultCredentials = $true } } if ($Path) { $invokeRestMethodSplat.OutFile = $Path } Write-Verbose "[$($MyInvocation.MyCommand.Name)]: Invoking Zabbix rest method: [$bodyJson]" try { $results = Invoke-RestMethod @invokeRestMethodSplat } catch { If ($PSitem -match 'File not found') { Write-Error "[$($MyInvocation.MyCommand.Name)]: Unable to locate the Zabbix api at: [$Uri], is this the correct uri? See: https://www.zabbix.com/documentation/4.2/manual/api for more information." -ErrorAction 'Stop' } } if ($results.result) { return $results.result } elseif ($results.error) { Write-Error -Exception $results.error.message -Message $results.error.data -ErrorId $results.error.code } } end { } } # New-ZBXMaintenance.ps1 function New-ZBXMaintenance { <# .SYNOPSIS Creates a new Zabbix maintenance period. .DESCRIPTION Creates a new Zabbix maintenance period. .PARAMETER Uri The Zabbix instance uri. .PARAMETER Credential Specifies a user account that has permission to the project. .PARAMETER Proxy Use a proxy server for the request, rather than connecting directly to the Internet resource. Enter the URI of a network proxy server. .PARAMETER ProxyCredential Specifie a user account that has permission to use the proxy server that is specified by the -Proxy parameter. The default is the current user. .PARAMETER Session ZabbixPS session, created by New-ZBXSession. .PARAMETER Name The case sensitive maintenance name. .PARAMETER Description The maintenance description. .PARAMETER GroupId The id of the group. Can be retrieved with Get-ZBXGroup. .PARAMETER HostId The id of the host. Can be retrieved with Get-ZBXHost. .PARAMETER Type The maintenance type, defaults to 0. 0 - data collection 1 - without data collection .PARAMETER ActiveSince The maintenance start time. .PARAMETER ActiveTill The maintenance end time. .PARAMETER Tags A comma sperated list of tags. 'Key1:Value1', 'Key2:Value2' .PARAMETER TpType The maintenance time period's type, defaults to 0. 0 - one time only 2 - daily 3 - weekly 4 - monthly .PARAMETER TpStartTime The maintenance time period's start time in seconds. Required for daily, weekly and monthly periods. .PARAMETER TpStartDate The maintenance time period's start date. Required only for one time period, defaults to current date. .PARAMETER TpDay Day of the month when the maintenance must come into effect Required only for monthly time periods .PARAMETER TpDayOfWeek Days of the week when the maintenance must come into effect. Used for weekly and monthly time periods. Required only for weekly time periods Days are stored in binary form with each bit representing the corresponding day. 4 - equals 100 in binary and means, that maintenance will be enabled on Wednesday .PARAMETER TpMonth Months when the maintenance must come into effect. Required only for monthly time periods. Months are stored in binary form with each bit representing the corresponding month. 5 - equals 101 in binary and means, that maintenance will be enabled in January and March .PARAMETER TpPeriod The maintenance time period's period/duration in seconds. .INPUTS None, does not support pipeline. .OUTPUTS PSObject, Zabbix maintenance period. .EXAMPLE .LINK https://www.zabbix.com/documentation/4.2/manual/api/reference/maintenance/create #> [CmdletBinding(DefaultParameterSetName = 'ByCredential')] [Alias("nzm")] param ( [Parameter(Mandatory, ParameterSetName = 'ByCredential')] [uri] $Uri, [Parameter(ParameterSetName = 'ByCredential')] [pscredential] $Credential, [Parameter(ParameterSetName = 'ByCredential')] [string] $Proxy, [Parameter(ParameterSetName = 'ByCredential')] [pscredential] $ProxyCredential, [Parameter(Mandatory, ParameterSetName = 'BySession')] [object] $Session, [Parameter(Mandatory)] [string] $Name, [Parameter()] [string] $Description, [Parameter()] [string[]] $GroupId, [Parameter()] [string[]] $HostId, [Parameter()] [int] $Type = 0, [Parameter()] [datetime] $ActiveSince, [Parameter()] [datetime] $ActiveTill, [Parameter()] [string[]] $Tags, [Parameter()] [int] $TpType = 0, [Parameter()] [int] $TpStartTime, [Parameter()] [datetime] $TpStartDate, [Parameter()] [int] $TpDay, [Parameter()] [int] $TpDayOfWeek, [Parameter()] [int] $TpMonth, [Parameter()] [int] $TpPeriod ) begin { if ($PSCmdlet.ParameterSetName -eq 'BySession') { $currentSession = $Session | Get-ZBXSession -ErrorAction 'Stop' | Select-Object -First 1 if ($currentSession) { $Uri = $currentSession.Uri $Credential = $currentSession.Credential $Proxy = $currentSession.Proxy $ProxyCredential = $currentSession.ProxyCredential $ApiVersion = $currentSession.ApiVersion } } } process { $body = @{ method = 'maintenance.create' jsonrpc = $ApiVersion id = 1 params = @{ name = $Name description = $Description active_since = [Math]::Floor([decimal](Get-Date($ActiveSince).ToUniversalTime()-uformat "%s")) active_till = [Math]::Floor([decimal](Get-Date($ActiveTill).ToUniversalTime()-uformat "%s")) maintenance_type = $Type timeperiods = @( @{ timeperiod_type = $TpType start_date = [Math]::Floor([decimal](Get-Date($TpStartDate).ToUniversalTime()-uformat "%s")) period = $TpPeriod every = $TpEvery start_time = $TpStartTime month = $TpMonth dayofweek = $TpDayOfWeek day = $TpDay } ) } } if ($GroupId) { $body.params.groupids = $GroupId } if ($HostId) { $body.params.hostids = $HostId } if ($Tags) { $body.params.tags = (Format-ZBXTags -Tags $Tags) } $invokeZabbixRestMethodSplat = @{ Body = $body Uri = $Uri Credential = $Credential ApiVersion = $ApiVersion ErrorAction = 'Stop' } if ($Proxy) { $invokeZabbixRestMethodSplat.Proxy = $Proxy if ($ProxyCredential) { $invokeZabbixRestMethodSplat.ProxyCredential = $ProxyCredential } else { $invokeZabbixRestMethodSplat.ProxyUseDefaultCredentials = $true } } return Invoke-ZBXRestMethod @invokeZabbixRestMethodSplat } end { } } # New-ZBXSession.ps1 function New-ZBXSession { <# .SYNOPSIS Creates a new Zabbix session. .DESCRIPTION Creates a new Zabbix session. Use Save-ZBXSession to persist the session data to disk. Save the session to a variable to pass the session to other functions. .PARAMETER Name The friendly name of the session. .PARAMETER Uri The zabbix instance uri. .PARAMETER Credential Specifies a user account that has permission to the project. .PARAMETER Proxy Use a proxy server for the request, rather than connecting directly to the Internet resource. Enter the URI of a network proxy server. .PARAMETER ProxyCredential Specifie a user account that has permission to use the proxy server that is specified by the -Proxy parameter. The default is the current user. .PARAMETER ApiVersion Version of the api to use, defaults to 2.0. .PARAMETER Path The path where module data will be stored, defaults to $Script:ZabbixModuleDataPath. .INPUTS None. Does not support pipeline. .OUTPUTS PSObject. Zabbix session .EXAMPLE Creates a session with the name of 'myZabbixInstance' returning it to the $session variable. $session = New-ZBXSession -Uri 'http://myCompany/zabbix/api_jsonrpc.php' -Credential $creds -Name myZabbixInstance .LINK Zabbix documentation: https://www.zabbix.com/documentation/4.2/manual/api Remove-ZBXSession Get-ZBXSession Save-ZBXSession Initialize-ZBXSession #> [CmdletBinding()] [Alias("Connect-ZBX","czab")] param ( [Parameter(Mandatory)] [string] $Name, [Parameter(Mandatory)] [uri] $Uri, [Parameter(Mandatory)] [pscredential] $Credential, [Parameter()] [string] $Proxy, [Parameter()] [pscredential] $ProxyCredential, [Parameter()] [string] $ApiVersion = '2.0', [Parameter()] [string] $Path = $Script:ZabbixModuleDataPath ) begin { } process { [int] $_sessionIdcount = (Get-ZBXSession -ErrorAction 'SilentlyContinue' | Sort-Object -Property 'Id' | Select-Object -Last 1 -ExpandProperty 'Id') + 1 $_session = New-Object -TypeName PSCustomObject -Property @{ Uri = $Uri Name = $Name Id = $_sessionIdcount Credential = $Credential ApiVersion = $ApiVersion } if ($Proxy) { $_session | Add-Member -NotePropertyName 'Proxy' -NotePropertyValue $Proxy } if ($ProxyCredential) { $_session | Add-Member -NotePropertyName 'ProxyCredential' -NotePropertyValue $ProxyCredential } if ($null -eq $Global:_ZabbixSessions) { $Global:_ZabbixSessions = @() } $Global:_ZabbixSessions += $_session return $_session } end { } } # Remove-ZBXAction.ps1 function Remove-ZBXAction { <# .SYNOPSIS Removes a Zabbix action. .DESCRIPTION Removes a Zabbix action. .PARAMETER Uri The Zabbix instance uri. .PARAMETER Credential Specifies a user account that has permission to the project. .PARAMETER Proxy Use a proxy server for the request, rather than connecting directly to the Internet resource. Enter the URI of a network proxy server. .PARAMETER ProxyCredential Specifie a user account that has permission to use the proxy server that is specified by the -Proxy parameter. The default is the current user. .PARAMETER Session ZabbixPS session, created by New-ZBXSession. .PARAMETER Id The action id. .INPUTS None, does not support pipeline. .OUTPUTS PSObject, action ids that were removed. .EXAMPLE .LINK https://www.zabbix.com/documentation/4.2/manual/api/reference/action/delete #> [CmdletBinding(DefaultParameterSetName = 'ByCredential')] param ( [Parameter(Mandatory, ParameterSetName = 'ByCredential')] [uri] $Uri, [Parameter(ParameterSetName = 'ByCredential')] [pscredential] $Credential, [Parameter(ParameterSetName = 'ByCredential')] [string] $Proxy, [Parameter(ParameterSetName = 'ByCredential')] [pscredential] $ProxyCredential, [Parameter(Mandatory, ParameterSetName = 'BySession')] [object] $Session, [Parameter(Mandatory)] [int] $Id ) begin { if ($PSCmdlet.ParameterSetName -eq 'BySession') { $currentSession = $Session | Get-ZBXSession -ErrorAction 'Stop' | Select-Object -First 1 if ($currentSession) { $Uri = $currentSession.Uri $Credential = $currentSession.Credential $Proxy = $currentSession.Proxy $ProxyCredential = $currentSession.ProxyCredential $ApiVersion = $currentSession.ApiVersion } } } process { $body = @{ method = 'action.delete' jsonrpc = $ApiVersion id = 1 params = @($Id) } $invokeZabbixRestMethodSplat = @{ Body = $body Uri = $Uri Credential = $Credential ApiVersion = $ApiVersion ErrorAction = 'Stop' } if ($Proxy) { $invokeZabbixRestMethodSplat.Proxy = $Proxy if ($ProxyCredential) { $invokeZabbixRestMethodSplat.ProxyCredential = $ProxyCredential } else { $invokeZabbixRestMethodSplat.ProxyUseDefaultCredentials = $true } } return Invoke-ZBXRestMethod @invokeZabbixRestMethodSplat } end { } } # Remove-ZBXMaintenance.ps1 function Remove-ZBXMaintenance { <# .SYNOPSIS Removes a Zabbix maintenance period. .DESCRIPTION Removes a Zabbix maintenance period. .PARAMETER Uri The Zabbix instance uri. .PARAMETER Credential Specifies a user account that has permission to the project. .PARAMETER Proxy Use a proxy server for the request, rather than connecting directly to the Internet resource. Enter the URI of a network proxy server. .PARAMETER ProxyCredential Specifie a user account that has permission to use the proxy server that is specified by the -Proxy parameter. The default is the current user. .PARAMETER Session ZabbixPS session, created by New-ZBXSession. .PARAMETER Id The maintenance id. .INPUTS None, does not support pipeline. .OUTPUTS PSObject, maintenance ids that were removed. .EXAMPLE .LINK https://www.zabbix.com/documentation/4.2/manual/api/reference/maintenance/delete #> [CmdletBinding(DefaultParameterSetName = 'ByCredential')] [Alias("rzm")] param ( [Parameter(Mandatory, ParameterSetName = 'ByCredential')] [uri] $Uri, [Parameter(ParameterSetName = 'ByCredential')] [pscredential] $Credential, [Parameter(ParameterSetName = 'ByCredential')] [string] $Proxy, [Parameter(ParameterSetName = 'ByCredential')] [pscredential] $ProxyCredential, [Parameter(Mandatory, ParameterSetName = 'BySession')] [object] $Session, [Parameter(Mandatory)] [int] $Id ) begin { if ($PSCmdlet.ParameterSetName -eq 'BySession') { $currentSession = $Session | Get-ZBXSession -ErrorAction 'Stop' | Select-Object -First 1 if ($currentSession) { $Uri = $currentSession.Uri $Credential = $currentSession.Credential $Proxy = $currentSession.Proxy $ProxyCredential = $currentSession.ProxyCredential $ApiVersion = $currentSession.ApiVersion } } } process { $body = @{ method = 'maintenance.delete' jsonrpc = $ApiVersion id = 1 params = @($Id) } $invokeZabbixRestMethodSplat = @{ Body = $body Uri = $Uri Credential = $Credential ApiVersion = $ApiVersion ErrorAction = 'Stop' } if ($Proxy) { $invokeZabbixRestMethodSplat.Proxy = $Proxy if ($ProxyCredential) { $invokeZabbixRestMethodSplat.ProxyCredential = $ProxyCredential } else { $invokeZabbixRestMethodSplat.ProxyUseDefaultCredentials = $true } } return Invoke-ZBXRestMethod @invokeZabbixRestMethodSplat } end { } } # Remove-ZBXSession.ps1 function Remove-ZBXSession { <# .SYNOPSIS Removes a Zabbix session. .DESCRIPTION Removes a Zabbix session. If the session is saved, it will be removed from the saved sessions as well. .PARAMETER Id Session id. .PARAMETER Path The path where module data will be stored, defaults to $Script:ZabbixModuleDataPath. .INPUTS PSObject. Get-ZBXSession .OUTPUTS None. Does not supply output. .EXAMPLE Deletes a Zabbix session with the id of '2'. Remove-ZBXSession -Id 2 .EXAMPLE Deletes all AP sessions in memory and stored on disk. Remove-ZBXSession .LINK Zabbix documentation: https://www.zabbix.com/documentation/4.2/manual/api New-ZBXSession Get-ZBXSession Save-ZBXSession Initialize-ZBXSession #> [CmdletBinding()] [Alias("rzsess","dzsess")] param ( [Parameter(Mandatory, ValueFromPipelineByPropertyName)] [int] $Id, [Parameter()] [string] $Path = $Script:ZabbixModuleDataPath ) begin { } process { $sessions = Get-ZBXSession -Id $Id foreach ($session in $sessions) { if ($session.Saved -eq $true) { $newData = @{SessionData = @() } $data = Get-Content -Path $Path -Raw | ConvertFrom-Json foreach ($_data in $data.SessionData) { if ($_data.Id -eq $session.Id) { Continue } else { $newData.SessionData += $_data } } $newData | ConvertTo-Json -Depth 5 | Out-File -FilePath $Path } $Global:_ZabbixSessions = $Global:_ZabbixSessions | Where-Object { $PSItem.Id -ne $session.Id } } } end { } } # Save-ZBXSession.ps1 function Save-ZBXSession { <# .SYNOPSIS Saves a Zabbix session disk. .DESCRIPTION Saves a Zabbix session disk. The sensetive data is encrypted and stored in the users local application data. These saved sessions will be available next time the module is imported. .PARAMETER Session Zabbix session, created by New-ZBXSession. .PARAMETER Path The path where session data will be stored, defaults to $Script:ZabbixModuleDataPath. .PARAMETER PassThru Returns the saved session object. .INPUTS PSbject. Get-ZBXSession, New-ZBXSession .OUTPUTS None. Save-ZBXSession does not generate any output. .EXAMPLE Creates a session with the name of 'myZabbixInstance' saving it to disk. New-ZBXSession -Uri 'http://myCompany/zabbix/api_jsonrpc.php' -Credential $creds -Name myZabbixInstance | Save-ZBXSession .LINK Zabbix documentation: https://www.zabbix.com/documentation/4.2/manual/api New-ZBXSession Get-ZBXSession Remove-ZBXSession Initialize-ZBXSession #> [CmdletBinding()] param ( [Parameter(Mandatory, ValueFromPipeline)] [object] $Session, [Parameter()] [string] $Path = $Script:ZabbixModuleDataPath ) begin { if (-not(Test-Path $Path)) { $data = @{SessionData = @() } } else { $data = Get-Content -Path $Path -Raw | ConvertFrom-Json } } process { if ($data.SessionData.Id -notcontains $session.Id) { $_object = @{ Id = $Session.Id Name = $Session.Name Uri = $Session.Uri ApiVersion = $Session.ApiVersion Saved = $true } if ($Session.Credential) { $_credentialObject = @{ Username = $Session.Credential.UserName Password = ($Session.Credential.GetNetworkCredential().SecurePassword | ConvertFrom-SecureString) } $_object.Credential = $_credentialObject } if ($Session.Proxy) { $_object.Proxy = $Session.Proxy } if ($Session.ProxyCredential) { $_proxyCredentialObject = @{ Username = $Session.ProxyCredential.UserName Password = ($Session.ProxyCredential.GetNetworkCredential().SecurePassword | ConvertFrom-SecureString) } $_object.ProxyCredential = $_proxyCredentialObject } $data.SessionData += $_object $session | Remove-ZBXSession -Path $Path } } end { $data | ConvertTo-Json -Depth 5 | Out-File -FilePath $Path Write-Verbose "[$($MyInvocation.MyCommand.Name)]: [$Name]: Session data has been stored at [$Path]" } } # Imported from [D:\a\1\s\ZabbixPS\Tests] |