Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
4.0 kB
1
Indexable
Never
$ErrorActionPreference = 'Continue'
$Command = 'Get-BitlockerVolume'

Function Run-ProgramwithStdout {
    param($Command)
    $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" = "$Command"
        "Result" = "Success"
    }

    $ts = Measure-Command {
        $return = Start-Process -FilePath "powershell" -ArgumentList "-NoProfile -ExecutionPolicy Bypass -Command $Command" -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
        }
    } 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
        }
    } else {
        $Detail += New-Object PSObject -Property @{
            'Attribute' = 'File does not exist'
            'Value' = $StdErrorFile
            'Result' = 'Failure'
        }
    }
    $TextReturn += $FileContents

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

Try {
    $htReturn = Run-ProgramwithStdout -Command $Command

    if ($htReturn.ExitCode -eq 0) {
        $Comment = 'Success'
        $Success = 1
        $Failure = 0
    } else {
        $Comment = 'Bad return code'
        $Success = 0
        $Failure = 1
    }
} Catch {
    $ExceptionObject = $_
    # Handle exceptions as needed
    $Comment = 'Error'
    $Success = 0
    $Failure = 1
    $Count = 0
} Finally {
    $return = @{
        'Summary' = @{
            "Status" = 'Ready'
            "Time" = (Get-Date)
            "Comment" = $Comment
            "SuccessPoints" = $Success
            "FailurePoints" = $Failure
            "Count" = $Count
            "ExitCode" = $htReturn['ExitCode']
            "ExecutionTime" = $htReturn['ExecutionTime']
        }
        'Txt' = $htReturn['Text']
    }
    $return 
}
Leave a Comment