简体   繁体   中英

Create custom output with variables

I'm trying to write a script for the technicians and end users.

My script is the "framework" so to speak that calls packages that are written in batch scripts by other people.

Long story short, initially I wanted to get the exit codes for each batch script but couldn't manage to do that. So I ended up pulling the return codes for each batch file from the log that it creates. Which brought me here.

What I'm trying to do (maybe the wring way to do this) is create an empty array, and add the , application names, final return codes, log file location and explanation of the final return code.

My code kind of works. But I couldn't get the table to show each application on a new line. Rather it just keeps adding next to it.

Any ideas? Either with what I'm trying to do or I was initially trying to do?

Thank you everyone in advance.

$a = new-object psobject -Property 
     @{
        AppName= ""
        FinalReturnCode = ""
        LogFileLocation  = ""
      }

        $APP1 = "Adobe_Reader"
        $a.Appname += $APP1
        $App1Log = "C:\Logs\Adobe_Reader.log"
        $a.LogfileLocation += $App1Log
        $App3LogResult = Get-Content $App1Log | Where{$_ -match "Final Return Code:\s*(\d*)"}
        $App1FinalReturnCode = $App1LogResult.Split("")[-2]
        $a.FinalReturnCode += $App1FinalReturnCode

        $APP2 = "Adobe_FlashPlayer"
        $a.Appname += $APP2
        $App2Log = "C:\Logs\Adobe_Flash.log"
        $a.LogfileLocation += $App2Log
        $App2LogResult = Get-Content $App2Log | Where{$_ -match "Final Return Code:\s*(\d*)"}
        $App2FinalReturnCode = $App2LogResult.Split("")[-2]
        $a.FinalReturnCode += $App2FinalReturnCode



$a | Format-Table  Appname, LogFileLocation, FinalReturnCode -AutoSize
$a = New-Object psobject -Property @{
                                       AppName= ""
                                       FinalReturnCode = ""
                                       LogFileLocation  = ""
                                   }
...
$a.AppName += $APP1
...
$a.AppName += $APP2

Here, $a.AppName is a string, not an array, this is why it keeps "adding" as you say.

Repeating two almost identical blocks of code is a recipe for disaster, instead I would use an array of hashtables to store the per-application information like this:

$Apps = @(
    @{
        Name = "Adobe_Reader"
        Logfile = "C:\Logs\Adobe_Reader.log"
    },
    @{
        Name = "Adobe_FlashPlayer"
        Logfile = "C:\Logs\Adobe_Flash.log"
    }
)

# Array to store the results
$Results = @()

foreach($App in $Apps){
    $LogResult = Get-Content $App.Logfile |Where-Object { $_ -match "Final Return Code:\s*(\d*)" }
    $ReturnCode = $LogResult.Split("")[-2]
    # Create new object and add it to the array
    $Results += New-Object psobject -Property @{
        AppName = $App.Name
        LogfileLocation = $App.Logfile
        FinalReturnCode = $ReturnCode
    }
}

$Results

Now you can easily add new applications, simply expand the $Apps array

If you intend to use this as a "Framework", you might want to avoid output formatting cmdlets like Format-Table - it makes reuse of the output impossible cumbersome and annoying

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM