How to add data to a specific member of a PSObject

Lets say I have a PSObject collection that has three NoteProperties like this:

WebServer         Website           Physical Folder

----------           ---------            --------------

server1        www.server1.com   e:\site1

server2        www.server2.com   e:\site2

server3       www.server3.com    e:\site3

...                ...                          ... (lots more)

Also, lets say I want to add a couple of additional properties like bindings and ssl which is gathered in a different way. So if I add two additional NoteProperties to this PSObject

bindings                ssl            site

---------             --------      ---------

test.site1.com       no         www.server1.com

test2.site2.com      yes      www.server2.com

How can I add this information to the first PSObject collection, making sure that the info for www.server1.com is matched with server1.com from the first object?

       

March 11th, 2014 11:13am

It basically comes down to using the Add-Member Cmdlet.

You could run your collection through a foreach loop and add additional members to the PSobjects on a condition.

If you need any more help, post a part of your script.

Example:

$Collection|foreach{
    $_ | Add-Member -MemberType NoteProperty -Name Bindings -Value $($_.Website )
    $_ | Add-Member -MemberType NoteProperty -Name SSL -Value $false
    $_ | Add-Member -MemberType NoteProperty -Name Site -Value "www.server1.com"
}



Free Windows Admin Tool Kit Click here and download it now
March 11th, 2014 12:10pm

that's a start, but note that it is the site attribute of the second list that is the same as the website attribute of the first, not the bindings attribute. Also, the bindings and ssl values are not the same in all cases but must be taken from the member of the second collection whose site attribute has the same value as the website attribute of the first collection.

I would change the second collection from an array to a hash table with each element addressed by its site value, then do it something like this:

  $collection1 | foreach {
    $bindings = ($coll2hash[$_.website]).bindings
    $ssl  = ($coll2hash[$_.website]).ssl
    $_ | Add-Member -MemberType NoteProperty -Name Bindings -Value $bindings
    $_ | Add-Member -MemberType NoteProperty -Name SSL -Value $ssl
  }

March 11th, 2014 1:50pm

I just gave an example on the Add-Member Cmdlet.

Cyberfreak95 will have to figure out how to fill the value of each member of each object in the collection.


For all we know Cyberfreak95 just did an Import-csv.
Free Windows Admin Tool Kit Click here and download it now
March 11th, 2014 2:17pm

I just gave an example on the Add-Member Cmdlet.

Cyberfreak95 will have to figure out how to fill the value of each member of each object in the collection.


For all we know Cyberfreak95 just did an Import-csv.

You seem to think he wanted to find out how to add a property to an object, while I assumed that he wanted to find out how he could determine how to select the data to be added from a second collection where the two collections had one property value in common. Maybe he will tell us which guess was closer to reality, and whether either of our comments have been useful to him.

I expect that his first collection (and possibly the second) did come from an import-csv as you suggest. But that doesn't make his question any less valid.

March 11th, 2014 4:45pm

The data is being acquired from two different sources using .net's server manager.

First I connect to the server and iterate through all the sites using GetSection("system.applicationHost/sites")

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration") 
$iis = new-object Microsoft.Web.Administration.ServerManager

#$configPaths = $iis.GetApplicationHostConfiguration().GetSection("configPaths").GetCollection() 

#lists sites
$configPaths = $iis.GetApplicationHostConfiguration().GetSection("system.applicationHost/sites").GetCollection()|Select-Object -ExpandProperty RawAttributes 


foreach ($app in $siteObj.Applications){

$sites += New-Object PSObject -Property @{}

...

}

Then I am using the servermanager to getWebConfiguration and search for all the sites that have custom SQL connection strings embedded in the application's configuration along with other application specific properties.

The result of these two processes is two custom PSObject collections with the common bond being the root IIS site itself. I have to do it this way because I do not know the names of all the sites nor do I know which applications have custom connection strings and I wanted to avoid crafting a lot of error checking code.

I cannot use a hashtable because a hashtable only contains a key and a value. In my example I know I only included two properties but in reality there are many more. 

As you can see my result is two PSObject collections with many individual pieces of data that need to be merged together. This is what I was looking for guidance on how to accomplish.

Free Windows Admin Tool Kit Click here and download it now
March 11th, 2014 6:06pm

It basically comes down to using the Add-Member Cmdlet.

You could run your collection through a foreach loop and add additional members to the PSobjects on a condition.

If you need any more help, post a part of your script.

Example:

$Collection|foreach{
    $_ | Add-Member -MemberType NoteProperty -Name Bindings -Value $($_.Website )
    $_ | Add-Member -MemberType NoteProperty -Name SSL -Value $false
    $_ | Add-Member -MemberType NoteProperty -Name Site -Value "www.server1.com"
}



  • Edited by gaff-jakobs Tuesday, March 11, 2014 4:39 PM
March 11th, 2014 7:05pm

I just gave an example on the Add-Member Cmdlet.

Cyberfreak95 will have to figure out how to fill the value of each member of each object in the collection.


For all we know Cyberfreak95 just did an Import-csv.
  • Edited by gaff-jakobs Tuesday, March 11, 2014 6:15 PM
Free Windows Admin Tool Kit Click here and download it now
March 11th, 2014 9:13pm

<snip>

The result of these two processes is two custom PSObject collections with the common bond being the root IIS site itself. I have to do it this way because I do not know the names of all the sites nor do I know which applications have custom connection strings and I wanted to avoid crafting a lot of error checking code.

I cannot use a hashtable because a hashtable only contains a key and a value. In my example I know I only included two properties but in reality there are many more. 

As you can see my result is two PSObject collections with many individual pieces of data that need to be merged together. This is what I was looking for guidance on how to accomplish.


you are correct in stating that a hashtable contains only a key and a value. But that value can be whatever you want it to be, including a copy one of the rows of one of your collections. That value would contain, for example, the bindings, ssl, and site properties of your second collection. The site property would be insignificant, though, as that would also be the key value through which you would find the related data.
March 12th, 2014 11:03am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics