How to filter info more efficiently using Where-Object -filterscript

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of I Am Sir Ask Alot I Am Sir Ask Alot 2 years, 3 months ago.

  • Author
    Posts
  • #24376
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    I am using a script that gets installed software and I want it to get Visio and Project installs.

    Here is what I have that works well, but is there another method I can use, perhaps regex, or something that doesn't require so many lines of code?

    # Step 1: Declare path variables
    
    $Path = "\MyDFSRoot\Share\Project_Visio_Installs"
    
    $FileName = "$env:COMPUTERNAME-Project_Visio_Installs.log"
    
    
    # Step 2: Create the filter to retrieve the desired installed applications
    
    Get-InstalledApp | 
            Where-Object -FilterScript { ($_.AppID -match "{") -and ($_.AppName -match "Project") -or ($_.AppName -match "Visio") -and ($_.AppName -notmatch "MUI") -and ($_.AppName -notmatch "Service Pack") -and ($_.AppName -notmatch "Update") -and ($_.AppName -notmatch "Viewer") } |
                Select-Object -Property * -OutVariable OutPut | 
                    Out-Null
                    
                    
    # Step 3: Create the directory if the OutPut variable is not null
    
    If ($OutPut -ne $null)
    {
        If (!(Test-Path -Path $Path))
        {
            New-Item -Path $Path -ItemType Directory | Out-Null
        }
    
        If (Test-Path -Path "$Path\$FileName")
        {
            Exit
        }
    }
    
    
    # Step 4: Create a variable to use to help limit output duplications that were not caught in the first filter in Step 3 
    
    $OutPut1 = @(
        If ($OutPut -eq $null)
        {
            Exit
        })
    
    $OutPut2 = @(
        If ($OutPut.Count -eq '1')
        {
            $OutPut
        })
        
    $OutPut3 = @(
        If ($OutPut.Count -ge '2')
        {
            $CheckProjDup = $OutPut |  
                Where-Object -FilterScript { $_.AppName -match "Project" }
    
            $CheckVisDup = $OutPut | 
                Where-Object -FilterScript { $_.AppName -match "Visio" }
    
            If ($CheckProjDup.Count -gt '1')
            {
                $CheckProjDup | 
                    Where-Object -FilterScript { ($_.AppID -match "{") }
    
                $CheckVisDup
            }
    
            ElseIf ($CheckVisDup.Count -gt '1')
            {
                $CheckProjDup
                
                $CheckVisDup | 
                    Where-Object -FilterScript { ($_.AppID -match "{") } 
            }
        })
    
    
    #Step 5: Create the logic to evaluate if the check duplicates variable has a null result, if so, exit, else output the results to the log file
    
    $OutPut1
    
    $EmailRecipients = "AssetManagementDL@mydomain.com"
    
    
    If ($OutPut2) 
    {
        $OutPut2 | Out-File -FilePath "$Path\$FileName"
        
        $MailBody = $OutPut2 | Out-String
        
        Send-MailMessage -To $EmailRecipients -From "Test Admin" -Subject "Microsoft Project and Visio Installs Alert" -Body $MailBody -SmtpServer 'test.domain.com'
    }
    
    If ($OutPut3)
    {
        $OutPut3 | Out-File -FilePath "$Path\$FileName"
        
        $MailBody = $OutPut3 | Out-String
        
        Send-MailMessage -To $EmailRecipients -From "ITDepartment@mydomain.com" -Subject "Microsoft Project and Visio Installs Alert" -Body $MailBody -SmtpServer 'smtp.mydomain.com'
    }{content}
  • #24386
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Yep, regex has an "or" operator build-in, so you could combine some of your expressions like this:

    ($_.AppID -match "{") -and ($_.AppName -match "Project|Visio") -and ($_.AppName -notmatch "MUI|Service Pack|Update|Viewer"))
    
  • #24415
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    Oh that's cool. I will use that and see how it works for me and get back to you.

    Thanks Dave

  • #24421
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    That did it for me, Dave.

    Thanks again for your help, much appreciated.

You must be logged in to reply to this topic.