How to add a column count...

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of Matt Bloomfield Matt Bloomfield 7 months, 2 weeks ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #35313
    Profile photo of Graham Beer
    Graham Beer
    Participant

    Hi, I have written this script to look at deployment status for SCCM.

    All works fine, but would like to add a column at the start to count the items as it runs down.
    i.e.

    Count – App – Status
    1 office Success
    2 office Success
    3 office Success

    Function Get-DeploymentStatus {      
            
        [CmdletBinding()]
        
        Param
        (
            #Set application name
            [Parameter(Mandatory=$true,
                       ValueFromPipelineByPropertyName=$true,
                       Position=0)]
            [string]$AppName,
            [string]$Collection,
    
            [ValidateSet("Success", "InProgress", "RequirementsNotMet","Unknown","error")]
            [String]$Status 
        )
    
    Begin {
    
    $query = @{
        Namespace = 'root\SMS\site_AAC'
        ClassName = 'SMS_AppDeploymentAssetDetails'
        Filter = "AppName like '$AppName' and CollectionName like '$Collection'"
    
    }
    
    
    $AppStatusTypeTable = DATA {ConvertFrom-StringData @'
        1 = Success
        2 = InProgress
        3 = RequirementsNotMet
        4 = Unknown
        5 = Error
    '@}
    
    }#End Begin
    
    process {
    
    Get-CimInstance @query | 
            Select-Object AppName,Machinename,
            @{Name="StatusOfApplication";Expression={$AppStatusTypeTable["$($PSItem.AppStatusType)"]}} |
            where {$PSItem.StatusOfApplication -eq "$status"} 
    
      
    }#End Process   
    
    }#End of Function
    

    Any help would be great, banging my head against a brick wall !

    #35339
    Profile photo of Matt Bloomfield
    Matt Bloomfield
    Participant

    If I've understood what you're after, I think this will work.

    Add $count = 0 to the Begin block.

    Add $count++ to the Process block.

    Add @{Name="Count";Expression={$count}} to your Select-Object

    #35352
    Profile photo of Graham Beer
    Graham Beer
    Participant

    Hi Matt,

    I've tried that, but doesn't count up...

    Function Get-DeploymentStatus {      
            
        [CmdletBinding()]
        
        Param
        (
            #Set application name
            [Parameter(Mandatory=$true,
                       ValueFromPipelineByPropertyName=$true,
                       Position=0)]
            [string]$AppName,
            [string]$Collection,
    
            [ValidateSet("Success", "InProgress", "RequirementsNotMet","Unknown","error")]
            [String]$Status 
        )
    
    Begin {
    
    $query = @{
        Namespace = 'root\SMS\site_AAC'
        ClassName = 'SMS_AppDeploymentAssetDetails'
        Filter = "AppName like '$AppName' and CollectionName like '$Collection'"
        }
    
    $count = 0 #Set counter for 'count' column in select statement
    
    
    $AppStatusTypeTable = DATA {ConvertFrom-StringData @'
        1 = Success
        2 = InProgress
        3 = RequirementsNotMet
        4 = Unknown
        5 = Error
    '@}
    
    }#End Begin
    
    process {
    
    $count++ #post-incrementation
    
    Get-CimInstance @query | 
            Select-Object  @{Name="Count";Expression={$count}},
            AppName,Machinename,
            @{Name="StatusOfApplication";Expression={$AppStatusTypeTable["$($PSItem.AppStatusType)"]}} |
            #filter out "status"
            where {$PSItem.StatusOfApplication -eq "$status"} 
    
      
    }#End Process   
    
    }#End of Function
    

    This is what the result looks like...

     Count AppName                                                   Machinename                                               StatusOfApplication                                      
                                                        ----- -------                                                   -----------                                               -------------------                                      
                                                            1 "PackageName"                  Machine01                                              Success                                                  
                                                            1 "PackageName"                  Machine-02                                              Success                                                  
                                                            1 "PackageName"                  Machine-03                                              Success                                                  
                                                            1 "PackageName"                  Machine-04                                              Success                                                  
                                                            1 "PackageName"                  Machine-05                                              Success           
    

    Do i need to "count" on something ?

    #35353
    Profile photo of Graham Beer
    Graham Beer
    Participant

    Cracked it...

    needed to set a "scope" once $Script: was put in front of the count it worked.

    Function Get-DeploymentStatus {      
            
        [CmdletBinding()]
        
        Param
        (
            #Set application name
            [Parameter(Mandatory=$true,
                       ValueFromPipelineByPropertyName=$true,
                       Position=0)]
            [string]$AppName,
            [string]$Collection,
    
            [ValidateSet("Success", "InProgress", "RequirementsNotMet","Unknown","error")]
            [String]$Status 
        )
    
    Begin {
    
    #Set query for 'Get-Ciminstance' search
    $query = @{
        Namespace = 'root\SMS\site_AAC'
        ClassName = 'SMS_AppDeploymentAssetDetails'
        Filter = "AppName like '$AppName' and CollectionName like '$Collection'"
        }
    
    #Set counter for 'count' column in select statement
    $script:count = 1 
    
    
    $AppStatusTypeTable = DATA {ConvertFrom-StringData @'
        1 = Success
        2 = InProgress
        3 = RequirementsNotMet
        4 = Unknown
        5 = Error
    '@}
    
    }#End Begin
    
    process {
    
    #Run get-ciminstance with query
    Get-CimInstance @query | 
            Select-Object @{Name="Count";Expression={$Script:count;$Script:count++}},
            AppName,Machinename,
            @{Name="StatusOfApplication";Expression={$AppStatusTypeTable["$($PSItem.AppStatusType)"]}} |
            #filter out "status"
            where {$PSItem.StatusOfApplication -eq "$status"}      
    
      
    }#End Process   
    
    }#End of Function
    

    Thanks for your help Matt.

    I've add a "where" statement to filter on desired result (i.e. Success), is there a better way to do this ?

    #35367
    Profile photo of Matt Bloomfield
    Matt Bloomfield
    Participant

    That's interesting. I thought the PROCESS block would increment the counter without any fiddling.

    I'm not sure if there's a better way. I don't have SCCM at home so I can't test. I did wonder if you could move it to your query by adding AppStatusType=1 to your filter.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.