Jump to content
JLogan3o13

Running scripts and capturing output

Recommended Posts

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.


"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

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

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

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Share this post


Link to post
Share on other sites
The problem is here:
$sText &= $sOutput
_GUICtrlEdit_AppendText($editProgress, $sText)

You reading new line and combining with old stuff and then appending. (Every single time)

 

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
            _GUICtrlEdit_AppendText($editProgress, $sOutput)
        EndIf
    WEnd

 

Share this post


Link to post
Share on other sites

@paradox109 typically people aren't still looking for an answer after 3 years, so we tend to frown on necroposting. Thanks though, maybe someone will wander along and find benefit.


"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...