Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Powershell script extend a colum width past the -wrap -autosize

Posted on 2014-01-24
6
Medium Priority
?
1,087 Views
Last Modified: 2014-01-28
Hi,

I'm trying to write a script to ammend a certiciate to add an additional service to it.  Before I try the update I was trying to gather all the information needed piror to making an update.  One of my colums doesnt have enough space when using Format-Table -Wrap -AutoSize, which also shows up in my *.csv outputs. Wondering if I could get ideas on how to resolve this issue.  

# My script:
# Check Servers Network Connection
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction:SilentlyContinue
#
$txt1 = "D:\bhav\ExServersAlive.txt"
$txt2 = "D:\bhav\RemoteShellTestPassed.txt"
$txt3 = "D:\bhav\RemoteShellTestFailed.txt"
$txt4 = "D:\bhav\RemoteExPSGood.csv"
$txt7 = "D:\bhav\RemoteExPSCN.csv"
$txt5 = "D:\bhav\RemoteExPSBad.txt"
$txt6 = "D:\bhav\ExServersAlive1.txt"
#
$oldtxt1 = "D:\bhav\ExServersAlive.txt"
$oldtxt2 = "D:\bhav\RemoteShellTestPassed.txt"
$oldtxt3 = "D:\bhav\RemoteShellTestFailed.txt"
$oldtxt4 = "D:\bhav\RemoteExPSGood.csv"
$oldtxt5 = "D:\bhav\RemoteExPSBad.txt"
$oldtxt7 = "D:\bhav\RemoteExPSCN.csv"
#
remove-item $oldtxt1 -ErrorAction:SilentlyContinue
remove-item $oldtxt2 -ErrorAction:SilentlyContinue
remove-item $oldtxt3 -ErrorAction:SilentlyContinue
remove-item $oldtxt4 -ErrorAction:SilentlyContinue
remove-item $oldtxt5 -ErrorAction:SilentlyContinue
remove-item $oldtxt7 -ErrorAction:SilentlyContinue
#
Write-Host "Pass Line-39 - Test Network Connection Section Start" -ForegroundColor Yellow
#
$ExchangeServers = get-exchangeserver |? {$_.IsE14OrLater -eq $True -and $_.IsClientAccessServer}
$ExchangeServersAlive = @()
foreach ($exCasSvr in $ExchangeServers) {
      IF (Test-Connection -ComputerName $exCasSvr.Name -Quiet) {
            # Write-Host "Test Server connection to $($exCasSvr.Name) is ok" -ForegroundColor White
            $ExchangeServersAlive += $exCasSvr
      } ELSE {
            # Write-Host "Test Server connection to $($exCasSvr) has failed" -ForegroundColor DarkRed
      }
}
$ExchangeServersAlive | % {Write-Host $_.Name -ForegroundColor Green}
$x = $ExchangeServersAlive | % {$_.Name}
$x > $txt1
#
Write-Host "Pass Line-55 - Test Network Connection Section End" -ForegroundColor Yellow
#
Write-Host "Pass Line-57 - Cert Data Section Start" -ForegroundColor Yellow
#
$results = @()
$RemoteExGood = @()
$RemoteExBad = @()
#
IF($x -ne $NULL) {
      Foreach ($Casserver in $x) {
      $casserver2 = [STRING] $Casserver
      $Cert1 = Get-ExchangeCertificate -server "$casserver2" -erroraction:silentlycontinue
      $CertT = $Cert1 | Where {$_.CertificateDomains -ilike "*Outlook*"}
            $hash = @{
                              CASServer = $casserver2.ToString()
                              CertificateDomains = $CertT.CertificateDomains
                              FriendlyName = $CertT.FriendlyName
                              Services = $CertT.Services          
                              Thumbprint = $CertT.Thumbprint          
                              Status = $CertT.Status
                              Issuer = $CertT.Issuer                                                                      
                        }      
                  IF($CertT -eq $NUll) {
                        Write-Host "The server $Casserver2 certificate did not match"
                        $RemoteExBad += $CasServer2                                    
                                                            } ELSE {
                                                                  $Object = New-Object PSObject
                                                                  $Object | Add-Member -Name 'CASServer' -MemberType Noteproperty -Value $hash.CASServer
                                                                  $Object | Add-Member -Name 'CertificateDomains' -MemberType Noteproperty -Value $hash.CertificateDomains
                                                                  $Object | Add-Member -Name 'FriendlyName' -MemberType Noteproperty -Value $hash.FriendlyName
                                                                  $Object | Add-Member -Name 'Services' -MemberType Noteproperty -Value $hash.Services
                                                                  $Object | Add-Member -Name 'Thumbprint' -MemberType Noteproperty -Value $hash.Thumbprint
                                                                  $Object | Add-Member -Name 'Status' -MemberType Noteproperty -Value $hash.Status
                                                                  $Object | Add-Member -Name 'Issuer' -MemberType Noteproperty -Value $hash.Issuer
                                                                  $results += $object
                                                                  }
                                                }
                                          }

