Single Line Scroll every 30 Seconds After Launching Script

Welcome Forums General PowerShell Q&A Single Line Scroll every 30 Seconds After Launching Script

This topic contains 6 replies, has 3 voices, and was last updated by

 
Participant
2 months ago.

  • Author
    Posts
  • #114204

    Participant
    Points: 0
    Rank: Member

    Guys,

    I've been looking all over for weeks now, and simply cannot find an answer to this.

    I have a script that uses OpenFileDialog to pull in a file for parsing. Pardon my (borrowed) code, please, I am NOT a professional.

    Function Get-FileName {
        param(
            [Parameter(Mandatory=$true)]
            [string] $initialDirectory,
            [Parameter(Mandatory=$true)]
            [string] $title
        )
        [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
        $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
        $OpenFileDialog.Title = $title
        $OpenFileDialog.initialDirectory = $initialDirectory
        $OpenFileDialog.filter = "CSV (*.csv)| *.csv"
        $result = $OpenFileDialog.ShowDialog()
        If ($result -eq "OK") {
            Write-Host "Selected File: $($OpenFileDialog.filename)" -ForegroundColor Green  
            Write-Host "File Content Imported!" -ForegroundColor Green 
            Return $OpenFileDialog.filename
        }
        Else {
            Write-Host "File Content Import Cancelled!" -ForegroundColor Yellow
            Exit
        }
    }
    
    # Set the default directory for the File Open Dialog to prevent lots of browsing around
    $directory = "$env:USERPROFILE\Desktop"
    
    # Prompt for, and fix the downloaded files
    $file = Get-FileName $directory "New Hires File"
    $obj = Import-CSV $file

    What I cannot figure out is why, after running this script from the command line, my command line scrolls by one line every 30 seconds until I close and launch another command line. I have seen this in one other script (which I no longer have) and the only commonality between them is the OpenFileDialog code.

    This happens no matter what I'm doing at the command prompt. The only way to stop the one-line auto-advance is to launch a new console window.

    It's the weirdest thing, and frustratingly difficult to Google for...

  • #114210

    Participant
    Points: 288
    Helping Hand
    Rank: Contributor

    Works fine for me.

    What kind of file are you using this with?

    It's written to only work with CSVs. So if you're trying with something else, it may have weird effects depending on the file.

    • #114223

      Participant
      Points: 0
      Rank: Member

      Thanks for the reply, Mark. I have confirmed that it is this block of code by defining it specifically in a console window. Called the function and now it's scrolling.

      I keep wondering if there's some OpenFileDialog cleanup I'm missing, but haven't had any luck finding anything. I do have a pretty elaborate profile, now that I think about it, so maybe there's some weird interaction between it and my environment.

      Time for more testing...

  • #114241

    Participant
    Points: 0
    Rank: Member

    Some additional details. I cleared my profile and re-imported the function – and the behavior continues. So strange!

    Here's my version information.

    Name             : ConsoleHost
    Version          : 5.1.17134.228
    InstanceId       : 2e17744f-eca7-47eb-94da-7a8f453c1e7a
    UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
    CurrentCulture   : en-US
    CurrentUICulture : en-US
    PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
    DebuggerEnabled  : True
    IsRunspacePushed : False
    Runspace         : System.Management.Automation.Runspaces.LocalRunspace
  • #114249

    Participant
    Points: 190
    Helping Hand
    Rank: Participant

    Most likely it's one of the OpenFileDialog lines that output something.
    You could troubleshoot this by various means but if you e.g. add a Write-Host "1" before the first openfiledialog line, then Write-Host "2" after second you will at least see which one triggers the extra output.

  • #114276

    Participant
    Points: 0
    Rank: Member

    Thanks for the suggestion, Fredrik. I've narrowed it down to

    $result = $OpenFileDialog.ShowDialog()

    At this point, I'm again at a loss. I know where it is, but I don't know why, or what the alternatives are. At the end of the day, it's not a huge deal – more of a curiosity at this point. I'm just baffled, and would love to know what's going on.

    The auto-scrolling was happening before I assigned the output to $result, when I simply returned the value $OpenFileDialog.filename, so that didn't create the issue.

    If I ever come across the solution, I'll be sure to update this post. In the meantime, come with whatever suggestions time and curiosity allow.

    Thanks!

  • #114399

    Participant
    Points: 190
    Helping Hand
    Rank: Participant

    Don't know to be honest, probably something in the way that ShowDialog() is launching, launching maybe a seperate process etc. that generate some output.
    But haven't used those methods and classes myself so don't have any experience about them.
    Could be interesting to test them in a C# console program and see if it behaves the same.

    Saw some blog posts that prefered ApplicationContext and Application.Run instead of ShowDialog().
    E.g.

    https://blog.netnerds.net/2016/01/showdialog-sucks-use-applicationcontexts-instead/

You must be logged in to reply to this topic.