-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAppDynamics.psm1
215 lines (187 loc) · 6.26 KB
/
AppDynamics.psm1
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<#
Function to load all external scripts/functions under the $PSScriptRoot\Scripts directory
#>
Write-Verbose 'Loading external scripts'
$scripts = Get-ChildItem $PSScriptRoot\scripts -Filter "*.ps1"
foreach ($script in $scripts){
. $script.FullName
}
Export-ModuleMember $scripts.BaseName
Write-Verbose 'External scripts Loaded'
#region Logging functions
function Write-AppDLog
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[Alias("LogContent")]
[string]$Message,
[Parameter(Mandatory=$false)]
[Alias('LogPath')]
[string]$Path='C:\Logs\AppDynamics-Powershell.log',
[Parameter(Mandatory=$false)]
[ValidateSet("Error","Warn","Info")]
[string]$Level="Info",
[Parameter(Mandatory=$false)]
[switch]$NoClobber
)
Begin
{
# Set VerbosePreference to Continue so that verbose messages are displayed.
$VerbosePreference = $PSCmdlet.GetVariableValue('VerbosePreference')
}
Process
{
# If the file already exists and NoClobber was specified, do not write to the log.
if ((Test-Path $Path) -AND $NoClobber) {
Write-Error "Log file $Path already exists, and you specified NoClobber. Either delete the file or specify a different name."
Return
}
# If attempting to write to a log file in a folder/path that doesn't exist create the file including the path.
elseif (!(Test-Path $Path)) {
Write-Verbose "Creating $Path."
New-Item $Path -Force -ItemType File | Out-Null
}
else {
# Nothing to see here yet.
}
# Format Date for our Log File
$FormattedDate = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
# Write message to error, warning, or verbose pipeline and specify $LevelText
switch ($Level) {
'Error' {
Write-Error $Message
$LevelText = 'ERROR:'
}
'Warn' {
Write-Warning $Message
$LevelText = 'WARNING:'
}
'Info' {
Write-Verbose $Message
$LevelText = 'INFO:'
}
}
# Write log entry to $Path
"$FormattedDate $LevelText $Message" | Out-File -FilePath $Path -Append
}
End
{
}
}
#endregion
#region Helper Functions. These wont get exported by the module, but will be available to be used by the exported cmdlets
function Put-AppDResource( [string]$uri, [object]$resource, [object]$connectionInfo) {
Write-AppDLog "$($MyInvocation.MyCommand)`turi:$uri`n`tbody:$resource"
Invoke-RestMethod -Method Put -Uri "$env:AppDURL/$uri" -Body $($resource | ConvertTo-Json -Depth 10) -Headers $connectionInfo.header -Verbose:$false
}
function Post-AppDResource([string]$uri, [object]$resource, [object]$connectionInfo) {
Write-AppDLog "$($MyInvocation.MyCommand)`turi:$uri`n`tbody:$resource"
Invoke-RestMethod -Method Post -Uri "$env:AppDURL/$uri" -Body $($resource | ConvertTo-Json -Depth 10) -Headers $connectionInfo.header -Verbose:$false
}
function Get-AppDResource([string]$uri, [object]$connectionInfo) {
Write-AppDLog "$($MyInvocation.MyCommand)`turi:$uri"
Invoke-RestMethod -Method Get -Uri (Join-Parts -Separator '/' -Parts $env:AppDURL,$uri) -Headers $connectionInfo.header -Verbose:$false
}
function Test-AppId
{
[CmdletBinding()]
param(
$AppDId,
$AppDName
)
Write-AppDLog -Message "Validating AppId..."
$AppId = @()
if($AppDId) {
foreach ($id in $AppDId) {
$AppId += (Get-AppDApplication -AppId $AppDId -ErrorAction SilentlyContinue).Id
}
}
elseif ($AppDName -and -not $AppDId) {
foreach ($name in $AppDName) {
$AppId += (Get-AppDApplication -AppName $name -ErrorAction SilentlyContinue).Id
}
}
elseif (-not $AppDId -and -not $AppDName)
{
$AppId = (Get-AppDApplication -ErrorAction SilentlyContinue).Id
}
if (!$AppId) {
$msg = "Failed to find application : ($AppDId|$AppDName)"
Write-AppDLog -Message $msg -Level 'Error'
Exit 1
}
Write-Output $AppId
}
function Join-Parts
{
param
(
$Parts = $null,
$Separator = ''
)
($Parts | Where-Object { $_ } | ForEach-Object {
([string]$_).trim($Separator)
} | Where-Object { $_ }) -join $Separator
}
function ConvertTo-EpochTime
{
param
(
[Parameter(Mandatory)]
[datetime]$datetime,
[Parameter(Mandatory=$false)]
[ValidateSet('Days',
'Hours',
'Minutes',
'Seconds',
'Milliseconds')]
$format = 'MilliSeconds'
)
$timeSpan = (New-TimeSpan -Start (Get-Date -Date "01/01/1970") -End $datetime)
switch ($format) {
Days { [MATH]::Floor($timeSpan.TotalDays) }
Hours { [MATH]::Floor($timeSpan.TotalHours) }
Minutes { [MATH]::Floor($timeSpan.TotalMinutes) }
Seconds { [MATH]::Floor($timeSpan.TotalSeconds) }
Milliseconds { [MATH]::Floor($timeSpan.TotalMilliseconds) }
}
}
function ConvertFrom-EpochTime
{
param
(
[Parameter(Mandatory)]
[long]$epochTime,
#AppDynamics Stores their timespamps in milliseconds
[Parameter(Mandatory=$false)]
[ValidateSet('Days',
'Hours',
'Minutes',
'Seconds',
'Milliseconds')]
$format = 'MilliSeconds',
#AppDynamics Stores their timespamps in UTC
[Parameter(Mandatory=$false)]
[switch]
$ToLocalTime
)
$epoch = Get-Date -Date '01/01/1970'
switch ($format) {
Days { $result = $epoch.AddDays($epochTime) }
Hours { $result = $epoch.AddHours($epochTime) }
Minutes { $result = $epoch.AddMinutes($epochTime) }
Seconds { $result = $epoch.AddSeconds($epochTime) }
Milliseconds { $result = $epoch.AddMilliseconds($epochTime) }
}
if ($ToLocalTime) {
[timezone]::CurrentTimeZone.ToLocalTime($result)
}
else {
$result
}
}
#endregion