Public/Invoke-CucmAxl.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
function Invoke-CucmAxl {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]
        $entity,
        [Parameter(Mandatory = $true)]
        [hashtable]
        $parameters,
        [string]
        $AXLVersion = '11.5',
        [Parameter(Mandatory = $true)]
        [string]
        $server,
        [Parameter(Mandatory = $true)]
        [pscredential]
        $Credential
    )
    $CUCMURL = "https://$server/axl/"

    $headers = @{
        'Content-Type' = 'text/xml; charset=utf-8'
        SOAPAction     = '"CUCM:DB ver={0} {1}"' -f $AXLVersion, $entity
    }
    $params = foreach ($paramKey in $parameters.Keys) {
        '<{0}>{1}</{0}>' -f $paramKey, $parameters[$paramKey]
    }
    $body = @'
        <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.cisco.com/AXL/API/{0}">
          <soapenv:Header/>
          <soapenv:Body>
            <ns:{1}>
              {2}
            </ns:{1}>
          </soapenv:Body>
        </soapenv:Envelope>
'@
 -f $AXLVersion, $entity, $params

    $IRMParams = @{
        Headers    = $headers
        Body       = $body
        Uri        = $CUCMURL
        Method     = 'Post'
        Credential = $Credential
    }
    if ($PSVersionTable.PSVersion.Major -ge 6) {
        $IRMParams.SkipCertificateCheck = $true
    }
    else {
        [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
    }
    try {
        Invoke-RestMethod @IRMParams |
            Select-XML -XPath '//return' |
            Select-Object -ExpandProperty Node
    }
    catch {
        Write-warning "Failed to execute AXL entity $entity. Error: $($_.exception)"
    }
}