Skip to content

Commit 1f34eea

Browse files
authored
Added ability to output to file (CSV and Text) 🪐
1 parent 9d8e8d7 commit 1f34eea

1 file changed

Lines changed: 56 additions & 22 deletions

File tree

Powershell/Invoke-SCXWinRMEnumeration.ps1

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
.NOTES
3838
Author: Blake Drumm
3939
Version: 1.1
40-
Created: November 17, 2023
40+
Created: November 17th, 2023
41+
Modified: November 26th, 2023
4142
#>
4243
[CmdletBinding(HelpUri = 'https://blakedrumm.com/')]
4344
param
@@ -60,8 +61,8 @@ function Invoke-SCXWinRMEnumeration
6061
[CmdletBinding(HelpUri = 'https://blakedrumm.com/')]
6162
param
6263
(
63-
[ValidateSet('Basic', 'Kerberos', '')]
64-
[string]$AuthenticationMethod,
64+
[ValidateSet('Basic', 'Kerberos')]
65+
[string]$AuthenticationMethod = 'Basic',
6566
[Parameter(Mandatory = $true,
6667
HelpMessage = 'Server names or IP addresses for SCX class enumeration.')]
6768
[Alias('ServerName')]
@@ -71,10 +72,26 @@ function Invoke-SCXWinRMEnumeration
7172
[string]$UserName,
7273
[System.Security.SecureString]$Password,
7374
[Parameter(HelpMessage = 'You can provide the credentials to utilize for the WinRM commands.')]
74-
[PSCredential]$Credential
75+
[PSCredential]$Credential,
76+
[Parameter(HelpMessage = 'Output type for the results. Valid values are CSV and Text.')]
77+
[ValidateSet('CSV', 'Text')]
78+
[string]$OutputType,
79+
[Parameter(HelpMessage = 'Output file path for the results.')]
80+
[string]$OutputFile
7581
)
7682

