Need help in PS Code “Output Format not Coming in Table Format if I use -ErrorVariable Errs”

Hi Guys,

I am writing a PS Function to get the status of a service and a file version from multiple computers. Also I am trying to Print the Error using (-ErrorVariable Errs). When I use (-ErrorVariable Errs) then i output is not coming in Table Format. without ‘Error’=$Errs;, output is coming in Table Format

function Get-EncaseInfo{
[CmdletBinding()]
param (
[Parameter(Mandatory=$True)]
[string[]]$ComputerName
)
begin {
$ErrorView = "CategoryInfo"
}
process {
foreach($Computer in $ComputerName){
$Service=Get-Service -Name enstart64 -ComputerName $Computer -ErrorAction SilentlyContinue -ErrorVariable Errs
$EnVer = Get-ChildItem C:\Windows\system32\enstart64.exe
#($EnVer.VersionInfo).FileVersion

$Output=[ordered]@{
'ComputerName'=$Computer;
'Service Status'=$Service.Status;
'Service StartType'=$Service.StartType;
'Encase Version'=($EnVer.VersionInfo).FileVersion;
'Error'=$Errs;
}
#This doesn't produce objects yet - just for testing
$obj=New-Object -TypeName PSObject -property $Output
Write-output $obj
}
}
end {
}
}

Basically I want the output in Table Format instead of List. If I comment “‘Error’=$Errs;” in the Code then the output is showing in Table Format.

Ishant,
Welcome to the forum. :wave:t4:

Before we proceed … please go back, edit your question again and fix the formatting of your code.

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

Next … if one of your posts is set on hold please do not repost. Give us a little time to check and release.

Thanks

i was looking for exactly the same option. thanks for guiding me.

For most cmdlets the default output up to 4 properties is formatted as table. If it’s more than 4 it’s formatted as list. If you want to force the output to be formatted as table you simply pipe the output to

Please rad the help completely including the examples to leanr how to use it.

This should be enough:

function Get-EncaseInfo {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $True)]
        [string[]]$ComputerName
    )
    process {
        foreach ($Computer in $ComputerName) {
            $Service = Get-Service -Name enstart64 -ComputerName $Computer -ErrorAction SilentlyContinue -ErrorVariable Errs
            $EnVer = Get-ChildItem C:\Windows\system32\enstart64.exe
    
            [PSCustomObject]@{
                'ComputerName'      = $Computer
                'Service Status'    = $Service.Status
                'Service StartType' = $Service.StartType
                'Encase Version'    = ($EnVer.VersionInfo).FileVersion
                'Error'             = $Errs
            }
        }
    }
}

And you’d use it like this:

Get-EncaseInfo -ComputerName 'Computer_01' | Format-Table
1 Like

Thank you @Olaf I am aware of Format-Table but I was trying to use FT within the Function like end of $output OR end of $obj but it was giving me wierd output.

never thought of using FT at the end of command while running the fuction.

Another Question: I know PS cmdlets and bit of formatting, I can also find the scripts and modify bit of to reach a small goal. but I want to improve my skills on PowerShell e.g. using Workflows, try catch, Param etc.I watched Jeff Snover’s [Getting Started with PowerShell 3.0] and [ Advanced Tools & Scripting with PowerShell 3.0 Jump Start] but while writing script I am getting stuck. Can you suggest any way to start wriring small code e.g. Check Ping Status if Ping works then check Service Status and Check uptime, check a file version etc. then get the output. also if the Capture the error on each stage e.g. Ping Failure, Server does not exist or any other error.

Note: I dont want you to write a script for me instead I would appreciate if you could guide me how can i learn writing a script like this.

You should never do that. This will break the pipeline and turn your rich and powerful objects into stupid boring text/strings.

That’s an excelent foundation to built a robust skill set on. Great. :+1:t4:

In the vast majority of the cases you’re not the very first one with a given task. And most of the times you can find code examples online you can adapt to your particular needs. So no need to re-invent the wheel again. Steal from all sources you can find and pick the best for your purpose. There’s no shame in using great code from others if they give it for free anyway. :wink:

Besides this I’d say practice, practice, practice. :man_shrugging:t4: