Quantcast
Channel: Windows PowerShell Forum
Viewing all articles
Browse latest Browse all 2562

How to merge data from two hashtabes with different types of data

$
0
0

I've been working on this one for a few hours and I am out of ideas, am hoping someone here had dealt with a similar issue. My problem is I am collecting data from two sources, one source is Get-Website command-let and the other is from parsing the web.config files that are located in each webstes root folder.

Here's what I have so far:

#gathers IIS Site information
$sitelist = get-website | select name,id,state,physicalpath, 
@{n="Bindings"; e= { ($_.bindings | select -expa collection) -join ';' }} ,
@{n="LogFile";e={ $_.logfile | select -expa directory}}

# this will dump sitelist to csv
$sitelist | export-csv -notypeinformation e:\iisInfo.txt

#gets the server name
$computer = get-content env:computername
#grabs the website infor out of the CSV
$fileCSV = "e:\iisInfo.txt"
$newcsv = Import-Csv $fileCsv
#adds the server's name to the hashtable
$newCsv | Add-Member -MemberType NoteProperty -Name 'ServerName' -Value $computer

foreach ($website in $newcsv)
{
$webConfigPath = $website."physicalPath"+'\web.config'
[xml]$connstr = Get-Content $webConfigPath
$data = $connstr.configuration.connectionStrings.add | where  {$_.name -eq "SQLConnectionString"}| select-object  -expand connectionstring
$data -match 'Data Source=(.+);Initial Catalog=(.+);Persist Security Info=.+;User ID=(.+);Password=.+'
$params = @{
    'SQL Server' = ($Matches[1] -split ',')[0]
    'SQL Port' = ($Matches[1] -split ',')[1]
    'DB Name' = $Matches[2]
    'DB User' = $Matches[3]
}
$sqllist= New-Object psObject -Property $params | select 'SQL Server', 'SQL Port', 'DB Name', 'DB User'
write-host $sqllist
## This is where I would except the merge into $newCSV to happen - any ideas how I can do that?
}

The foreach block grabs the SQL info from the different web.configs works, but it I cannot figure out to merge it into $newCSV successfully.

Here's the output from $newcsv:

name         : vendor.myweb.com
id           : 2
state        : Started
physicalPath : E:\vendor websites
Bindings     : http *:80:jimmys.myweb.com;http *:80:freds.myweb.com;http *:80:larrys.myweb.com
LogFile      : E:\IISLogs\
ServerName   : WEBSERVER

name         : employee.myweb.com
id           : 3
state        : Started
physicalPath : E:\webs\employees websites
Bindings     : http *:80:work.myweb.com
LogFile      : E:\logfiles
ServerName   : WEBSERVER

name         : home.myweb.com
id           : 4
state        : Started
physicalPath : E:\webs\corp web
Bindings     : http *:80:corpweb.myweb.com
LogFile      : E:\logfiles
ServerName   : WEBSERVER

This is the output from $sqllist (but keep in mind this is only for ONE siteend as you can see from the previous output, I have several web.configs that I need to gather info from)

SQL Server                    SQL Port                      DB Name                       DB User
----------                           --------                      -------                           -------
sql5                                   1433                        applicationDB             web_user

I already have a "foreach" loop for ($website in $newcsv) and I am using the physicalPath from the Get-Website info to populate the $webConfigPath variable, which is working!

I cannot figure out how I can take the output from $sqllist and merge it into the $newcsv hash table on everyforeach iteration (because on the next iteration it is overwritten). Every time I have tried it goes horribly wrong.

This would be my ideal output from $newcsv:


name         : vendor.myweb.com
id           : 2
state        : Started
physicalPath : E:\vendor websites
Bindings     : http *:80:jimmys.myweb.com;http *:80:freds.myweb.com;http *:80:larrys.myweb.com
LogFile      : E:\IISLogs\
ServerName   : WEBSERVER
SQL Server  : sql6
SQL Port     : 1433
DB Name      : applicationDB
DB User      : web_user2

name         : employee.myweb.com
id           : 3
state        : Started
physicalPath : E:\webs\employees websites
Bindings     : http *:80:work.myweb.com
LogFile      : E:\logfiles
ServerName   : WEBSERVER
SQL Server  : sql7
SQL Port     : 1433
DB Name      : applicationDB
DB User      : web_user5

name         : home.myweb.com
id           : 4
state        : Started
physicalPath : E:\webs\corp web
Bindings     : http *:80:corpweb.myweb.com
LogFile      : E:\logfiles
ServerName   : WEBSERVER
SQL Server  : sql9
SQL Port     : 1433
DB Name      : appDB
DB User      : user4

I'm probably missing something simple but every time I have tried it ends up adding the DB stuff as repeated data on the end of $newcsv:

name         : home.myweb.com
id           : 4
state        : Started
physicalPath : E:\webs\corp web
Bindings     : http *:80:corpweb.myweb.com
LogFile      : E:\logfiles
ServerName   : WEBSERVER

...

SQL Server  : sql9 <---- bad because it should be a part of the data above, not separate
SQL Port     : 1433
DB Name      : appDB
DB User      : user4

SQL Server  : sql9<---- bad because it should be a part of the data above, not separate - and also this is duplicated.
SQL Port     : 1433
DB Name      : appDB
DB User      : user4

Thanks for looking!


Viewing all articles
Browse latest Browse all 2562

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>