77-
if ($AuthenticationMethod -eq '')
83+
if ($OutputFile -and -not $OutputType)
84+
{
85+
Write-Warning "The -OutputType parameter is required."
86+
return
87+
}
88+
elseif (-NOT $OutputFile -and $OutputType)
89+
{
90+
Write-Warning "The -OutputFile parameter is required."
91+
return
92+
}
93+
94+
if ($AuthenticationMethod -eq '' -or -NOT $AuthenticationMethod)
7895
{
7996
try
8097
{
@@ -93,7 +110,7 @@ function Invoke-SCXWinRMEnumeration
93110
}
94111
elseif (-NOT $UserName -and -NOT $Password -and -NOT $Credential -and $AuthenticationMethod -eq 'Basic')
95112
{
96-
$Credential = (Get-Credential)
113+
$Credential = Get-Credential
97114
}
98115

99116
$scxClasses = @(
@@ -110,16 +127,16 @@ function Invoke-SCXWinRMEnumeration
110127
"SCX_DiskDriveStatisticalInformation",
111128
"SCX_FileSystemStatisticalInformation",
112129
"SCX_UnixProcessStatisticalInformation",
113-
"SCX_LANEndpoint",
114-
"SCX_LogFile"
130+
"SCX_LANEndpoint"
115131
)
116132

117133
if (-NOT $Classes -and -NOT $EnumerateAllClasses)
118134
{
119135
$EnumerateAllClasses = $true
120136
}
121-
Write-Host "===================================================="
122-
Write-Host "Authentication Method: $AuthenticationMethod" -ForegroundColor Gray
137+
138+
$results = @()
139+
123140
foreach ($ServerName in $ComputerName)
124141
{
125142
Write-Host "===================================================="
@@ -129,24 +146,24 @@ function Invoke-SCXWinRMEnumeration
129146
{
130147
if ($UserName -and $Password)
131148
{
132-
# Create a PSCredential object
133149
$Credential = New-Object System.Management.Automation.PSCredential($Username, $Password)
134150
}
151+
135152
if ($EnumerateAllClasses)
136153
{
137154
foreach ($class in $scxClasses)
138155
{
139156
Write-Host "Enumerating: $class" -ForegroundColor Cyan
140-
if ($Credential)
157+
$result = if ($Credential)
141158
{
142-
# Invoke WinRM Enumeration
143159
Get-WSManInstance -ComputerName $ServerName -Authentication $AuthenticationMethod -Credential:$Credential -Port 1270 -UseSSL -Enumerate "http://schemas.microsoft.com/wbem/wscim/1/cim-schema/2/$class`?__cimnamespace=root/scx"
144160
}
145161
else
146162
{
147-
# Invoke WinRM Enumeration
148163
Get-WSManInstance -ComputerName $ServerName -Authentication $AuthenticationMethod -Port 1270 -UseSSL -Enumerate "http://schemas.microsoft.com/wbem/wscim/1/cim-schema/2/$class`?__cimnamespace=root/scx"
149164
}
165+
166+
$results += $result
150167
}
151168
}
152169
else
@@ -155,36 +172,53 @@ function Invoke-SCXWinRMEnumeration
155172
{
156173
foreach ($c in $Classes)
157174
{
158-
Write-Host "Enumerating: $class" -ForegroundColor Cyan
159-
if ($Credential)
175+
Write-Host "Enumerating: $c" -ForegroundColor Cyan
176+
$result = if ($Credential)
160177
{
161-
# Now, you have a PSCredential object in the $credential variable
162178
Get-WSManInstance -ComputerName $ServerName -Authentication $AuthenticationMethod -Credential:$Credential -Port 1270 -UseSSL -Enumerate "http://schemas.microsoft.com/wbem/wscim/1/cim-schema/2/$c`?__cimnamespace=root/scx"
163179
}
164180
else
165181
{
166-
# Now, you have a PSCredential object in the $credential variable
167182
Get-WSManInstance -ComputerName $ServerName -Authentication $AuthenticationMethod -Port 1270 -UseSSL -Enumerate "http://schemas.microsoft.com/wbem/wscim/1/cim-schema/2/$c`?__cimnamespace=root/scx"
168-
169183
}
184+
185+
$results += $result
170186
}
171187
}
172188
else
173189
{
174190
Write-Warning "Please provide one or more classes to the '-Classes' parameter. Or you can use the '-EnumerateAllClasses' parameter to list all available data for the Linux Agent."
175191
break
176192
}
177-
178193
}
179194
}
180195
catch
181196
{
182-
Write-Warning "An error occurred on $ServerName` - $error"
197+
Write-Warning "An error occurred on $ServerName - $error"
198+
$results += "Error on $ServerName`: $_"
183199
}
184200
}
185201

202+
# Output handling
203+
if ($OutputType -eq 'CSV')
204+
{
205+
$results | Export-Csv -Path $OutputFile -NoTypeInformation
206+
Write-Host "CSV file output located here: " -ForegroundColor Green -NoNewline
207+
Write-Host "$OutputFile" -ForegroundColor Magenta
208+
return
209+
}
210+
elseif ($OutputType -eq 'Text')
211+
{
212+
$results | Out-File -FilePath $OutputFile
213+
Write-Host "Text file output located here: " -ForegroundColor Green -NoNewline
214+
Write-Host "$OutputFile" -ForegroundColor Magenta
215+
return
216+
}
217+
else
218+
{
219+
return $results
220+
}
186221
}
187-
188222
if ($Servers -or $ComputerName -or $Password)
189223
{
190224
Invoke-SCXWinRMEnumeration -ComputerName $ComputerName -Credential:$Credential -UserName $UserName -Password $Password -AuthenticationMethod $AuthenticationMethod -Classes $Classes -EnumerateAllClasses:$EnumerateAllClasses

0 commit comments

Comments
 (0)