If your goal is to simply write out the services which are exploitable due to insecure permissions, you can assign your Get-CimInstance
result to a variable (e.g. $insecureServices
, and use a subexpression in a string to join the service names together:
Note: Write-Host can be used for informational output that does not need to be processed within the same PowerShell session (the information stream will still be visible on STDOUT from the calling process). You can read my answer here which goes into output streams in more detail.
Write-Output "Exploitable services: $($insecureServices.Name -join ', ')"
This will result in a comma-delimited list of services returned by your existing code (the below uses the names in your screenshot):
Exploitable services: ClickToRunSvc, DeepETPService, DeepMgmtService
Note: within a double-quoted string, the $()
operator will evaluate a sub-expression and return the result as a string. The above use of this is essentially shorthand for the following, and both will produce the same output:
$allServicesString = $insecureServices.Name -join ', '
Write-Output "Exploitable services: $allServicesString"
The reason this works is because even though you are returning a collection of services meeting specific criteria, PowerShell lets you operate on common properties of objects within the collection. So if every object in the collection has a Name
property, $insecureServices.Name
will return a collection of values for the Name
property on each object in the collection, even though [System.Array] does not have a property called Name
itself.
And since $insecureServices.Name
returns a collection of names, we can then use the -join
operator to join each element with ', '
.