?
Solved

Formating Table output so that it doesn't truncate columns

Posted on 2013-12-04
3
Medium Priority
?
3,915 Views
Last Modified: 2013-12-09
Ok,

So I have a PowerShell script what now works pretty well to dump a basic weekly audit.

The big issue at this point is that it mails a text file that tells me:

- Accounts created this week
- Groups modified this week
- Users not logged on in 30 days
- Users not logged on in 30-60 days
- Users not logged on in over 90 days
- Users with active accounts that haven't logged on ever

To create the output, I'm doing this:

$OutputfilePath = "C:\ADUserAudit-"+([datetime]::Now).tostring("yyyyMMdd")+".txt"
$30DaysTable = ($30Days | format-table)
$60DaysTable = ($60Days | format-table)
$90DaysTable = ($90Days | format-table)

# Get users that have never logged in, but use Windows AD Powershell cmdlets as it should look at all domain controllers. Not restricted to OU:

$NeverBeenKissed = get-aduser -f {-not ( lastlogontimestamp -like "*") -and (enabled -eq $true)} | select samaccountname,name,distinguishedname

$NeverBeenKissedTable = $NeverBeenKissed | format-table
 
# Write to a file

Out-File $OutputfilePath

Add-Content -Path $OutputfilePath "Users Created in the Last Week:"
Add-Content -Path $OutputfilePath " "
$UsersCreatedLastWeekTable | Out-File $OutputfilePath -append
Add-Content -Path $OutputfilePath "Groups Modified in the Last Week:"
Add-Content -Path $OutputfilePath " "
$GroupsModifiedLastWeekTable | Out-File $OutputfilePath -append
Add-Content -Path $OutputfilePath "Users not logged in for 30 days:"
Add-Content -Path $OutputfilePath " "
$30DaysTable | Out-File $OutputfilePath -append
Add-Content -Path $OutputfilePath "Users not logged in for 60-90 days:"
Add-Content -Path $OutputfilePath " "
$60DaysTable | Out-File $OutputfilePath -append
Add-Content -Path $OutputfilePath "Users not logged in for over 90 days:"
Add-Content -Path $OutputfilePath " "
$90DaysTable | Out-File $OutputfilePath -append
Add-Content -Path $OutputfilePath "Users who have NEVER logged in!:"
Add-Content -Path $OutputfilePath " "
$NeverBeenKissedTable | Out-File $OutputfilePath -append

Open in new window


When I run the powershell script manually, I get a non-truncated output.  When I run it as a timer job, I get:

Name                       DN                                          samaccountname          
----                             --                                             --------------          
John Smith              CN=John Smith,OU=Use...    jsmith

I've looked at the format-table options, and I don't really see one that will obviously fix my problem.

How to I fix this, or put it in a format that will allow me to email it without loosing critical information?

My email command is this:

Send-MailMessage -To "group@company.com” -Subject “User Audit 7/7/30/60/90” -From “PowerShellAudit@company.com” -Body “Current weekly AD User Audit is attached. Note: You need to look at this file full screen.” -Priority High -SmtpServer $smtpServer -Attachments $OutputfilePath 

Open in new window

0
Comment
Question by:gerhardub
[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
  • 2
3 Comments
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 39696040
Use -Autosize at the end of the format-table
Or
use -Wrap option
0
 
LVL 1

Accepted Solution

by:
gerhardub earned 0 total points
ID: 39696443
I ended up putting this in the beginning of the script... the blue and white is not needed.

# Prepare the UI environment so that we don't have an issue with the report being truncated:

$Shell = $Host.UI.RawUI
$Shell.WindowTitle="One PowerShell to Rule Them"
$Shell.BackgroundColor="Blue"
$Shell.ForegroundColor="White"
$size = $Shell.WindowSize
$size.width=240
$size.height=60
$Shell.WindowSize = $size
$size.width=240
$size.height=3000
$Shell.BufferSize = $size
Clear-Host

This was repurposed from a $profile article... so obviously the blue and white doesn't matter.  However, as long as the width does not exceed 240 characters, then the OU will not be truncated.

Using -WRAP or -AUTOSIZE causes other issues:

Wrap will make the report very hard to read, and autosize still operates as if the screen width is 120 chacters and truncates the report.
0
 
LVL 1

Author Closing Comment

by:gerhardub
ID: 39705721
This solution prevents -wrap and it's readability issues, and the fact the -autosize only works within the confines of the current shell environment (which is 120 characters when running as a Windows timer job).
0

Featured Post

Introducing the WatchGuard 420 Access Point

WatchGuard's newest access point includes an 802.11ac Wave 2 chipset, providing the fastest speeds for VoIP, video and music streaming, and large data file transfers. Additionally, enjoy the benefits of strong security as the 3rd radio delivers dedicated WIPS protection!

Question has a verified solution.

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

Had a business requirement to store the mobile number in an environmental variable. This is just a quick article on how this was done.
This process allows computer passwords to be managed and secured without using LAPS. This is an improvement on an existing process, enhanced to store password encrypted, instead of clear-text files within SQL
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

719 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