Append Array

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
obrienc
Posts: 59
Last visit: Wed Apr 20, 2022 5:43 am

Append Array

Post by obrienc »

When I try to append this array its not quite returning what I want. I want to list the Servername with its windows features but instead it returns the servername with an array attached to it. I tried to -expandproperty....

Code: Select all

$results = @()
$x = Get-Cluster -Domain Cloud|where {$_.Name -like "*hvc*"}|Get-ClusterNode
foreach ($item in $x)
{
  $r = Get-WindowsFeature -ComputerName $item|where installed
  $results += New-Object -TypeName PSObject -Property @{
    Server = $item
    Features = $r|Select -ExpandProperty Name
    }
}
$results|Select Server,Features
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Append Array

Post by jvierra »

This is how to enumerate embedded objects.

Code: Select all

Get-Cluster -Domain Cloud |
	Where-Object { $_.Name -like "*hvc*" } | 
	Get-ClusterNode |
	ForEach-Object{
		$server =$_.Name
		Get-WindowsFeature -ComputerName $_.Name | 
			Where-Object installed |
			ForEach-Object{
				[pscustomobject]@{
					Server    = $_.Name
					Feature  = $r_.Name
				}
			}
	}
User avatar
obrienc
Posts: 59
Last visit: Wed Apr 20, 2022 5:43 am

Re: Append Array

Post by obrienc »

Thanks. I ran it exactly as you posted and it lists the windows features under the Server header, and under Feature its blank.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Append Array

Post by jvierra »

Replace these two lines:

Server = $server
Feature = $_.Name
This topic is 6 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked