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…
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.
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.
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.
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.