Output Clean Up

by dagr8tim at 2012-08-28 05:48:15

I created alittle script to compare the web.config files from our production with our non production environmental to identify any non environmental differences. I’m very happy with the script, but the output sucks. I tried adding the format wide option to the output string, but that didn’t help. Any suggestions on cleaning up the output?

# Creates a list of applications from the production web server. Comment out once list is created.
# gci \<prod_web_server> -recu web.config | select-string "<configuration>" | foreach{$.Path} > C:\temp\compare\list.txt

#Destination/Working Folder
$Dest_Folder = "C:\temp\compare"
$date = Get-Date
#Loads list file for use
$list = Get-Content -Path "$Dest_Folder\list.txt"
# Writes date to appended file
"$date" >"C:\temp\compare\compare.txt"

foreach ($prodfilename in $list) {

$stagefilename = $prodfilename.Replace("\<prod_web_server>", "\<nonprod_web_server>")
"nnComparing $prodfilename vs $stagefilename" >> "C:\temp\compare\compare.txt"
$File_Path1 = Get-Content -Path "$prodfilename"
$File_Path2 = Get-Content -Path "$stagefilename"

Compare-Object $File_Path1 $File_Path2 -SyncWindow 200 >> "C:\temp\compare\compare.txt"

}
by poshoholic at 2012-08-28 05:56:29
I think it would be helpful if you included a sample showing the output you get from this now, followed by the information you would like to see in the output more clearly so that someone can offer suggestions on how to change the output that is returned from this script.
by dagr8tim at 2012-08-28 06:30:24
[quote="poshoholic"]I think it would be helpful if you included a sample showing the output you get from this now, followed by the information you would like to see in the output more clearly so that someone can offer suggestions on how to change the output that is returned from this script.[/quote]

Here’s my output. My two complaints are 1) that it seems to stop the lines at 80 characters, 2) that it doesn’t seem to parse the entire file.

08/28/2012 08:31:39


Comparing \<prod_web_server>\Web.config vs \<nonprod_web_server>\Web.config

InputObject SideIndicator
----------- -------------
<add name="AbsenceManagementConnectionString" connectionSt… =>
<!–<add name="AbsenceManagementConnectionString" connect… =>
<add key="ODH.SingleSignOn.Wrapper.SingleSignOnService.Si… =>
<add key="AttachmentsFilePath" value="\odhdocsstg1\ODH\A… =>
<add key="ODHReportServiceUrl" value="http://odhappclstg1… =>
<!–<add key="ODH.SingleSignOn.Wrapper.SingleSignOnServic… =>
<add key="ReportingServerCredential" value="AM_Reports,#W… =>
<add key="ODHReportServiceUrl" value="http://odhappclprd1… =>
<sessionState mode="StateServer" stateConnectionString="t… =>
<!–<sessionState mode="StateServer" stateConnectionStrin… =>
<!–<add name="AbsenceManagementConnectionString" connecti… <=
<add name="AbsenceManagementConnectionString" connectionS… <=
<!–<add key="ODH.SingleSignOn.Wrapper.SingleSignOnServic… <=
<add key="AttachmentsFilePath" value="\odhdocsstg\ODH\Ab… <=
<add key="ODHReportServiceUrl" value="http://odhappclstg1… <=
<add key="ODH.SingleSignOn.Wrapper.SingleSignOnService.Si… <=
<add key="ReportingServerCredential" value="AM_Reports,c#… <=
<add key="ODHReportServiceUrl" value="http://odhappclprd1… <=
<!–<sessionState mode="StateServer" stateConnectionStrin… <=
<sessionState mode="StateServer" stateConnectionString="t… <=
by DonJ at 2012-08-28 06:33:23
So, the line stopping is expected. Pipe the output to Format-Table -wrap to fix that, or widen your PowerShell window.
by poshoholic at 2012-08-28 06:37:52
Right, now I get you. You can work around the PowerShell window width limitations that you’re seeing and get the output without these limitations in a text file using some techniques that have already been blogged about before.

As for whether or not it properly compares the entire file, you should be able to raise your -SyncWindow value to something much larger so that it can compare larger files.
by dagr8tim at 2012-08-28 09:21:49
Thank you for all of your help. That should be enough to get me moving forward on this.

EDIT:
This is what I came up with that works for me. This is the last two lines of the script.
$a = @{Expression={$
.InputObject};Label="Web Config Line";width=140},@{Expression={$.SideIndicator};Label="Side Indicator";width=15}
Compare-Object $File_Path1 $File_Path2 | Format-Table -Property $a -wrap | Out-String -Width 4096 | Out-File C:\temp\compare\compare.txt


This solves the output problem. Now I just need to suppress entries that exist in production but not in non production so I don’t get errors in my report.
by poshoholic at 2012-08-28 18:17:27
Excellent, glad it worked out for you! Do you mind if I mark this topic as solved then?
by dagr8tim at 2012-08-29 06:39:13
[quote="poshoholic"]Excellent, glad it worked out for you! Do you mind if I mark this topic as solved then?[/quote]

Absolutely. I’ve made some tweaks to the script, so I’ll post the entire script in it’s current form in case somebody is trying to do something similar.

#Destination/Working Folder
$Dest_Folder = "C:\temp\compare"

# Creates a list of applications from the production web server. Comment out once list is created.
# gci \<prod_web_server> -recu web.config | select-string "<configuration>" | foreach{$
.Path} > "$Dest_Folder\list.txt"

$date = Get-Date
#Loads list file for use
$list = Get-Content -Path "$Dest_Folder\list.txt"
# Writes date to appended file
"$date" >> "$Dest_Folder\compare.txt"

foreach ($prodfilename in $list) {
$stagefilename = $prodfilename.Replace("\<prod_web_server>", "\<nonprod_web_server>")
"nnComparing $prodfilename vs $stagefilename" >> "$Dest_Folder\compare.txt"
$File_Path1 = Get-Content -Path "$prodfilename"
$File_Path2 = Get-Content -Path "$stagefilename"
if(!(test-path -path $stagefilename)) {
"`nStage File Not Found: $stagefilename" >> "$Dest_Folder\compare.txt"
continue
}
$a = @{Expression={$.InputObject};Label="Web Config Line";width=140},@{Expression={$.SideIndicator};Label="Side Indicator";width=15}
Compare-Object $File_Path1 $File_Path2 | Format-Table -Property $a -wrap | Out-String -Width 6144 >> "$Dest_Folder\compare.txt"
}
by poshoholic at 2012-08-29 06:51:49
Thanks for sharing your final script!