jonathanbyers
asked on
Powershell Output Without Format-Table
I want to change from using Format-Table to using Select-Object since Format-Table is causing a problem when connecting to a PowerShell v3 server from a PowerShell v2 server. I've tried changing it but it does not format the same way. I've attached an image of the output I am getting with Format-Table and I want to get the same thing with Select-Object or any other way.
# Core: Enable-PSRemoting
# Agent: Set-ExecutionPolicy Unrestricted -Force
#.\AppAssure-v5-Backup.ps1 -cmdCore AO-FS-02 -cmdAgent AO-AD-01
param ([string] $cmdCore, [string] $cmdAgent)
$Session = New-PSSession $cmdCore
Invoke-Command -Session $Session {
param ($cmdCore, $cmdAgent)
Import-Module AppAssurePowerShellModule
Get-CompletedJobs -JobType Transfer -Number l24 -ProtectedServer $cmdAgent | Where {($_.StartTime).AddHours(-8) -gt (Get-Date).AddHours(-24)} |
Format-Table -HideTableHeaders `
@{label="Status"; Width=12; e={$_.Status}},
@{label="Snapshot Time"; Width=25; e={$_.StartTime.AddHours(-8)}}
} -ArgumentList $cmdCore, $cmdAgent | Tee-Object -file C:\temp\AppAssure_Backup.txt
Remove-PSSession $session
$path = "C:\temp\AppAssure_Backup.txt"
If(-not(Test-Path -path $path))
{
Write-Host "Backup Failed: Backup log does not exist."
exit 2001
}
Else
{
if ((Get-Content C:\temp\AppAssure_Backup.txt) -match "Succeeded")
{
#Exit Code 0
Write-Host " | Backup Successful"
#$host.SetShouldExit(0)
exit 0
}
else
{
#Exit Code 2001
Write-Host " | Backup Failed"
#$host.SetShouldExit(2001)
exit 2001
}
}
ASKER
There appears to be an issue when using Format-Table when connecting to a system running PowerShell v3 from a system running PowerShell v2. I have some Windows 2003 server (which can't run v3) that I need to execute this script from. I'm having issues and according to http://social.technet.micr osoft.com/ Forums/is/ winserverp owershell/ thread/875 70f5e-3c71 -4efc-a7fc -75677c32f 664 it is because of the version mismatch.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Ah, I kept reading just take out the FT, but needed the formatting. I tested the code below and all works. I left the labels since I want them there, but need a line return due to how our monitoring software interprets the output of the script. I'll probably just end up removing them for now.
Invoke-Command -Session $Session {
param ($cmdCore, $cmdAgent)
Import-Module AppAssurePowerShellModule
Get-CompletedJobs -JobType Transfer -Number l1 -ProtectedServer $cmdAgent | Where {($_.StartTime).AddHours(-8) -gt (Get-Date).AddHours(-24)}
} -ArgumentList $cmdCore, $cmdAgent |
Format-Table -HideTableHeaders `
@{label="Status"; Width=12; e={$_.Status}},
@{label="Snapshot Time"; Width=25; e={$_.StartTime.AddHours(-8)}} | Tee-Object -file C:\temp\AppAssure_Backup.txt
Remove-PSSession $session
ASKER
Excellent solution, thanks!
http://blogs.msdn.com/b/powershell/archive/2006/04/30/how-powershell-formatting-and-outputting-really-works.aspx
and
http://www.windowsitpro.com/blog/powershell-with-a-purpose-blog-36/scripting-languages/how-the-powershell-formatting-subsystem-works-137397
I'm not sure I can suggest a work-around for you. What problem are you seeing when using Format-Table?