Solved

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

Posted on 2014-01-24
6
889 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
A brief introduction to what I consider to be the best editor for PowerShell.
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…
how to add IIS SMTP to handle application/Scanner relays into office 365.

930 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now