-
Notifications
You must be signed in to change notification settings - Fork 7
/
GetCDPFromAPI
113 lines (93 loc) · 3.49 KB
/
GetCDPFromAPI
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
Clear-Host
@"
CDPFromAPI.ps1
The script retrieves the Custom Device Properties of all devices
The API user requires the following role permissions:
Devices->LAN/WAN Devices
* Custom Device Properties - Manage
* LAN/WAN Devices Settings - Manage
Created by: Jon Czerwinski, Cohn Consulting Corporation
Date: December 3, 2018
Version: 1.0
"@
#
# Generate a pseudo-unique namespace to use with the New-WebServiceProxy and
# associated types.
#
# By controlling the namespace, the script becomes portable and is not
# dependent upon the endpoint url the webservice is connecting. However, this
# introduces another complexity because once the namespace is defined within a
# powershell session, it cannot be reused, nor can it be undefined. As long as
# all the calls are made to the existing webserviceproxy, then everything would be
# OK. But, if you try to rerun the script without closing and reopening the
# powershell session, you will get an error.
#
# One way around this is to create a unique namespace each time the script is run.
# We do this by using the last 'word' of a GUID appended to our base namespace 'NAble'.
# This means our type names for parameters (such as T_KeyPair) now have a dynamic
# type. We could pass types to each new-object call using "$NWSNameSpace.T_KeyPair",
# and I find it more readable to define our 'dynamic' types here and use the typenames
# in variables when calling New-Object.
#
$NWSNameSpace = "NAble" + ([guid]::NewGuid()).ToString().Substring(25)
$KeyPairType = "$NWSNameSpace.T_KeyPair"
$DevicePropertyType = "$NWSNameSpace.DeviceProperty"
$DevicePropertiesType = "$NWSNameSpace.DeviceProperties"
Write-Output "Using Namespace $NWSNameSpace"
Write-Output " "
#
# Locate the Windows Agent Config folder
#
# By querying the Windows Agent Service path, the folder will be correctly identified
# even if it's not on the C: drive.
#
$AgentConfigFolder = (gwmi win32_service -filter "Name like 'Windows Agent Service'").PathName
$AgentConfigFolder = $AgentConfigFolder.Replace("bin\agent.exe", "config").Replace('"','')
#
# Get the N-Central server out of the ServerConfig.xml file
#
function Get-NCentralSvr() {
$ConfigXML = [xml](Get-Content "$Script:AgentConfigFolder\ServerConfig.xml")
$ConfigXML.ServerConfig.ServerIP
}
#
# Get the device's ApplianceID out of the ApplianceConfig.xml file
#
function Get-ApplianceID() {
$ConfigXML = [xml](Get-Content "$Script:AgentConfigFolder\ApplianceConfig.xml")
$ConfigXML.ApplianceConfig.ApplianceID
}
#
# Determine where the N-Central server is
#
$serverHost = Read-Host "Enter the fqdn of the N-Central Server "
#
# Get credentials
# We could read them as plain text and then create a SecureString from it
# By reading it as a SecureString, the password is obscured on entry
#
# We still have to extract a plain-text version of the password to pass to
# the API call.
#
$username = Read-Host "Enter N-Central user id "
$secpasswd = Read-Host "Enter password " -AsSecureString
$creds = New-Object System.Management.Automation.PSCredential ("\$username", $secpasswd)
$password = $creds.GetNetworkCredential().Password
$bindingURL = "https://" + $serverHost + "/dms/services/ServerEI?wsdl"
$nws = New-Webserviceproxy $bindingURL -credential $creds -Namespace ($NWSNameSpace)
#
# Set up and execute the query
#
$DevProps = $nws.devicePropertyList(
$username,
$password,
$null, # Pass nulls and get *all devices*
$null,
$null,
$null,
$false
)
($DevProps).Count
Foreach ($Device in $DevProps) {
$Device.Properties | ft *
}