Custom Format Views

Welcome Forums General PowerShell Q&A Custom Format Views

This topic contains 9 replies, has 4 voices, and was last updated by

 
Participant
3 months ago.

  • Author
    Posts
  • #111340

    Participant
    Points: 0
    Rank: Member

    HI,

    I created a custom formatted view as below:

    $currentdate = get-date

    $currentdate = $currentdate.ToString('MM-dd-yyyy_hh-mm-ss')

    update-formatdata -prependpath C:\scripts\datest.format.ps1xml

    get-da8stats -computername localhost

    When I run the above function, I get the desired column header output mentioned in datest.format.ps1xml. But, when I export this to csv, the column headers are different again.

    get-da8stats -computername localhost | export-csv -path "C:\test_$currentdate.csv"

    Pics at the link: https://drive.google.com/open?id=1UFFRVqGyB361Pg8PR_CnrSkTFTYCQ3Dv

     

    Thanks in advance.

  • #111356
    js

    Participant
    Points: 325
    Helping Hand
    Rank: Contributor

    You probably want to use select-object, not update-formatdata. Those format files are only for the screen.

  • #111389

    Participant
    Points: 884
    Helping Hand
    Rank: Major Contributor

    Yes, and all formatting cmdlets are only for printing the output in console, it shouldn't be used between pipelines but at the end of the pipeline, using it between the pipeline will convert the objects to text which is useful only in console.

  • #111511

    Participant
    Points: 0
    Rank: Member

    This is the code I am using to generate Csv file which contains Columns ComputerName, Memory %, CPU %, RAM(GB), No.Of Processors, Connections in order. I am getting the same when I run in powershell.exe, but format changes when I export to CSV. Can someone modify this and help me. Thank you.

    function Get-DA8Stats

    {

    [cmdletbinding()]

    param

    (

    [parameter(mandatory=$True, ValueFromPipeline=$true, HelpMessage="Computer name or IP address")]

    [ValidateCount(1,40)]

    [ValidateNotNullOrEmpty()]

    [string[]]$ComputerName

     

     

    )

    begin

    {}

    process

    {

    foreach ($computer in $ComputerName)

    {

    $memorysize = [math]::round((Get-WmiObject -ClassName Win32_OperatingSystem -ComputerName $computer).TotalVisibleMemorySize / 1mb)

    $numberofprocessors = Get-WmiObject -ClassName Win32_computersystem -ComputerName $computer | select-object NumberOfLogicalProcessors

    $daconnections = Get-RemoteAccessConnectionStatisticsSummary -ComputerName $computer | Select-Object totaldaconnections

    $proc = Get-WmiObject -computername $computer win32_processor | Measure-Object -property LoadPercentage -Average | Select-Object Average

    $mem = gwmi -Class win32_operatingsystem -computername $computer | Select-Object @{Name = "MemoryUsage"; Expression = {“{0:N2}” -f ((($_.TotalVisibleMemorySize – $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize) }}

    $paras = @{"RAM(GB)" = $memorysize;

    "No.Of Processors" = $numberofprocessors.numberoflogicalprocessors;

    "ComputerName" = $computer;

    "Connections" = $daconnections.totaldaconnections;

    "Memory %" = $mem.memoryusage;

    "CPU %" = $proc.average

    }

    $obj = New-Object -TypeName psobject -Property $paras

    $obj.psobject.typenames.insert(0, "DA.SystemInfo")

    Write-Output $obj

    }

    }

    end {}

     

    }

     

    $CurrentDate = Get-Date

    $CurrentDate = $CurrentDate.ToString('MM-dd-yyyy_hh-mm-ss')

    Update-FormatData -PrependPath C:\scripts\DATest.format.ps1xml

    Get-DA8Stats -ComputerName (Get-Content -Path C:\test\servers.txt) | Export-Csv -NoTypeInformation -Path "C:\DA8Data_$CurrentDate.csv"

  • #111526

    Participant
    Points: 884
    Helping Hand
    Rank: Major Contributor

    I request you to update the above post by formating the code properly which makes others to easily understand your code, below link will help you.

    • #111529

      Participant
      Points: 0
      Rank: Member

      Hi KV, thanks for the reply.

      Let me know if above link is ok. Apologies for inconvenience as I am new to this. Thank you.

  • #111539

    Participant
    Points: 292
    Helping Hand
    Rank: Contributor

    Format.ps1xml files are purely for display formatting. The objects in question still retain their original properties, which are what is exported to CSV.

    If you want to alter those, I'd recommend using Select-Object, Add-Member, or just building your own custom object with the properties you need. If I understand correctly, the issue is the ordering of the columns? This is an issue because hashtables do not store the order of properties. In PS v3 and up, you can use [ordered] to force the hashtable to be an ordered collection type instead, but there's a simpler way:

    $Obj = [PSCustomObject]@{
        "RAM(GB)"          = $memorysize
        "No.Of Processors" = $numberofprocessors.numberoflogicalprocessors
        "ComputerName"     = $computer
        "Connections"      = $daconnections.totaldaconnections
        "Memory %"         = $mem.memoryusage
        "CPU %"            = $proc.average
    }
    $Obj | Export-Csv -Path $FilePath
    
    • #111551

      Participant
      Points: 0
      Rank: Member

      Thanks Joel, that worked 🙂

The topic ‘Custom Format Views’ is closed to new replies.