Solved

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

Posted on 2014-01-24
6
907 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
  • 3
  • 2
6 Comments
 
LVL 17

Accepted Solution

by:
Barry Cunney earned 250 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 39

Assisted Solution

by:footech
footech earned 250 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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 39

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Marketers need statistics and metrics like everybody else needs oxygen. In this article we explain how to enable marketing campaign statistics for Microsoft Exchange mail.
Read this checklist to learn more about the 15 things you should never include in an email signature.
In this Micro Video tutorial you will learn the basics about Database Availability Groups and How to configure one using a live Exchange Server Environment. The video tutorial explains the basics of the Exchange server Database Availability grou…
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…

809 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