none
Formatting one item in a table output RRS feed

  • Question

  • First, this is a rather lengthy dialog.
    I have the following script which I created to get the last hotfix applied from each server in a collection:

    #Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
    
    #Load Configuration Manager PowerShell Module
    Import-module ($Env:SMS_ADMIN_UI_PATH.Substring(0,$Env:SMS_ADMIN_UI_PATH.Length-5) + '\ConfigurationManager.psd1')
    #$location = Get-Location
    
    #Get SiteCode
    $SiteCode = Get-PSDrive -PSProvider CMSITE
    Set-location $SiteCode":"
    
    #$emailAddress = ("name@domain.com")
    
    #Parameters
    $file = "c:\temp\list.txt"
    $file2 = "c:\temp\list2.txt"
    $file3 = "c:\temp\NoClient.txt"
    $file4 = "c:\temp\Result.txt"
    $file5 = "c:\temp\ComplianceCheck.txt"
    
    Remove-Item ($file, $file2, $file3, $file4, $file5)
    
    Get-CMDevice -CollectionName "CollectionName" | Where-Object {$_.ClientType -eq $null} |Select-object Name |sort name |out-file $file3 -Force -Encoding utf8
    Get-CMCollectionMember -CollectionName "CollectionName" | Where-Object {$_.ClientType -ne $null} | select-object name |sort Name |out-file $file -Encoding utf8
    $content = Get-Content $file3
    $content | Foreach {$_.TrimEnd()} | Set-Content $file3
    
    get-content $file3 | ? {$_.trim() -ne ""} |
        select -Skip 2 |
        set-content $file2
    move $file2 $file3 -Force
    
    $content = Get-Content $file
    $content | Foreach {$_.TrimEnd()} | Set-Content $file
    
    get-content $file | ? {$_.trim() -ne ""} |
        select -Skip 2 |
        set-content $file2
    move $file2 $file -Force
    
    
    #$file = "c:\scripts\list.txt"
    GC $file | foreach {(Get-HotFix -Cn $_ |sort installedon -desc) | Select-Object -First 1} | format-table -autosize |out-file $file4 -Encoding utf8
        Add-Content $file4 -Value ' '
        Add-Content $file4 -Value 'Non-Client Systems'
        Add-Content $file4 -Value '---------------------'
    
    
    gc $file4 |out-file $file5
    gc $file3 |Out-File $file5 -Append
    
    #add-content $file5 
    Add-Content $file5 -Value ' '
    Add-Content $file5 -Value (Get-Date) #| Out-File $file5
    #$env:USERNAME |Out-File c:\temp\user.txt
    gc $file5
    
    #Write-Host $file5
    
    
    #Send-MailMessage -To $emailAddress -From Patch_SpotCheck@domain.com -Subject "Patching_Compliance_SpotCheck" -Body "See attachment for Patching Compliance report" -Attachments c:\temp\ComplianceCheck.txt -SmtpServer xxx.xxx.xxx.xxx
    

    Ouput is as below:

    Source         Description     HotFixID  InstalledBy                  InstalledOn           
    ------         -----------     --------  -----------                  -----------           
    BRNDC28        Security Update KB4487000 NT AUTHORITY\SYSTEM          2/26/2019 12:00:00 AM 
    BRNDC29        Security Update KB4487000 NT AUTHORITY\SYSTEM          2/26/2019 12:00:00 AM 
    BRN-SCCM-02    Security Update KB4487000 NT AUTHORITY\SYSTEM          2/21/2019 12:00:00 AM 
    BRN-SCVMM-02   Update          KB4072650 NT AUTHORITY\SYSTEM          3/26/2019 12:00:00 AM 
    DEE1           Update          KB4486553 NT AUTHORITY\SYSTEM          3/26/2019 12:00:00 AM 
    HOME-HV        Update          KB4483453 NT AUTHORITY\SYSTEM          3/16/2019 12:00:00 AM 
    HOME-HV-03     Update          KB4483450 NT AUTHORITY\SYSTEM          3/11/2019 12:00:00 AM 
    MIKE-PC        Update          KB4486553 NT AUTHORITY\SYSTEM          3/18/2019 12:00:00 AM 
    MIKESTABLET    Update          KB4486553 NT AUTHORITY\SYSTEM          3/31/2019 12:00:00 AM 
    PLEX-PC        Update          KB4457034 NT AUTHORITY\SYSTEM          10/1/2018 12:00:00 AM 
    SQLCL01        Update          KB4072650 NT AUTHORITY\SYSTEM          3/29/2019 12:00:00 AM 
    SQLCL02        Update          KB4072650 NT AUTHORITY\SYSTEM          3/30/2019 12:00:00 AM 
    SVR-2008R2-TST Update          KB976902  SVR-2008R2-TST\Administrator 11/21/2010 12:00:00 AM
    
    Non-Client Systems
    ---------------------
    BRNDC30
    BRN-PLEX-01
    BRN-SCCM-03
    BRN-SRV-19
    BRN-WSUS02
    CMSQLCL
    ConfigMgrCL
    MININT-52DR5P3
    NAS
    SVR-2003-TST
     
    4/1/2019 1:11:42 PM

    What I would like is to format just the date (InstalldOn) as yyyyDDmm. I know how to format it, but I can't seem to just tell the script to format the date. It will only report the date if I tell it to format the date. If I use Select-Object and name all the elements I wish to see, the Source column returns null values. Looking for some assistance in formatting AND keeping all of the rest of the items in my output example.


    Mike Brown

    Monday, April 1, 2019 6:29 PM

Answers

  • Here is a simple example:

    Get-HotFix  |
         sort installedon -desc | 
         Select-Object @{n='InstalledOn'; e={$_.InstalledOn.ToString('yyyy-MM-dd')}} -First 1


    \_(ツ)_/


    • Edited by jrv Monday, April 1, 2019 7:27 PM
    • Marked as answer by Mike.Brown Monday, April 1, 2019 7:48 PM
    Monday, April 1, 2019 7:26 PM

All replies

  • $date.ToString('yyyy-MM-dd')

    You can specify any format you want.


    \_(ツ)_/

    Monday, April 1, 2019 7:23 PM
  • Here is a simple example:

    Get-HotFix  |
         sort installedon -desc | 
         Select-Object @{n='InstalledOn'; e={$_.InstalledOn.ToString('yyyy-MM-dd')}} -First 1


    \_(ツ)_/


    • Edited by jrv Monday, April 1, 2019 7:27 PM
    • Marked as answer by Mike.Brown Monday, April 1, 2019 7:48 PM
    Monday, April 1, 2019 7:26 PM