Capturing output best way

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 9 months ago.

  • Author
    Posts
  • #5130

    by kamcknig at 2012-09-20 08:56:35

    I'm brand new to PowerShell but I've figured out a lot on my own. Though maybe it doesn't look pretty haha. I have a script that I'm using for mobile development using Gideros Mobile.

    Here is what it does.
    1. Checks for an environment variable GIDEROS_HOME
    2. Checks to make sure Gideros Studio is closed, and asks to close it if it's not.
    3. Starts up the Gideros Player exe
    4. Runs a process called gdrbridge to see if gdrdaemon (gdrdaemon is woken up and controlled by gdrbridge) is connected to the Gideros Player (if you can help me optimize this that would be great)
    5. Once connected, runs gdrbridge on a project file to run my project in the Gideros Player

    I could use help with step 4, in case there is a better way than how I'm doing it, that's the function "connectGdrDaemon" in my code. B

    But my real questions is after all this is done, gdrbridge has a parameter i can pass called "getlog" which will print the output from gdrbridge. So in my project I can use print() statements and "gdrbridge getlog" will print all messages since the last time getlog was called. How can I get the output from gdrbridge getlog and poll it. I'd like to send it to the console output if I can. If not then to a file and tail the file if possible. I dont mind having some of this in a separate script file if need be or keeping it all in this one script file. But I have no clue about how to do it. All the googling I've done on capture output hasn't helped in the least!

    Here is my current script:

    param(
    [Parameter(Position=0, Mandatory=$true)]
    [String]$projectPath
    )

    #make sure path doesnt start or end with quotes, this will happen if user trys to run without entering path
    #and is therefore prompted for it. You don't actually need the quotes in the case, but most people will
    #probably still use them.
    if($projectPath.StartsWith("'") -or $projectPath.StartsWith("`"")) {
    $projectPath = $projectPath.Substring(1)
    }

    if($projectPath.EndsWith("'") -or $projectPath.EndsWith("`"")) {
    $projectPath = $projectPath.Substring(0, 54)
    }

    #Make sure environment variable GIDEROS_HOME exists
    Write-Host "`nTesting if GIDEROS_HOME exists..."
    if(-not (Test-Path env:\GIDEROS_HOME)) {
    Write-Host "[ERROR] You must set the GIDEROS_HOME environment variable."
    return
    }

    #Make sure Gideros Studio is not running
    if($gideros = Get-Process GiderosStudio -ErrorAction 0) {
    $closeGiderosStudio = Read-Host "`n[WARNING] You must first close Gideros Studio. Would you like to close it now? (If not, build execution will halt)`t[Y] or [N]"

    if($closeGiderosStudio -eq 'y' -or $closeGiderosStudio -eq 'Y') {
    if(-not $gideros.HasExited) {
    Stop-Process $gideros.Id -ErrorAction 0
    }
    }
    elseif(-not $gideros.HasExited){
    return
    }
    else {
    Write-Host "`nGideros Studio has been closed in some other fashion, continuing build process.."
    }
    }

    #start up the gideros player if it's not already
    if(-not (Get-Process GiderosPlayer -ErrorAction 0) ) {
    Write-Host "`nStarting gideros player..."
    $giderosPlayer = Start-Process ($env:GIDEROS_HOME + "\GiderosPlayer.exe") -PassThru
    }
    else {
    Write-Host "`nGideros player already running..."
    $giderosPlayer = Get-Process GiderosPlayer
    }

    #test to see if gdrdaemon is connected to gideros player, if not wait until it is
    Function connectGdrDaemon() {
    Write-Host "Connecting to gdrdaemon..."
    $isConnected = & ($env:GIDEROS_HOME + "\Tools\gdrbridge.exe") "isconnected"

    if($isConnected -eq 0) {
    Write-Host "gdrbridge is not connected, trying again..."
    Start-Sleep 1
    connectGdrDaemon
    }
    else {
    Write-Host "gdrbridge is connected..."
    & ($env:GIDEROS_HOME + "\Tools\gdrbridge.exe") "play" "$projectPath"
    }
    }

    Write-Host "`nStarting gdrdaemon and running project...`n"
    connectGdrDaemon

    Thanks so much! Also, like I said, I'm pretty new so details are welcome if possible 🙂
    Kyle

    by kamcknig at 2012-09-20 10:00:02

    I've changed my code to this:


    Function getLog {
    & ($env:GIDEROS_HOME + "\Tools\gdrbridge.exe") "getlog" | Out-Host
    Start-Sleep 1
    getLog
    }

    #test to see if gdrdaemon is connected to gideros player, if not wait until it is
    Function connectGdrDaemon() {
    Write-Host "Connecting to gdrdaemon..."
    $isConnected = & ($env:GIDEROS_HOME + "\Tools\gdrbridge.exe") "isconnected"

    if($isConnected -eq 0) {
    Write-Host "gdrbridge is not connected, trying again..."
    Start-Sleep 1
    connectGdrDaemon
    }
    else {
    Write-Host "gdrbridge is connected..."
    & ($env:GIDEROS_HOME + "\Tools\gdrbridge.exe") "play" "$projectPath"
    Start-Sleep 1
    getLog
    }
    }

    It does print my output to the console like i want, but i'd still like some input for better ways to do it if there are any

    by poshoholic at 2012-09-21 08:19:37

    That's a pretty good script. You could clean-up your Write-Host messages if you don't want them taking up as much space on the screen. You also shouldn't need to quote all of the strings you are quoting. And Out-Host is implicit, so you don't need to use it in your script. For example:

    function getLog {
    & $env:GIDEROS_HOME\Tools\gdrbridge.exe getlog
    Start-Sleep 1
    getLog
    }

    #test to see if gdrdaemon is connected to gideros player, if not wait until it is
    function connectGdrDaemon() {
    Write-Host Connecting to gdrdaemon... -NoNewLine
    $isConnected = & $env:GIDEROS_HOME\Tools\gdrbridge.exe isconnected

    if ($isConnected -eq 0) {
    Write-Host . -NoNewLine
    Start-Sleep 1
    connectGdrDaemon
    }
    else {
    Write-Host Connected!
    & $env:GIDEROS_HOME\Tools\gdrbridge.exe play $projectPath
    Start-Sleep 1
    getLog
    }
    }

    That should make it appear more like a progress, showing more and more dots the longer it takes to connect. You could also set up connectGdrDaemon to have a configurable timeout with some default value, and have it output 'Connection failed!' if it takes too long to connect. As for the output of the log messages, your script seems pretty straight-forward, and I don't see any reason to change it (aside from removing the Out-Host, which is implicit in PowerShell).

    by kamcknig at 2012-09-21 08:39:00

    Great thanks so much! Glad to know I was able to figure it all out on my own then! Thanks for the input!

    Kyle

You must be logged in to reply to this topic.