# $results
#
$results | Format-Table 'CASServer','FriendlyName','Services','Thumbprint','Status','Issuer' -Wrap | Out-File -FilePath $txt4
$results | Format-Table 'CASServer','CertificateDomains' -Wrap -AutoSize | Out-File -FilePath $txt7
#import-csv tabdelimitedfile.txt -delimiter "`t" | export-csv csvfile.csv
# $results | Export-Csv $txt4
# $bp = $hash.CertificateDomains.ToString()
# $results | select-object 'CASServer','FriendlyName','Services''Thumbprint''Status''Issuer' |export-csv -Delimiter "`t" -Path $txt4
$RemoteExBad  > $txt5
Write-Host "Pass Line 91 - Cert Data Section End" -ForegroundColor Yellow
#
#
$data = Get-Content -Path $txt4
write-host $data.count total lines read from file
 foreach ($line in $data)
 {
     write-host $line
 }

 $data1 = Get-Content -Path $txt7
write-host $data1.count total lines read from file
 foreach ($line in $data1)
 {
     write-host $line
 }
0
Comment
Question by:patelbg2001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 17

Accepted Solution

by:
Barry Cunney earned 500 total points
ID: 39807064
Hi Patel
use Out-String -Width

$results | Format-Table 'CASServer','CertificateDomains' -Wrap -AutoSize | Out-String -Width 300 |Out-File -FilePath
0
 
LVL 41

Assisted Solution

by:footech
footech earned 500 total points
ID: 39807100
Instead of using Format-Table and Out-String, I would use Select-Object and Export-CSV.
$results | Select 'CASServer','FriendlyName','Services','Thumbprint','Status','Issuer' | Export-CSV -Path $txt4 -notype
$results | Select 'CASServer','CertificateDomains' | Export-CSV -Path $txt7 -notype

Open in new window

It looks like you have that commented out in your code.  I'm not certain it would work, but I would expect it to.  Did you encounter an issue when using that?
0
 
LVL 6

Author Comment

by:patelbg2001
ID: 39807793
Using the export-csv, would require the output of certain columns to be presented as string first. I will try the out-string in the morning. See if that helps.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 6

Author Comment

by:patelbg2001
ID: 39813534
Dump out to txt file & manipulate the file.
gc $txt4) | ForEach {$_ -replace "-",""} | set-content $txt4
(gc $txt4) | ? {$_.trim() -ne "" } | set-content $txt4
(gc $txt4) | ForEach {$_ -replace " +", ":"} | set-content $txt4
(gc $txt4) | ForEach {$_ -replace ",:", ","} | set-content $txt4
0
 
LVL 41

Expert Comment

by:footech
ID: 39813772
If the suggestions didn't get you what you needed, if you post your own solution it's better to accept that as the solution rather other posts.
0
 
LVL 6

Author Comment

by:patelbg2001
ID: 39814374
Sorry, I will in future thanks.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
As much as Microsoft wants to kill off PST file support, just as they tried to do with public folders, there are still times when it is useful or downright necessary to export Exchange mailboxes to PST files. Thankfully, it is still possible to e…
The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…
Suggested Courses

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question