classes/JWNewJob.Class.ps1

class JWNewJob {
    [string]$Batch = [guid]::NewGuid()
    [datetime]$StartTime = (Get-Date)
    $FilePath 
    $JobInfo
    [string]$State = [string]::Empty
    [string]$Name = [string]::Empty
    [string]$Type = [string]::Empty
    [string]$Count = [string]::Empty
    [string]$View = "Generic"
    [int]$throttle = 20
    $resultlist  #works


    JWNewJob ([string]$FilePath, $inputlist) {

        Try {
            $this.State = "Running"
            $ScriptInfo = Test-ScriptFileInfo $FilePath -ErrorAction Stop
            $this.Name = $ScriptInfo.Name
            $this.FilePath = $scriptinfo.path
            $this.type = $ScriptInfo.Tags
            $scriptblock = [scriptblock]::Create($scriptinfo.path)
            #$scriptblock = [scriptblock]::Create([System.IO.File]::ReadAllText($(Resolve-Path -LiteralPath $this.FilePath).ProviderPath))

            switch -wildcard ($ScriptInfo.Tags) {
                "PSRSJob" {
                    Write-Verbose "Creating RSJOB"
                
                    $RSJOBStarter = @{
                        Name              = $this.Name 
                        Scriptblock       = $scriptblock 
                        Batch             = $this.Batch
                        InputObject       = $inputlist
                        VariablesToImport = "SyncTable"
                        Throttle          = $this.throttle
                    }
            
                    $this.JobInfo = Start-RSJob @RSJOBStarter -Verbose
                }

                "PSRemote" {
                    Write-Verbose "Creating PSRemoteJob"
                    

                    import-module CredentialManager
                    $admcreds = Get-StoredCredential -Target admcreds
                    $inputlist | ForEach-Object {
                        Write-Verbose "PSRemote - $($_.ComputerName)"
                        $this.JobInfo = invoke-command -computername $_.ComputerName -Credential $admcreds -scriptblock $scriptblock -AsJob -JobName $this.Batch -ArgumentList $_ -ThrottleLimit $this.throttle
                       
                    }
                    
                }

                "PSLocal" {
                    Write-Verbose "Creating PSLocalJob"
                    $this.type = 'PSLocal'
                    $inputlist | ForEach-Object {
                        #Write-Verbose "Starting $_"
                        $this.State = "Running"
                        $this.JobInfo = Start-ThreadJob -Name $this.Batch -scriptblock $scriptblock -ThrottleLimit $this.throttle  -ArgumentList $_
                    
                    }
                }

                "List" {
               
                    $RSJOBStarter = @{
                        Name              = $this.Name #.Substring(0,$this.FileName.LastIndexOf('.'))
                        Scriptblock       = $scriptblock
                        Batch             = $this.Batch
                        InputObject       = 1
                        VariablesToImport = "SyncTable"
                        Throttle          = $this.throttle
                    }

                    $this.JobInfo = Start-RSJob @RSJOBStarter -Verbose
                
                }
            }
        }
        Catch {}

    }

    [Object] Status() {
        If ($this.Batch -ne "" -and $this.State -match "Running") {

            switch -wildcard ($this.Type) {

                { $_ -eq "RSJob" -or $_ -eq "List" } {
                    #
            
                    IF ((Get-RSJob -Batch $this.Batch).State -match 'Running') {
                        $this.State = "Running"
                    }
                    ELSE {
                        Write-Verbose "Receiving RSJob"
                        $this.State = "Completed"
                        $this.resultlist = Receive-RSJob -Batch $this.Batch -ErrorAction SilentlyContinue
                        Try {
                            Write-PSFMessage (($this.resultlist[0]).pstypenames | Out-String)
                            $this.View = (($this.resultlist[0]).pstypenames | ForEach-Object { IF ($_ -notmatch "Selected.RSJob") { $_ } } | Select-Object -first 1).Replace('Deserialized.', '')   # Works for PSLocal
                            Write-PSFMessage $this.View
        
                        }
                        Catch {
                            $this.View = "Generic"
                        }

                        $DataTable = $this.resultlist | Out-DataTable
                        $this.Count = $datatable | Measure-Object | select -ExpandProperty Count
                        Invoke-SQLiteBulkCopy -DataTable $DataTable -DataSource (Get-PSFConfigValue -FullName Gat30.SQLLiteDBPath) -Table $this.Batch -Confirm:$false
                        

                        $Query = @"
                        UPDATE GUIJobs
                        SET View = '$($this.View)', State = '$($this.State)', Count = '$($this.Count)'
                        WHERE Batch = '$($this.Batch)'
"@


                        Invoke-SqliteQuery -Query $Query -DataSource (Get-PSFConfigValue -FullName Gat30.SQLLiteDBPath) -As DataTable
                        

                    }
                    IF ($this.Type -eq 'List') {
                        $this.Count = $this.resultlist.Count
                    }
                    ELSE {
                        $this.Count = "$(($this.JobInfo.State -match 'Completed|Failed|Stopped|Suspended|Disconnected').Count) / $($this.JobInfo.Count)"
                    }
                }

                { $_ -eq "PSLocal" -or $_ -eq "PSRemote" } {
                    $joba = Get-Job -Name  $this.Batch
                    IF (($joba).State -match 'Running') {
                        $this.State = "Running"
                    }
                    ELSE {
                        $this.State = "Completed"
                        $this.resultlist = Get-Job -Name $this.Batch | Receive-Job -keep

                    }
                    $this.Count = "$(($joba.state -match 'Completed|Failed|Stopped|Suspended|Disconnected').Count) / $($joba.Count)"

                }
                { $_ -eq "MiniApp" } {
                    $result = Receive-RSJob -Batch $this.Batch
                    $result | ForEach-Object { $this.resultlist.add($_) }
                    $this.State = "Completed"
                }
            }

            
        }
        return $this
    }

    [void]Remove() {
        Remove-RSJob -Batch $this.Batch
        $this.JobInfo = "Removed"
    }

    [void]Stop() {
        Stop-RSJob -Batch $this.Batch
        $this.JobInfo = "Stopped"
    }

}