2

I'm attempting to create a custom object from an existing object. However, I can't seem to figure out the best way to go about doing this.

My goal is to create a custom object for each computer name that will have all of the values from Application Name.

I'm not sure if I should use a foreach or a where clause to make this happen and I'm also not sure the correct syntax to use either.

Thanks in advance for any help with this.

The data in the existing object looks like:

Computer Name, IP Address, Application Name

host1,10.10.10.10,firefox

host1,10.10.10.10,chrome

host1,10.10.10.10,internet explorer

host2,11.11.11.11,firefox

host2,11.11.11.11,chrome

host2,11.11.11.11,opera

Code Example:

foreach ($global:ComputerName in $global:SNWReportObject."Computer name"){
    if ($_ -eq $global:ComputerName) {
        Add-Member -InputObject $global:NEWReportObject -MemberType NoteProperty -Name ApplicationName -Value $global:SNWReportObject."Application name"
    }

}

Results I'm hoping for:

Computer Name, Application Name

host1,firefox,chrome,internet explorer

host2,firefox,chrome,opera

1 Answer 1

1

Edit:
So your actual case was a bit weird since we're working with arrays of strings in a custom object rather than a hash table. The following code snippet will turn your object into a [HashTable] that is easy to work with. Assumption: all the input data is this well-formed

<# Example.csv
Computer name,IP-Address,Application
host1,10.10.10.10,firefox
host1,10.10.10.10,chrome
host1,10.10.10.10,internet explorer
host2,11.11.11.11,firefox
host2,11.11.11.11,chrome
host2,11.11.11.11,opera
#>
$Csv = Import-Csv -LiteralPath C:\Temp\Example.csv
[HashTable]$Hash=@{}

For ($i = 0; $i -lt ($Csv.'Computer name').Count; $i++)
{
    If ($Hash.ContainsKey($Csv.'Computer name'[$i]))
    {
        $Hash.($Csv.'Computer name'[$i]).Application += $Csv.Application[$i]
        Continue
    }
    $Hash.($Csv.'Computer name'[$i]) = @{
      Application = @($Csv.Application[$i])
    }
}

Now your members are accessible like so: $Hash.Host1.Application

To get your desired output:

Set-Content -Path C:\Temp\file.csv -Value 'Computer Name,Application Name'
$Hash.Keys |
  ForEach-Object
  {
      Add-Content -Path C:\temp\file.csv -Value "$_,$($Hash.$_.Application -join ',')"
  }

When working with a [PSCustomObject], your members are quite limited in what you can do. Here are the standard members (to ignore):

   TypeName: System.Management.Automation.PSCustomObject

Name        MemberType   Definition
----        ----------   ----------
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()
GetType     Method       type GetType()
ToString    Method       string ToString()

Knowing this, you can parse through the rest to copy:

$Comp = @('Equals','GetHashCode','GetType','ToString'
[HashTable]$CopyObject=@{}
($Object | Get-Member).Name |
  ForEach-Object {
      If ($_ -notin $Comp) {$CopyObject.$_ = $Object.$_}
  }
Sign up to request clarification or add additional context in comments.

9 Comments

While this is useful, it didn't quite accomplish what I was hoping for. My goal was to create the final output where there would be one computer name that contained all of the applications it had listed under it. The results I received was a list of all computer names and a list of all applications that all contained duplicates.
@MrMr Is your original object a .csv file?
Yes it is originally a .csv
@MrMr That's quite a difference. I'll adjust my answer later today then
I'm trying just to build the hash table first. My results show that there are individual computer names for keys, but the values for each are literally "IPAddr,App" and I cannot reference them like you indicated, I receive a "Missing property name after reference operator.".
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.