?
Solved

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

Posted on 2014-01-24
6
Medium Priority
?
1,015 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 40

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
Ransomware Attacks Keeping You Up at Night?

Will your organization be ransomware's next victim?  The good news is that these attacks are predicable and therefore preventable. Learn more about how you can  stop a ransomware attacks before encryption takes place with our Ransomware Prevention Kit!

 
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 40

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will help to fix the below errors for MS Exchange Server 2013 I. Certificate error "name on the security certificate is invalid or does not match the name of the site" II. Out of Office not working III. Make Internal URLs and Externa…
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
In this video we show how to create an Address List in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Organization >> Ad…
This video discusses moving either the default database or any database to a new volume.
Suggested Courses

777 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