Untitled

 avatar
unknown
plain_text
a year ago
6.1 kB
8
Indexable
$ErrorActionPreference = 'Continue'
$FilePath = 'ipconfig.exe'
$ArgumentList  = '/all'

Function Run-ProgramwithStdout{
    param($FilePath, $ArgumentList)
    $detail = [System.Collections.ArrayList]@()

    [string]$TextReturn = ''

    $StdOutputFile = "$($ENV:TEMP)\$($pid)-stdout.log"
    $StdErrorFile = "$($ENV:TEMP)\$($pid)-stderr.log"
                    $Detail += new-object PSObject -Property @{
                    "Attribute"= "Command line"
                    "Value" = "$($filepath) $($ArgumentList)"
                    "Result" = "Success"
                }

    $ts = measure-command{$return = start-process -FilePath $FilePath -ArgumentList $ArgumentList -Wait -PassThru -RedirectStandardOutput $StdOutputFile -RedirectStandardError $StdErrorFile}

    if ($return.exitcode -eq 0){
                $Detail += new-object PSObject -Property @{
                    "Attribute"= "Return code"
                    "Value" = "$($return.exitcode)"
                    "Result" = "Success"
                }
    }else{
                $Detail += new-object PSObject -Property @{
                    "Attribute"= "Return Code Error"
                    "Value" = "$($return.exitcode)"
                    "Result" = "Failure"
                }
    }

        # Standard output
    if (Test-Path -path $StdOutputFile -PathType Leaf){
        $detail += new-object psobject -Property @{
            'Attribute' = 'File exists'
            'Value' = $StdOutputFile
            'Result' = 'Success'
        }

        $LineNumber = 1
        $FileContents = get-content -path $StdOutputFile -raw

        forEach ($Line in $FileContents){
                    $detail += new-object psobject -Property @{
                        'Attribute' = "StdOut Line: $($Linenumber)"
                        'Value' = $Line
                        'Result' = "Success"
                    }
                $LineNumber += 1
            }
#            $LineNumber
#$line 
#            $textReturn += $line + '`r`n' #[System.Environment.NewLine]
        
    }Else{
        $detail += new-object psobject -Property @{
            'Attribute' = 'File does not exist'
            'Value' = $StdOutputFile
            'Result' = 'Failure'
        }
    }
    $TextReturn = $FileContents


    # Standard Error
    if (Test-Path -path $StdErrorFile -PathType Leaf){
        $detail += new-object psobject -Property @{
            'Attribute' = 'File exists'
            'Value' = $StdErrorFile
            'Result' = 'Success'
        }

        $LineNumber = 1
        $FileContents = get-content -path $StdErrorFile -raw

        forEach ($Line in $FileContents){
            $detail += new-object psobject -Property @{
                'Attribute' = "StdErr Line: $($Linenumber)"
                'Value' = $Line
                'Result' = "Success"
            }
            $LineNumber += 1
#            $textReturn += $line + '`r`n' #[System.Environment.NewLine]
        }
    }Else{
        $detail += new-object psobject -Property @{
            'Attribute' = 'File does not exist'
            'Value' = $StdErrorFile
            'Result' = 'Failure'
        }
    }
    $TextReturn += $FileContents

    $ht = @{}
    $ht.add('Text',$TextReturn)
    $ht.add('ExecutionTime',[math]::round($ts.TotalSeconds,0))
    $ht.add('ExitCode',$return.exitcode)
    return $ht
}




Try{
    
    $htReturn = Run-ProgramwithStdout -FilePath $FilePath -ArgumentList $ArgumentList 

    if ($htReturn.ExitCode -eq 0){
            $Comment = 'Success'
            $Success = 1
            $Failure = 0
        }else{
            $Comment = 'Bad return code'
            $Success = 0
            $Failure = 1
    }
}Catch{
            $ExceptionObject = $_
            $Detail += new-object PSObject -Property @{
                "Error"="Error Message" 
                "Value" = $ExceptionObject.ToString()
            }
            $Detail += new-object PSObject -Property @{
                "Error"="Error Exception" 
                "Value" = $ExceptionObject.Exception
            }
            $Detail += new-object PSObject -Property @{
                "Error"="Error Code" 
                "Value" = $ExceptionObject.exception.ErrorCode
            }
            $Detail += new-object PSObject -Property @{
                "Error"="Script Stack Trace" 
                "Value" = $ExceptionObject.ScriptStackTrace
            }
            $Detail += new-object PSObject -Property @{
                "Error"="InvocationInfo" 
                "Value" = $ExceptionObject.InvocationInfo.Line
            }
            $Detail += new-object PSObject -Property @{
                "Error"="Position Message" 
                "Value" = $ExceptionObject.InvocationInfo.PositionMessage
            }
            $Detail += new-object PSObject -Property @{
                "Error"="Command Path" 
                "Value" = $ExceptionObject.InvocationInfo.PSCommandPath
            }
            $Detail += new-object PSObject -Property @{
                "Error"="Line Number" 
                "Value" = $ExceptionObject.InvocationInfo.ScriptLineNumber
            }
            $Detail += new-object PSObject -Property @{
                "Error"="Script Name" 
                "Value" = $ExceptionObject.InvocationInfo.ScriptName
            }
    $comment = 'Error'
    $Success = 0
    $Failure = 1
    $Count = 0
}Finally{
    $return = @{}
    $Summary = new-object PSObject -Property @{
        "Status" = 'Ready'
        "Time" = (Get-date)
        "Comment" = $comment
        "SuccessPoints" = $Success
        "FailurePoints" = $Failure
        "Count" =  $count # $htReturn['text'] | measure-object -Line
        "ExitCode" = $htReturn['ExitCode']
        "ExecutionTime" = $htReturn['ExecutionTime']
    }
    $return.add('summary',$summary)
    #$return.add('detail',$detail)
    $return.add('txt',$htReturn['text'])
    $return 
}
Editor is loading...
Leave a Comment