Skip to content

Commit ac61414

Browse files
authored
Added -PassThru parameter 🧇 🥚
1 parent 240754c commit ac61414

1 file changed

Lines changed: 72 additions & 19 deletions

File tree

Powershell/Invoke-SCXWinRMEnumeration.ps1

Lines changed: 72 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@
2626
.PARAMETER Credential
2727
You can provide the credentials to utilize for the WinRM commands.
2828
29+
.PARAMETER OutputType
30+
Output type for the results. Valid values are CSV and Text.
31+
32+
.PARAMETER OutputFile
33+
Output file path for the results.
34+
35+
.PARAMETER PassThru
36+
Do not Write-Host and pass through the Object data.
37+
2938
.EXAMPLE
3039
$securePassword = ConvertTo-SecureString 'Password1' -AsPlainText -Force
3140
Invoke-SCXWinRMEnumeration -ComputerName "Server1", "Server2" -UserName "admin" -Password $securePassword -AuthenticationMethod "Basic" -Classes SCX_Agent, SCX_OperatingSystem
@@ -36,9 +45,9 @@
3645
3746
.NOTES
3847
Author: Blake Drumm
39-
Version: 1.1
48+
Version: 1.2
4049
Created: November 17th, 2023
41-
Modified: November 26th, 2023
50+
Modified: November 26th, 2023
4251
#>
4352
[CmdletBinding(HelpUri = 'https://blakedrumm.com/')]
4453
param
@@ -53,7 +62,14 @@ param
5362
[string]$UserName,
5463
[System.Security.SecureString]$Password,
5564
[Parameter(HelpMessage = 'You can provide the credentials to utilize for the WinRM commands.')]
56-
[PSCredential]$Credential
65+
[PSCredential]$Credential,
66+
[Parameter(HelpMessage = 'Output type for the results. Valid values are CSV and Text.')]
67+
[ValidateSet('CSV', 'Text')]
68+
[string[]]$OutputType,
69+
[Parameter(HelpMessage = 'Output file path for the results.')]
70+
[string]$OutputFile,
71+
[Parameter(HelpMessage = 'Do not Write-Host and pass through the Object data.')]
72+
[switch]$PassThru
5773
)
5874

5975
function Invoke-SCXWinRMEnumeration
@@ -75,9 +91,11 @@ function Invoke-SCXWinRMEnumeration
7591
[PSCredential]$Credential,
7692
[Parameter(HelpMessage = 'Output type for the results. Valid values are CSV and Text.')]
7793
[ValidateSet('CSV', 'Text')]
78-
[string]$OutputType,
94+
[string[]]$OutputType,
7995
[Parameter(HelpMessage = 'Output file path for the results.')]
80-
[string]$OutputFile
96+
[string]$OutputFile,
97+
[Parameter(HelpMessage = 'Do not Write-Host and pass through the Object data.')]
98+
[switch]$PassThru
8199
)
82100

83101
if ($OutputFile -and -not $OutputType)
@@ -139,8 +157,14 @@ function Invoke-SCXWinRMEnumeration
139157

140158
foreach ($ServerName in $ComputerName)
141159
{
142-
Write-Host "===================================================="
143-
Write-Host "Current Server: $ServerName" -ForegroundColor Green
160+
if (-NOT $PassThru)
161+
{
162+
Write-Host "===================================================="
163+
Write-Host "Current Server: $ServerName"
164+
Write-Host "Authentication Method: " -NoNewline
165+
Write-Host "$AuthenticationMethod" -ForegroundColor DarkCyan
166+
}
167+
144168
$error.Clear()
145169
try
146170
{
@@ -153,7 +177,10 @@ function Invoke-SCXWinRMEnumeration
153177
{
154178
foreach ($class in $scxClasses)
155179
{
156-
Write-Host "Enumerating: $class" -ForegroundColor Cyan
180+
if (-NOT $PassThru)
181+
{
182+
Write-Host " Enumerating: $class" -ForegroundColor Cyan
183+
}
157184
$result = if ($Credential)
158185
{
159186
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"
@@ -172,7 +199,10 @@ function Invoke-SCXWinRMEnumeration
172199
{
173200
foreach ($c in $Classes)
174201
{
175-
Write-Host "Enumerating: $c" -ForegroundColor Cyan
202+
if (-NOT $PassThru)
203+
{
204+
Write-Host " Enumerating: $c" -ForegroundColor Cyan
205+
}
176206
$result = if ($Credential)
177207
{
178208
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"
@@ -202,22 +232,45 @@ function Invoke-SCXWinRMEnumeration
202232
# Output handling
203233
if ($OutputType -eq 'CSV')
204234
{
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
235+
if ($OutputType -match 'Text')
236+
{
237+
$ParentDirectory = Split-Path $OutputFile
238+
$OutputPath = "$ParentDirectory\$([System.IO.Path]::GetFileNameWithoutExtension($OutputFile)).csv"
239+
}
240+
else
241+
{
242+
$OutputPath = $OutputFile
243+
}
244+
$results | Export-Csv -Path $OutputPath -NoTypeInformation
245+
if (-NOT $PassThru)
246+
{
247+
Write-Host "CSV file output located here: " -ForegroundColor Green -NoNewline
248+
Write-Host "$OutputPath" -ForegroundColor Yellow
249+
}
209250
}
210-
elseif ($OutputType -eq 'Text')
251+
if ($OutputType -eq 'Text')
211252
{
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
253+
if ($OutputType -match 'CSV')
254+
{
255+
$ParentDirectory = Split-Path $OutputFile
256+
$OutputPath = "$ParentDirectory\$([System.IO.Path]::GetFileNameWithoutExtension($OutputFile)).txt"
257+
}
258+
else
259+
{
260+
$OutputPath = $OutputFile
261+
}
262+
$results | Out-File -FilePath $OutputPath
263+
if (-NOT $PassThru)
264+
{
265+
Write-Host "Text file output located here: " -ForegroundColor Green -NoNewline
266+
Write-Host "$OutputPath" -ForegroundColor Yellow
267+
}
216268
}
217269
else
218270
{
219-
return $results
271+
$results
220272
}
273+
return
221274
}
222275
if ($Servers -or $ComputerName -or $Password)
223276
{

0 commit comments

Comments
 (0)