Solved

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

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

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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Unified and professional email signatures help maintain a consistent company brand image to the outside world. This article shows how to create an email signature in Exchange Server 2010 using a transport rule and how to overcome native limitations …
In this video we show how to create a Contact 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 Recipients >> Contact ta…
To show how to generate a certificate request 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 Servers >> Certificates…

707 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