-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubmit-Computer.ps1
216 lines (164 loc) · 6.41 KB
/
Submit-Computer.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
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
216
#requires -version 3
<#
.SYNOPSIS
Crawl an ActiveDirectory for available Computerobjects.
.DESCRIPTION
The script crawls an ActiveDirectory for available Computerobjects and submits them to an IDB.
.PARAMETER <Parameter_Name>
<Brief description of parameter input required. Repeat this attribute if required>
.INPUTS
NONE
.OUTPUTS Log File
The script log file stored in C:\Windows\Temp\<name>.log
.NOTES
Version: 1.0
Author: Felix Kronlage
Creation Date: 10/09/2015
Purpose/Change: Initial script development
.EXAMPLE
<Example goes here. Repeat this attribute for more than one example>
<Example explanation goes here>
#>
#---------------------------------------------------------[Initialisations]--------------------------------------------------------
#Set Error Action to Silently Continue
#$ErrorActionPreference = 'SilentlyContinue'
#Import PSLogging Module
#Import-Module PSLogging
#Dot Source required Function Libraries
. "$PSScriptRoot\contrib\Function-Write-Log.ps1"
. "$PSScriptRoot\contrib\Get-PendingUpdate.ps1"
#Import ActiveDirectory Module
Import-Module ActiveDirectory
#----------------------------------------------------------[Declarations]----------------------------------------------------------
$LogPath = "$PSScriptRoot\idbad.log"
$ConfigPath = "$PSScriptRoot\config.xml"
Write-Log -Path $LogPath -Message "Loading config $ConfigPath"
[xml]$XMLConfig = Get-Content "$ConfigPath"
$IDBUrl = $XMLConfig.Settings.IDBURL
$IDBApiToken = $XMLConfig.Settings.IDBApiToken
$ADFilter = $XMLConfig.Settings.ADFilter
[bool]$IgnoreSSL = [System.Convert]::ToBoolean($XMLConfig.Settings.IgnoreSSL)
[bool]$IDBCreateMachine = [System.Convert]::ToBoolean($XMLConfig.Settings.IDBCreateMachine)
Write-Log -Path $LogPath -Message "IDB Url: $IDBUrl"
Write-Log -Path $LogPath -Message "IDB Create Machine: $IDBCreateMachine"
Write-Log -Path $LogPath -Message "AD Filter: $ADFilter"
Write-Log -Path $LogPath -Message "Ignore SSL: $IgnoreSSL"
#-----------------------------------------------------------[Functions]------------------------------------------------------------
function Ignore-SSLCertificates
{
Write-Log -Path $LogPath -Message "Ignoring broken SSL Certificates"
$Provider = New-Object Microsoft.CSharp.CSharpCodeProvider
$Compiler = $Provider.CreateCompiler()
$Params = New-Object System.CodeDom.Compiler.CompilerParameters
$Params.GenerateExecutable = $false
$Params.GenerateInMemory = $true
$Params.IncludeDebugInformation = $false
$Params.ReferencedAssemblies.Add("System.DLL") > $null
$TASource=@'
namespace Local.ToolkitExtensions.Net.CertificatePolicy
{
public class TrustAll : System.Net.ICertificatePolicy
{
public bool CheckValidationResult(System.Net.ServicePoint sp,System.Security.Cryptography.X509Certificates.X509Certificate cert, System.Net.WebRequest req, int problem)
{
return true;
}
}
}
'@
$TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)
$TAAssembly=$TAResults.CompiledAssembly
## We create an instance of TrustAll and attach it to the ServicePointManager
$TrustAll = $TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
[System.Net.ServicePointManager]::CertificatePolicy = $TrustAll
}
Function Get-Pending-Updates-Per-Computer {
<#
.SYNOPSIS
Use the Get-PendingUpdates Cmdlet to get updates per Maschine and parse them
.DESCRIPTION
Submits machine information to an infrastructure database
.PARAMETER ComputerName
Mandatory. The name of the computer object you want to have the pending updates for.
.INPUTS
Parameters above
#>
[CmdletBinding()]
Param ([Parameter(Mandatory=$true)]$ComputerName)
$num_updates= 0;
$num_sec_updates= 0;
$updates= Get-PendingUpdate -Computer $ComputerName
foreach ($update in $updates) {
<#
Parse String and count:
security-update++ if the title contains Security
#>
if ($update."Title" -match "Security") {
$num_sec_updates++
}
$num_updates++
}
Write-Log -Path $LogPath -Message "1: $num_updates"
return @{ "Updates"= $num_updates; "Security_Updates"= $num_sec_updates}
}
Function Submit-Computer {
<#
.SYNOPSIS
Submits machine information to an infrastructure database
.DESCRIPTION
Submits machine information to an infrastructure database
.PARAMETER Computer
Mandatory. The computer object you want to submit.
.INPUTS
Parameters above
.OUTPUTS
NONE
.NOTES
Version: 1.0
Author: Felix Kronlage
Creation Date: 13.10.2015
Purpose/Change: Initial function development
.EXAMPLE
Submit-Computer -Computer ...
#>
[CmdletBinding()]
Param ([Parameter(Mandatory=$true)]$Computer)
$machine_data= Get-ADComputer -Identity $Computer -Property *
$fqdn= $machine_data."DNSHostName"
$os= $machine_data."OperatingSystem"
$os_release= $machine_data."OperatingSystemVersion"
$ip4= $machine_data."IPv4Address"
$Updates= Get-Pending-Updates-Per-Computer($Computer."Name")
$P_u= $Updates.Get_Item("Updates")
$P_su= $Updates.Get_Item("Security_Updates")
$createMachine = ""
if ($IDBCreateMachine) {
$createMachine = "true"
} else {
$createMachine = "false"
}
$json= "{
""fqdn"":""$fqdn"",
""os"":""$os"",
""os_release"":""$os_release"",
""nics"":[{""ip_address"": {""addr"": ""$ip4"" }, ""name"": ""eth0""}],
""pending_updates"":""$P_u"",
""pending_security_updates"":""$P_su"",
""create_machine"":""$createMachine""
}"
Write-Log -Path $LogPath -Message "Dumping the body: $json"
try {
Invoke-RestMethod -Method PUT -ContentType "application/json" -Body $json -Uri "${IDBUrl}?idb_api_token=${IDBApiToken}"
} catch {
$res_status = $_.Exception.Response.StatusCode
Write-Log -Path $LogPath -Message "IDB returned: $res_status"
}
}
#-----------------------------------------------------------[Execution]------------------------------------------------------------=
if ($IgnoreSSL) {
Ignore-SSLCertificates
}
$machines= Get-ADComputer -Filter $ADFilter
foreach ($machine in $machines) {
Submit-Computer($machine)
}