Post_Deployment_Check.ps1

#==========================================================================================================================#
# Author - Markus Schanche #
# Title - Trainee #
# Name - Post_Deployment_Check.ps1 #
# Version - 1.0.4 #
# #
# Original Script by Thomas Waaler, Trainee Avinor AS #
# #
#==========================================================================================================================#

function Run-PostDeploymentCheck {
  
    # Enables parameters for running the function.
    
        param
    (
        
        [Parameter(Mandatory)]
        [string]
        $Computername,

        [Switch]
        $GUI,

        [string]
        $OutPath = "$env:temp\Post_deploymentlog.csv"

    )

    if($computername -like "*-a01-*"){
    $computers = $computername
    }
    else{
    $computers = get-content $Computername
    }

    # Sets up variables for the progress bar

    $computercount = $Computers
    $ferdige = 0
    $antall = $computercount.count

    # Clears the log file if it exists from before

    Clear-Content -Path $OutPath -ErrorAction SilentlyContinue -Force

    # Starts the loop of getting all the information from all PC's in the list

    if($computername -like "*-a01-*"){
    $computers = $computername
    }
    elseif(Test-path $Computername)
    {
    $computers = get-content $Computername
    }
    else{
    Write-error "Cant find Client name"
    Break
    }
    foreach ($Computer in $Computers) {
        
        # Writes the progress bar and updates it for every loop

        $ferdige += 1
        $avnr = ([string]$ferdige + ' of ' + [string]$antall)
        Write-Progress -Activity "Checking clients" -status "Checking Client $Computer. ($avnr)" ` -percentComplete ($ferdige / $antall*100)

        # Pings the client and saves time by continuing to the next item if it cant ping

        if(Test-Connection $Computer -Count 2 -Quiet -ErrorAction SilentlyContinue){
            $Connection = 'True'

            # Retrieves the OSDImage ID

            [string]$OSDID = invoke-command -ComputerName $computer -ScriptBlock { gwmi avinor_osd_info | select -Expand OSDImagePackageID }

            # Retrieves the CostCode

            [string]$CostCode = invoke-command -computername $computer -ScriptBlock { gwmi avinor_client_info | select -Expand CostCode }

            # Empties SCCM Cache

            Try{
                Invoke-Command -ComputerName $Computer -ScriptBlock {
                    # Deletes Cache Elements
                    $ResMgr = New-Object -ComObject "UIResource.UIResourceMgr"
                    $CacheInfo = $ResMgr.GetCacheInfo()
                    $CacheInfo.GetCacheElements() | Foreach {
                        $CacheInfo.DeleteCacheElement($_.CacheElementID)
                    }
                }
                $CacheStatus = 'Emptied'
            }
            Catch{
                $CacheStatus = 'Not Emptied'
            }

            # Checks if Symantec Antivirus is updated
        
            $PathPart1 = "\\$Computer\C$\ProgramData\Symantec\Symantec Endpoint Protection\"
            $Child1 = Get-ChildItem "$PathPart1" | select name
            $PathPart2 = $($Child1.name[0])                                       
            $PathPart3 = $PathPart1 + $PathPart2
            $PathPart4 = $PathPart3 + "\Data\Definitions\SDSDefs"        
 
 
            $Child = Get-childitem -Path $PathPart4 | Where-Object{$_.Name -like "20*"} # Only lists items starting with 20*, must be changed in 2100!
            [string]$year = $child.lastwritetime.Year
            [string]$month = $child.lastwritetime.Month
            [string]$day = $child.lastwritetime.Day

            if($day.length -le 1){
                if($month.Length -le 1){
                    $Updated = ("$year" + "0" + "$Month" + "0" + "$day")
                }
                else{
                    $Updated =  ("$year" + "$month" + "0" + "$day")
                }
            }
            else
            {
                if($month.Length -le 1){
                    $Updated = ("$year" + "0" + "$Month" + "$day")
                }
                else{
                    $Updated =  ("$day" + "$month" + "$year")
                }
            }


            # Determines how many days since last update

            $datetoday = Get-Date -format "yyyyMMdd"

            $a=[datetime]::ParseExact("$updated", "yyyymmdd", $null)
            $b=[datetime]::ParseExact("$datetoday", "yyyymmdd", $null)

            $virusage = New-TimeSpan –Start $a –End $b

            # Determines if the Antivirus is up-to-date

            if($virusage -le 1){
            $virusstatus = 'OK'
            }
            else{
            $virusstatus = 'BAD'
            }

            if($computer -like "GM-A01-W*"){
            $Drivestatus = 'Desktop'
            }
            else{
                $bitlocker = manage-bde -Computername $computer -status c:
                if (Select-String -InputObject $bitlocker -Pattern 'Protection On' -SimpleMatch){
                $Drivestatus = 'True'
                }
                else{
                $Drivestatus = 'False'
                }
            }
        }

        # If ping fails

        Else{ 
            
            # Sets connection to false and writes a CSV row
            
            $Connection = 'False'

            $Object = New-Object PSObject -Property ([Ordered]@{
                "Computername" = $Computer
                "Connection" = $Connection
                "OSD_info" = ""
                "CostCode" = ""
                "SCCM-Cache" = ""
                "Antivirus status" = ""
                "Virus definisjon alder" = ""
                "CDisk kryptering status" = ""
            })
            $Object | Export-Csv -Path $OutPath -Append -Delimiter ","
            Continue
            }

        # Writes everything down to the CSV log

        $Object = New-Object PSObject -Property ([Ordered]@{
            "Computername" = $Computer
            "Connection" = $Connection
            "OSD_info" = $OSDID
            "CostCode" = $CostCode
            "SCCM-Cache" = $CacheStatus
            "Antivirus status" = $virusstatus
            "Virus definisjon alder" = ([string]$virusage.days + ' Days')
            "CDisk kryptering status" = $Drivestatus
        })
        $Object | Export-Csv -Path $OutPath -Append -Delimiter ","
    }
    
    # Opens the CSV file in a grid view if you chose the -gui parameter

    if ($GUI){ 
        Import-Csv -Path $OutPath | Out-GridView 
    }
    else{
        $GridSwitch = Read-Host -prompt "Sjekk er loggført til $OutPath. Ønsker du å åpne en oversikt? (Y/N)"
        $GridStatus = 'False'
        do{

            # Gives you the choice to open GUI even if you didnt use the parameter

            Switch ($GridSwitch){

                Y{
                    Import-Csv -Path $OutPath | Out-GridView
                    $GridStatus = 'True'
                }

                N{
                    $GridStatus = 'True'
                }

                Default{Write-Host 'Error: Choose an alternative...'}
            }
        }while($GridStatus -like 'False')
    }
}