JLogan3o13

Running scripts and capturing output

3 posts in this topic

#1 ·  Posted

Just a curiosity question, which is why I am not posting anything runnable. I know several of you have been doing some great work marrying AutoIt and PowerShell, and am just curious what you are doing in this regard. I find myself running almost exclusively in PowerCLI lately, as 99% of my contracts are for virtualization tasks. I fall back on AutoIt for anything that requires a GUI (of course!) because I don't have the patience for GUIs in pure PS. One thing I have noticed is returning output from the PowerShell script. At present I am just using the old StdoutRead method:

Local $sPSScript = @ScriptDir & "\Launch.ps1"
Local $sText = "", $sSTDOUT = ""
Local $sCMD = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command . '" & $sPSScript & "'"
Local $pid = Run($sCMD, @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD)
    StdinWrite($pid)

    While 1
        $sOutput = StdoutRead($pid)
            If @error Then ExitLoop
        If $sOutput <> "" Then
            $sText &= $sOutput
            _GUICtrlEdit_AppendText($editProgress, $sText)
        EndIf
    WEnd

As the script runs it outputs to the edit field. However, I am continually appending said field so my output is duplicated. For example, a PowerCLI script to connect to 4 vCenter servers returns output like so:

Quote

Name                           Port  User                          
Name                           Port  User                          
----                           ----  ----                          
mycompany-vc02.mycompany.com     443   mycompany\sa871022            

Name                           Port  User                          
----                           ----  ----                          
mycompany-vc02.mycompany.com     443   mycompany\sa871022            
mycompany-vs01mgt01.mycompany... 443   mycompany\sa871022            

Name                           Port  User                          
----                           ----  ----                          
mycompany-vc02.mycompany.com     443   mycompany\sa871022            
mycompany-vs01mgt01.mycompany... 443   mycompany\sa871022            
mycompany-vs01mgt01.mycompany... 443   mycompany\sa871022            

Name                           Port  User                          
----                           ----  ----                          
mycompany-vc02.mycompany.com     443   mycompany\sa871022            
mycompany-vs01mgt01.mycompany... 443   mycompany\sa871022            
mycompany-vs01mgt01.mycompany... 443   mycompany\sa871022            
mycompany-vsvc02.mycompany...    443   mycompany\sa871022            

Name                           Port  User                          
----                           ----  ----                          
mycompany-vc02.mycompany.com     443   mycompany\sa871022            
mycompany-vs01mgt01.mycompany... 443   mycompany\sa871022            
mycompany-vs01mgt01.mycompany... 443   mycompany\sa871022            
mycompany-vsvc02.mycompany...    443   mycompany\sa871022            


Name                           Port  User                          
----                           ----  ----                          
mycompany-vc02.mycompany.com     443   mycompany\sa871022            
mycompany-vs01mgt01.mycompany... 443   mycompany\sa871022            
mycompany-vs01mgt01.mycompany... 443   mycompany\sa871022            
mycompany-vsvc02.mycompany...    443   mycompany\sa871022

Just curious if anyone is doing any different; the duplication of output gets to be tedious when dealing with longer scripts.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites



One the things you can do is using the famous  Out-Gridview -Passthru output to get the data in a more structured way.

So you don't need to build any extra GUI ;-)

Once you run it you can see the Grid View popping up and use that that for filtering etc...

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thanks, I thought about that as well. I can get all the data in a single window if I wrap it all in a function and the just output the function to GridView. The problem I encounter is when you're returning different objects and values. For example,  in the first part of the function I connect to a several vCenter Servers:

$hosts = @(
    "vCenter01",
    "vCenter02",
    "vCenter03",
    "vCenter04"
)

$cred = Get-Credential
Connect-VIServer -Server $hosts -Credential $cred

Output to GridView looks good,  auto-generated columns for Server, Port. and User. However if I do another call like a Get-VirtualPortGroup, GridView doesn't know what to do with the different return types and columns, so it ends up changing to just two columns, Value and Type. For anything that returns more than one value (everything) this cuts off the majority of what gets returned.

I am playing around with another function to take in the text from stdout in blocks, remove any duplicate lines, then pass to the Edit control. If it doesn't cause too much of a lag, this may be the way I have to go.

Edited by JLogan3o13

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now